1
1
import { SchematicTestRunner } from '@angular-devkit/schematics/testing'
2
2
import { Tree } from '@angular-devkit/schematics'
3
- import { beforeEach , describe , expect , it } from '@jest/globals'
3
+ import { beforeEach , describe } from '@jest/globals'
4
4
import { join } from 'path'
5
5
import { Schema as NgAddSchema } from './schema'
6
- import { Schema as NgNewSchema } from '@schematics/angular/ng-new/schema'
7
- import { LIB_NAME } from '../testing/lib-name'
6
+ import { ProviderTestCase } from './testing/provider-test-case'
7
+ import { shouldAddRootProvider } from './testing/should-add-root-provider'
8
+ import { createTestApp } from '../testing/create-test-app'
8
9
9
10
// https://github.com/angular/components/blob/18.2.8/src/cdk/schematics/ng-add/index.spec.ts
10
11
// https://github.com/angular/components/blob/18.2.8/src/material/schematics/ng-add/index.spec.ts
@@ -24,6 +25,12 @@ describe('ng-add schematic', () => {
24
25
join ( __dirname , '..' , 'collection.json' ) ,
25
26
)
26
27
} )
28
+
29
+ const CORE_PROVIDER = new ProviderTestCase ( {
30
+ name : 'core' ,
31
+ symbol : 'provideNgxMetaCore' ,
32
+ } )
33
+
27
34
; ( [ true , false ] as const ) . forEach ( ( standalone ) => {
28
35
const appKind = standalone ? 'standalone' : 'module-based'
29
36
describe ( `when the app is ${ appKind } ` , ( ) => {
@@ -34,58 +41,19 @@ describe('ng-add schematic', () => {
34
41
} )
35
42
} )
36
43
37
- it ( 'should add core provider' , async ( ) => {
38
- const tree = await runner . runSchematic < NgAddSchema > (
39
- SCHEMATIC_NAME ,
40
- defaultOptions ,
41
- appTree ,
42
- )
43
- const appConfigOrAppModule = getAppConfigOrAppModuleContents (
44
- tree ,
45
- standalone ,
46
- )
47
- expect ( appConfigOrAppModule ) . toContain (
48
- `import { provideNgxMetaCore } from '${ LIB_NAME } /core` ,
49
- )
50
- expect ( stripWhitespace ( appConfigOrAppModule ) ) . toMatch (
51
- / p r o v i d e r s : \[ .* p r o v i d e N g x M e t a C o r e \( \) .* ] / ,
52
- )
44
+ describe ( 'by default' , ( ) => {
45
+ let tree : Tree
46
+
47
+ beforeEach ( async ( ) => {
48
+ tree = await runner . runSchematic < NgAddSchema > (
49
+ SCHEMATIC_NAME ,
50
+ defaultOptions ,
51
+ appTree ,
52
+ )
53
+ } )
54
+
55
+ shouldAddRootProvider ( CORE_PROVIDER , ( ) => tree , standalone )
53
56
} )
54
57
} )
55
58
} )
56
59
} )
57
-
58
- // https://github.com/FortAwesome/angular-fontawesome/blob/0.15.0/projects/schematics/src/ng-add/index.spec.ts#L107
59
- // https://github.com/angular/components/blob/18.2.8/src/cdk/schematics/testing/test-app.ts
60
- const createTestApp = async (
61
- runner : SchematicTestRunner ,
62
- options : Omit < NgNewSchema , 'version' > & Partial < Pick < NgNewSchema , 'version' > > ,
63
- ) => {
64
- return runner . runExternalSchematic < NgNewSchema > (
65
- '@schematics/angular' ,
66
- 'ng-new' ,
67
- {
68
- version : '9.0.0' ,
69
- directory : '.' ,
70
- ...options ,
71
- } ,
72
- )
73
- }
74
-
75
- const getAppConfigOrAppModuleContents = ( tree : Tree , standalone : boolean ) =>
76
- standalone
77
- ? getFileContent ( tree , '/src/app/app.config.ts' )
78
- : getFileContent ( tree , '/src/app/app.module.ts' )
79
-
80
- // https://github.com/angular/components/blob/18.2.8/src/cdk/schematics/testing/file-content.ts
81
- const getFileContent = ( tree : Tree , filePath : string ) : string => {
82
- const contentBuffer = tree . read ( filePath )
83
-
84
- if ( ! contentBuffer ) {
85
- throw new Error ( `Cannot read "${ filePath } " because it does not exist.` )
86
- }
87
-
88
- return contentBuffer . toString ( )
89
- }
90
-
91
- const stripWhitespace = ( value : string ) => value . replace ( / \s / g, '' )
0 commit comments