Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 0 value is ignored #2015

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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