Skip to content

Commit c6bebc8

Browse files
authored
Merge pull request #2 from orchestratora/fix-module-config
[Fix] Accept schemas from ngModule config
2 parents 02890cd + 57df863 commit c6bebc8

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

.vscode/settings.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"typescript.tsdk": "node_modules\\typescript\\lib"
2+
"typescript.tsdk": "node_modules\\typescript\\lib",
3+
"workbench.colorCustomizations": {
4+
"activityBar.background": "#1D2F40",
5+
"titleBar.activeBackground": "#29425A",
6+
"titleBar.activeForeground": "#F8FAFC"
7+
}
38
}

projects/ngx-testing/src/lib/host-generator.service.spec.ts

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { ComponentFactoryResolver } from '@angular/core';
2+
import * as ngCore from '@angular/core';
23
import { TestBed } from '@angular/core/testing';
34

5+
import { spyOnModule } from '../../test/spy-on-module';
46
import { HostGeneratorService } from './host-generator.service';
7+
import * as tplGen from './template-gen';
58
import {
69
ExtraConfigToken,
710
TestModuleToken,
@@ -10,7 +13,6 @@ import {
1013
} from './tokens';
1114
import { TestTypeKind } from './types';
1215
import * as util from './util';
13-
import * as tplGen from './template-gen';
1416

1517
class TestModuleTokenMock {}
1618
class TestTypeTokenMock {}
@@ -33,11 +35,74 @@ describe('Service: HostGenerator', () => {
3335
});
3436

3537
describe('generateModuleFor() method', () => {
38+
let ngModule: jasmine.Spy;
39+
40+
beforeEach(() => {
41+
getService();
42+
ngModule = spyOnModule(ngCore, 'NgModule');
43+
});
44+
3645
it('should return new type', () => {
3746
expect(getService().generateModuleFor(class {})).toEqual(
3847
jasmine.any(Function),
3948
);
4049
});
50+
51+
it('should import `this.testModule`', () => {
52+
getService().generateModuleFor(class {});
53+
54+
expect(ngModule).toHaveBeenCalledWith(
55+
jasmine.objectContaining({
56+
imports: [TestModuleTokenMock],
57+
}),
58+
);
59+
});
60+
61+
it('should declare `host`', () => {
62+
const host = class {};
63+
getService().generateModuleFor(host);
64+
65+
expect(ngModule).toHaveBeenCalledWith(
66+
jasmine.objectContaining({
67+
declarations: [host],
68+
}),
69+
);
70+
});
71+
72+
it('should export `host`', () => {
73+
const host = class {};
74+
getService().generateModuleFor(host);
75+
76+
expect(ngModule).toHaveBeenCalledWith(
77+
jasmine.objectContaining({
78+
exports: [host],
79+
}),
80+
);
81+
});
82+
83+
it('should set `host` as entryComponents', () => {
84+
const host = class {};
85+
getService().generateModuleFor(host);
86+
87+
expect(ngModule).toHaveBeenCalledWith(
88+
jasmine.objectContaining({
89+
entryComponents: [host],
90+
}),
91+
);
92+
});
93+
94+
it('should set schemas from `this.extraConfig.ngModule.schemas`', () => {
95+
const extraConfig = TestBed.get(ExtraConfigToken);
96+
extraConfig.ngModule = { schemas: 'schema' };
97+
98+
getService().generateModuleFor(class {});
99+
100+
expect(ngModule).toHaveBeenCalledWith(
101+
jasmine.objectContaining({
102+
schemas: 'schema',
103+
}),
104+
);
105+
});
41106
});
42107

43108
describe('generate() method', () => {

projects/ngx-testing/src/lib/host-generator.service.ts

+4
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ export class HostGeneratorService {
4343

4444
generateModuleFor(host: Type<any>): Type<any> {
4545
const testModule = this.testModule;
46+
const ngModule = this.extraConfig.ngModule;
47+
4648
@NgModule({
4749
imports: [testModule],
4850
declarations: [host],
4951
exports: [host],
5052
entryComponents: [host],
53+
schemas: ngModule ? ngModule.schemas : [],
5154
})
5255
class TestModule {}
56+
5357
return TestModule;
5458
}
5559

projects/ngx-testing/src/lib/ngx-testing.module.ts

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function getTestingModuleFor<T>(
6868
entryComponents: entryType
6969
? mergeArrays(extra.entryComponents, [entryType])
7070
: extra.entryComponents,
71+
schemas: extra.schemas,
7172
})
7273
class TestingModule<D> {}
7374
return TestingModule as Type<TestingModule<T>>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function spyOnModule<T extends object>(
2+
moduleObj: T,
3+
exportSymbolName: keyof T,
4+
): jasmine.Spy {
5+
const spy = jasmine.createSpy(`Spy of ${exportSymbolName}`);
6+
spyOnProperty(moduleObj, exportSymbolName).and.returnValue(spy);
7+
return spy;
8+
}

0 commit comments

Comments
 (0)