1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const common_1 = require("@angular/common");
|
4 | const core_1 = require("@angular/core");
|
5 | const platform_browser_1 = require("@angular/platform-browser");
|
6 | const animations_1 = require("@angular/platform-browser/animations");
|
7 | const PropertyExtractor_1 = require("./PropertyExtractor");
|
8 | const test_module_1 = require("../__testfixtures__/test.module");
|
9 | const TEST_TOKEN = new core_1.InjectionToken('testToken');
|
10 | const TestTokenProvider = { provide: TEST_TOKEN, useValue: 123 };
|
11 | const TestService = (0, core_1.Injectable)()(class {
|
12 | });
|
13 | const TestComponent1 = (0, core_1.Component)({})(class {
|
14 | });
|
15 | const TestComponent2 = (0, core_1.Component)({})(class {
|
16 | });
|
17 | const StandaloneTestComponent = (0, core_1.Component)({ standalone: true })(class {
|
18 | });
|
19 | const TestDirective = (0, core_1.Directive)({})(class {
|
20 | });
|
21 | const TestModuleWithDeclarations = (0, core_1.NgModule)({ declarations: [TestComponent1] })(class {
|
22 | });
|
23 | const TestModuleWithImportsAndProviders = (0, core_1.NgModule)({
|
24 | imports: [TestModuleWithDeclarations],
|
25 | providers: [TestTokenProvider],
|
26 | })(class {
|
27 | });
|
28 | const analyzeMetadata = (metadata, component) => {
|
29 | return new PropertyExtractor_1.PropertyExtractor(metadata, component);
|
30 | };
|
31 | const extractImports = (metadata, component) => {
|
32 | const { imports } = new PropertyExtractor_1.PropertyExtractor(metadata, component);
|
33 | return imports;
|
34 | };
|
35 | const extractDeclarations = (metadata, component) => {
|
36 | const { declarations } = new PropertyExtractor_1.PropertyExtractor(metadata, component);
|
37 | return declarations;
|
38 | };
|
39 | const extractProviders = (metadata, component) => {
|
40 | const { providers } = new PropertyExtractor_1.PropertyExtractor(metadata, component);
|
41 | return providers;
|
42 | };
|
43 | const extractApplicationProviders = (metadata, component) => {
|
44 | const { applicationProviders } = new PropertyExtractor_1.PropertyExtractor(metadata, component);
|
45 | return applicationProviders;
|
46 | };
|
47 | describe('PropertyExtractor', () => {
|
48 | jest.spyOn(console, 'warn').mockImplementation(() => { });
|
49 | describe('analyzeMetadata', () => {
|
50 | it('should remove BrowserModule', () => {
|
51 | const metadata = {
|
52 | imports: [platform_browser_1.BrowserModule],
|
53 | };
|
54 | const { imports, providers, applicationProviders } = analyzeMetadata(metadata);
|
55 | expect(imports.flat(Number.MAX_VALUE)).toEqual([common_1.CommonModule]);
|
56 | expect(providers.flat(Number.MAX_VALUE)).toEqual([]);
|
57 | expect(applicationProviders.flat(Number.MAX_VALUE)).toEqual([]);
|
58 | });
|
59 | it('should remove BrowserAnimationsModule and use its providers instead', () => {
|
60 | const metadata = {
|
61 | imports: [animations_1.BrowserAnimationsModule],
|
62 | };
|
63 | const { imports, providers, applicationProviders } = analyzeMetadata(metadata);
|
64 | expect(imports.flat(Number.MAX_VALUE)).toEqual([common_1.CommonModule]);
|
65 | expect(providers.flat(Number.MAX_VALUE)).toEqual([]);
|
66 | expect(applicationProviders.flat(Number.MAX_VALUE)).toEqual((0, animations_1.provideAnimations)());
|
67 | });
|
68 | it('should remove NoopAnimationsModule and use its providers instead', () => {
|
69 | const metadata = {
|
70 | imports: [animations_1.NoopAnimationsModule],
|
71 | };
|
72 | const { imports, providers, applicationProviders } = analyzeMetadata(metadata);
|
73 | expect(imports.flat(Number.MAX_VALUE)).toEqual([common_1.CommonModule]);
|
74 | expect(providers.flat(Number.MAX_VALUE)).toEqual([]);
|
75 | expect(applicationProviders.flat(Number.MAX_VALUE)).toEqual((0, animations_1.provideNoopAnimations)());
|
76 | });
|
77 | it('should remove Browser/Animations modules recursively', () => {
|
78 | const metadata = {
|
79 | imports: [animations_1.BrowserAnimationsModule, platform_browser_1.BrowserModule],
|
80 | };
|
81 | const { imports, providers, applicationProviders } = analyzeMetadata(metadata);
|
82 | expect(imports.flat(Number.MAX_VALUE)).toEqual([common_1.CommonModule]);
|
83 | expect(providers.flat(Number.MAX_VALUE)).toEqual([]);
|
84 | expect(applicationProviders.flat(Number.MAX_VALUE)).toEqual((0, animations_1.provideAnimations)());
|
85 | });
|
86 | it('should not destructure Angular official module', () => {
|
87 | const metadata = {
|
88 | imports: [test_module_1.WithOfficialModule],
|
89 | };
|
90 | const { imports, providers, applicationProviders } = analyzeMetadata(metadata);
|
91 | expect(imports.flat(Number.MAX_VALUE)).toEqual([common_1.CommonModule, test_module_1.WithOfficialModule]);
|
92 | expect(providers.flat(Number.MAX_VALUE)).toEqual([]);
|
93 | expect(applicationProviders.flat(Number.MAX_VALUE)).toEqual([]);
|
94 | });
|
95 | });
|
96 | describe('extractImports', () => {
|
97 | it('should return Angular official modules', () => {
|
98 | const imports = extractImports({ imports: [TestModuleWithImportsAndProviders] });
|
99 | expect(imports).toEqual([common_1.CommonModule, TestModuleWithImportsAndProviders]);
|
100 | });
|
101 | it('should return standalone components', () => {
|
102 | const imports = extractImports({
|
103 | imports: [TestModuleWithImportsAndProviders],
|
104 | }, StandaloneTestComponent);
|
105 | expect(imports).toEqual([
|
106 | common_1.CommonModule,
|
107 | TestModuleWithImportsAndProviders,
|
108 | StandaloneTestComponent,
|
109 | ]);
|
110 | });
|
111 | });
|
112 | describe('extractDeclarations', () => {
|
113 | it('should return an array of declarations that contains `storyComponent`', () => {
|
114 | const declarations = extractDeclarations({ declarations: [TestComponent1] }, TestComponent2);
|
115 | expect(declarations).toEqual([TestComponent1, TestComponent2]);
|
116 | });
|
117 | });
|
118 | describe('analyzeDecorators', () => {
|
119 | it('isStandalone should be false', () => {
|
120 | const { isStandalone } = PropertyExtractor_1.PropertyExtractor.analyzeDecorators(TestComponent1);
|
121 | expect(isStandalone).toBe(false);
|
122 | });
|
123 | it('isStandalone should be true', () => {
|
124 | const { isStandalone } = PropertyExtractor_1.PropertyExtractor.analyzeDecorators(StandaloneTestComponent);
|
125 | expect(isStandalone).toBe(true);
|
126 | });
|
127 | });
|
128 | describe('extractProviders', () => {
|
129 | it('should return an array of providers', () => {
|
130 | const providers = extractProviders({
|
131 | providers: [TestService],
|
132 | });
|
133 | expect(providers).toEqual([TestService]);
|
134 | });
|
135 | it('should return an array of singletons extracted', () => {
|
136 | const singeltons = extractApplicationProviders({
|
137 | imports: [animations_1.BrowserAnimationsModule],
|
138 | });
|
139 | expect(singeltons).toEqual((0, animations_1.provideAnimations)());
|
140 | });
|
141 | });
|
142 | });
|