Skip to content

Commit

Permalink
fix(forms): "0" value is display correctly (#2015)
Browse files Browse the repository at this point in the history
fixes #2013 

---------

Co-authored-by: Sebastian Leidig <sebastian@aam-digital.com>
  • Loading branch information
TheSlimvReal and sleidig authored Oct 2, 2023
1 parent ec425c5 commit 63150d5
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Meta, StoryFn, applicationConfig } from "@storybook/angular";
import { applicationConfig, Meta, StoryFn } from "@storybook/angular";
import { StorybookBaseModule } from "app/utils/storybook-base.module";
import { DisplayDynamicPercentageComponent } from "./display-dynamic-percentage.component";
import { importProvidersFrom } from "@angular/core";

export default {
title: "Core/Entities/Display Properties/DisplayDynamicPercentage",
title: "Core/Entities/Properties/number/DisplayDynamicPercentage",
component: DisplayDynamicPercentageComponent,
decorators: [
applicationConfig({
Expand All @@ -28,3 +28,13 @@ Primary.args = {
decimalPlaces: 3,
},
};

export const Zero = Template.bind({});
Zero.args = {
entity: { allDays: 110, presentDays: 0 },
config: {
actual: "presentDays",
total: "allDays",
decimalPlaces: 3,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { CommonModule } from "@angular/common";
@DynamicComponent("DisplayPercentage")
@Component({
selector: "app-display-percentage",
template: "{{ value ? (value | number : numberFormat) + '%' : '-' }}",
template:
"{{ value !== undefined ? (value | number : numberFormat) + '%' : '-' }}",
standalone: true,
imports: [CommonModule],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const Template: StoryFn<DisplayPercentageComponent> = (

export const Low = Template.bind({});
Low.args = {
value: 5,
value: 0,
};
export const Medium = Template.bind({});
Medium.args = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DynamicComponent } from "../../../config/dynamic-components/dynamic-com
@DynamicComponent("DisplayUnit")
@Component({
selector: "app-display-unit",
template: '{{ value ? value + " " + config : "" }}',
template: '{{ value !== undefined ? value + " " + config : "" }}',
standalone: true,
})
export class DisplayUnitComponent extends ViewDirective<string, string> {}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ Basic.args = {
config: "kg",
};

export const Zero = Template.bind({});
Zero.args = {
value: 0,
config: "kg",
};

export const WithoutValue = Template.bind({});
WithoutValue.args = {
value: undefined,
Expand Down
1 change: 1 addition & 0 deletions src/app/core/basic-datatypes/number/number.datatype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Injectable } from "@angular/core";
@Injectable()
export class NumberDatatype extends DefaultDatatype<number, number> {
static dataType = "number";
viewComponent = "DisplayText";
editComponent = "EditNumber";

transformToDatabaseFormat(value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { TestBed } from "@angular/core/testing";
import { ChangeDetectorRef, ViewContainerRef } from "@angular/core";
import { DynamicComponentDirective } from "./dynamic-component.directive";
import { ComponentRegistry } from "../../../dynamic-components";

describe("DynamicComponentDirective", () => {
let directive: DynamicComponentDirective;
let mockContainer: jasmine.SpyObj<ViewContainerRef>;
let mockDetector: jasmine.SpyObj<ChangeDetectorRef>;
let mockRegistry: jasmine.SpyObj<ComponentRegistry>;

beforeEach(() => {
mockContainer = jasmine.createSpyObj(["clear", "createComponent"]);
mockDetector = jasmine.createSpyObj(["detectChanges"]);
mockRegistry = jasmine.createSpyObj(["get"]);
TestBed.configureTestingModule({
providers: [
{ provide: ViewContainerRef, useValue: mockContainer },
{ provide: ChangeDetectorRef, useValue: mockDetector },
{ provide: ComponentRegistry, useValue: mockRegistry },
DynamicComponentDirective,
],
});
directive = TestBed.inject(DynamicComponentDirective);
});

it("should create the configured component", async () => {
directive.appDynamicComponent = { component: "TestComp" };
const comp = {} as any;
mockRegistry.get.and.returnValue(() => Promise.resolve(comp));

await directive.ngOnChanges();

expect(mockRegistry.get).toHaveBeenCalledWith("TestComp");
expect(mockContainer.createComponent).toHaveBeenCalledWith(comp);
});

it("should assign the properties to the component", async () => {
directive.appDynamicComponent = {
component: "TestComp",
config: {
numberProp: 0,
stringProp: "should exist",
missingProp: "should not exist",
},
};
const changesSpy = jasmine.createSpy();
const comp: any = {
prototype: {
constructor: {
["ɵcmp"]: {
inputs: { numberProp: {}, stringProp: {}, otherProp: {} },
},
},
},
};
const compRef: any = { instance: { ngOnChanges: changesSpy } };
mockRegistry.get.and.returnValue(() => Promise.resolve(comp));
mockContainer.createComponent.and.returnValue(compRef);

await directive.ngOnChanges();

expect(compRef.instance.numberProp).toBe(0);
expect(compRef.instance.stringProp).toBe("should exist");
expect(compRef.instance.missingProp).toBeUndefined();
expect(compRef.instance.otherProp).toBeUndefined();
expect(compRef.instance.ngOnChanges).toHaveBeenCalledWith({
numberProp: jasmine.anything(),
stringProp: jasmine.anything(),
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class DynamicComponentDirective implements OnChanges {
private changeDetector: ChangeDetectorRef,
) {}

async ngOnChanges() {
await this.loadDynamicComponent();
ngOnChanges() {
return this.loadDynamicComponent();
}

private async loadDynamicComponent() {
Expand All @@ -57,7 +57,7 @@ export class DynamicComponentDirective implements OnChanges {

private setInputProperties(proto, component) {
const inputs = Object.keys(proto.constructor["ɵcmp"].inputs).filter(
(input) => this.appDynamicComponent.config?.[input],
(input) => this.appDynamicComponent.config?.[input] !== undefined,
);
const inputValues = pick(this.appDynamicComponent.config, inputs);
const initialValues = pick(component, inputs);
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/core-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const coreComponents: ComponentTuple[] = [
"DisplayDynamicPercentage",
() =>
import(
"./basic-datatypes/number/display-dyanamic-percentage/display-dynamic-percentage.component"
"./basic-datatypes/number/display-dynamic-percentage/display-dynamic-percentage.component"
).then((c) => c.DisplayDynamicPercentageComponent),
],
[
Expand Down

0 comments on commit 63150d5

Please sign in to comment.