Skip to content

Commit

Permalink
feat(lib): enable strict mode in Typescript and Angular
Browse files Browse the repository at this point in the history
  • Loading branch information
gund committed Aug 29, 2022
1 parent 72c32fd commit fbac529
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('ComponentOutletInjectorDirective', () => {
})
class HostComponent {
@ViewChild(ComponentOutletInjectorDirective, { static: false })
directive: ComponentOutletInjectorDirective;
component: Type<any>;
directive?: ComponentOutletInjectorDirective;
component?: Type<any>;
}

const testSetup = new TestSetup(HostComponent, {
Expand All @@ -37,14 +37,14 @@ describe('ComponentOutletInjectorDirective', () => {

const directive = fixture.getHost().directive;

expect(directive.componentRef).toBeInstanceOf(ComponentRef);
expect(directive?.componentRef).toBeInstanceOf(ComponentRef);
});

it('should hold instance of injected component in `componentRef`', async () => {
const fixture = await testSetup.redner();

const directive = fixture.getHost().directive;

expect(directive.componentRef.instance).toBeInstanceOf(DynamicComponent);
expect(directive?.componentRef.instance).toBeInstanceOf(DynamicComponent);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Directive: ComponentOutletIo', () => {
template: `<ng-container *ngComponentOutlet="component"></ng-container>`,
})
class HostComponent {
component: Type<any>;
component?: Type<any>;
}

const testSetup = new TestSetup(HostComponent, {
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('Directive: ComponentOutletIo', () => {

expect(outputs.output).not.toHaveBeenCalled();

fixture.getComponent(DynamicComponent).output.emit('data');
fixture.getComponent(DynamicComponent)!.output.emit('data');

expect(outputs.output).toHaveBeenCalledWith('data');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { InputsType, IoService, OutputsType } from '../../io';
})
export class ComponentOutletIoDirective implements DoCheck {
@Input()
ngComponentOutletNdcDynamicInputs: InputsType;
ngComponentOutletNdcDynamicInputs?: InputsType | null;
@Input()
ngComponentOutletNdcDynamicOutputs: OutputsType;
ngComponentOutletNdcDynamicOutputs?: OutputsType | null;

constructor(private ioService: IoService) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ describe('DynamicAttributesDirective', () => {
`,
})
class HostComponent {
component: Type<any>;
attrs: AttributesMap;
component?: Type<any> | null;
attrs?: AttributesMap | null;
}

class DynamicTestFixture<THost> extends TestFixture<THost> {
getDynamicElem() {
return this.getComponentElement(DynamicComponent);
return this.getComponentElement(DynamicComponent)!;
}
}

Expand Down Expand Up @@ -110,7 +110,10 @@ describe('DynamicAttributesDirective', () => {
});

it('should add new attr if added to object', async () => {
const attrs = { 'attr-one': 'val-1', 'attr-two': 'val-two' };
const attrs: Record<string, any> = {
'attr-one': 'val-1',
'attr-two': 'val-two',
};

const fixture = await testSetup.redner({ props: { attrs } });

Expand All @@ -131,7 +134,10 @@ describe('DynamicAttributesDirective', () => {
});

it('should remove attr if removed from object', async () => {
const attrs = { 'attr-one': 'val-1', 'attr-two': 'val-two' };
const attrs: Record<string, any> = {
'attr-one': 'val-1',
'attr-two': 'val-two',
};

const fixture = await testSetup.redner({ props: { attrs } });

Expand Down Expand Up @@ -188,7 +194,7 @@ describe('DynamicAttributesDirective', () => {
fixture.setHostProps({ component: Dynamic2Component });

expect(
fixture.getComponentElement(Dynamic2Component).attributes,
fixture.getComponentElement(Dynamic2Component)?.attributes,
).toMatchObject({
'attr-one': 'val-1',
'attr-two': 'val-two',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,28 @@ interface AttributeActions {
})
export class DynamicAttributesDirective implements DoCheck {
@Input()
ndcDynamicAttributes: AttributesMap;
ndcDynamicAttributes?: AttributesMap | null;
@Input()
ngComponentOutletNdcDynamicAttributes: AttributesMap;
ngComponentOutletNdcDynamicAttributes?: AttributesMap | null;

private attrsDiffer = this.differs.find({}).create<string, string>();
private lastCompType: Type<unknown>;
private lastAttrActions: AttributeActions;
private lastCompType?: Type<unknown>;
private lastAttrActions?: AttributeActions;

private get _attributes() {
return (
this.ndcDynamicAttributes || this.ngComponentOutletNdcDynamicAttributes
this.ndcDynamicAttributes ||
this.ngComponentOutletNdcDynamicAttributes ||
{}
);
}

private get nativeElement() {
return this.componentInjector.componentRef?.location.nativeElement;
return this.componentInjector?.componentRef?.location.nativeElement;
}

private get compType() {
return this.componentInjector.componentRef?.componentType;
return this.componentInjector?.componentRef?.componentType;
}

private get isCompChanged() {
Expand Down Expand Up @@ -93,8 +95,8 @@ export class DynamicAttributesDirective implements DoCheck {
}
}

private updateAttributes(actions: AttributeActions) {
if (!this.compType) {
private updateAttributes(actions?: AttributeActions) {
if (!this.compType || !actions) {
return;
}

Expand All @@ -113,9 +115,9 @@ export class DynamicAttributesDirective implements DoCheck {
remove: [],
};

changes.forEachAddedItem((r) => (attrActions.set[r.key] = r.currentValue));
changes.forEachAddedItem((r) => (attrActions.set[r.key] = r.currentValue!));
changes.forEachChangedItem(
(r) => (attrActions.set[r.key] = r.currentValue),
(r) => (attrActions.set[r.key] = r.currentValue!),
);
changes.forEachRemovedItem((r) => attrActions.remove.push(r.key));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ describe('Directive: DynamicDirectives', () => {
></ng-container>`,
})
class HostComponent {
component: Type<any>;
directives: DynamicDirectiveDef<any>[];
component?: Type<any> | null;
directives?: DynamicDirectiveDef<any>[] | null;
onCreated = jest.fn();
}

Expand Down Expand Up @@ -77,7 +77,7 @@ describe('Directive: DynamicDirectives', () => {
out = new EventEmitter<any>();
@Output()
out2 = new EventEmitter<any>();
hooksOrder = [];
hooksOrder: string[] = [];
ngAfterContentChecked = jest
.fn()
.mockImplementation(this.logHook('ngAfterContentChecked'));
Expand Down Expand Up @@ -174,8 +174,9 @@ describe('Directive: DynamicDirectives', () => {

const fixture = await testSetup.redner({ props: { directives } });

const dir = fixture.getFirstDirective();
const dir = fixture.getFirstDirective()!;

expect(dir).toBeDefined();
expect(dir.ngOnInit).toHaveBeenCalledTimes(1);
expect(dir.ngAfterContentInit).toHaveBeenCalledTimes(1);
expect(dir.ngAfterContentChecked).toHaveBeenCalledTimes(1);
Expand All @@ -200,8 +201,9 @@ describe('Directive: DynamicDirectives', () => {

const fixture = await testSetup.redner({ props: { directives } });

const dir = fixture.getFirstDirective();
const dir = fixture.getFirstDirective()!;

expect(dir).toBeDefined();
expect(dir.hooksOrder).toEqual([
'inputSet:true',
'ngOnChanges',
Expand Down Expand Up @@ -291,11 +293,12 @@ describe('Directive: DynamicDirectives', () => {

expect(MockDirective.INSTANCES.size).toBe(1);

const dir = fixture.getFirstDirective();
const dir = fixture.getFirstDirective()!;

fixture.setHostProps({ component: null });

expect(MockDirective.INSTANCES.size).toBe(0);
expect(dir).toBeDefined();
expect(dir.ngOnDestroy).toHaveBeenCalledTimes(1);
});

Expand Down Expand Up @@ -546,8 +549,8 @@ describe('Directive: DynamicDirectives', () => {
const callback1 = jest.fn();
const callback2 = jest.fn();
const directives = [
dynamicDirectiveDef(MockDirective, null, { out: callback1 }),
dynamicDirectiveDef(Mock2Directive, null, { out: callback2 }),
dynamicDirectiveDef(MockDirective, undefined, { out: callback1 }),
dynamicDirectiveDef(Mock2Directive, undefined, { out: callback2 }),
];

const fixture = await testSetup.redner({ props: { directives } });
Expand All @@ -566,13 +569,15 @@ describe('Directive: DynamicDirectives', () => {
it('should be connected when changed and disconnected from old', async () => {
const callback = jest.fn();
const outputs = { out: callback };
const directives = [dynamicDirectiveDef(MockDirective, null, outputs)];
const directives = [
dynamicDirectiveDef(MockDirective, undefined, outputs),
];

const fixture = await testSetup.redner({ props: { directives } });

expect(MockDirective.INSTANCES.size).toBe(1);

const dir = fixture.getFirstDirective();
const dir = fixture.getFirstDirective()!;

const callback2 = jest.fn();
outputs.out = callback2;
Expand All @@ -588,15 +593,17 @@ describe('Directive: DynamicDirectives', () => {
it('should disconnect when removed', async () => {
const callback = jest.fn();
const outputs = { out: callback };
const directives = [dynamicDirectiveDef(MockDirective, null, outputs)];
const directives = [
dynamicDirectiveDef(MockDirective, undefined, outputs),
];

const fixture = await testSetup.redner({ props: { directives } });

expect(MockDirective.INSTANCES.size).toBe(1);

const dir = fixture.getFirstDirective();
const dir = fixture.getFirstDirective()!;

outputs.out = null;
outputs.out = null!;

fixture.detectChanges();

Expand All @@ -608,14 +615,14 @@ describe('Directive: DynamicDirectives', () => {
it('should disconnect when host component destroyed', async () => {
const callback = jest.fn();
const directives = [
dynamicDirectiveDef(MockDirective, null, { out: callback }),
dynamicDirectiveDef(MockDirective, undefined, { out: callback }),
];

const fixture = await testSetup.redner({ props: { directives } });

expect(MockDirective.INSTANCES.size).toBe(1);

const dir = fixture.getFirstDirective();
const dir = fixture.getFirstDirective()!;

fixture.setHostProps({ component: null });

Expand All @@ -636,7 +643,7 @@ describe('Directive: DynamicDirectives', () => {
ngModule: { imports: [CommonModule] },
});

const dynamicElem = fixture.getComponentElement(DynamicComponent);
const dynamicElem = fixture.getComponentElement(DynamicComponent)!;

expect(dynamicElem).toBeTruthy();
expect(dynamicElem.classes).toEqual({ cls1: true, cls2: true });
Expand All @@ -657,7 +664,7 @@ describe('Directive: DynamicDirectives', () => {
ngModule: { imports: [CommonModule] },
});

const dynamicElem = fixture.getComponentElement(DynamicComponent);
const dynamicElem = fixture.getComponentElement(DynamicComponent)!;

expect(dynamicElem).toBeTruthy();
expect(dynamicElem.classes).toEqual({ cls1: true, cls2: true });
Expand All @@ -682,7 +689,7 @@ describe('Directive: DynamicDirectives', () => {
},
});

const dynamicElem = fixture.getComponentElement(DynamicComponent);
const dynamicElem = fixture.getComponentElement(DynamicComponent)!;

expect(dynamicElem).toBeTruthy();
expect(dynamicElem.classes).toEqual({ cls1: true, cls2: true });
Expand Down
Loading

0 comments on commit fbac529

Please sign in to comment.