Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import angularQueryPackage from '../package.json' with { type: 'json' }
3import {
4 SchematicTestRunner,
5 angularCollection,
6 devtoolsPackage,
7 queryCollection,
8} from './test-utils'
9β
10const angularRunner = new SchematicTestRunner(
11 '@schematics/angular',
12 angularCollection,
13)
14const queryRunner = new SchematicTestRunner(
15 angularQueryPackage.name,
16 queryCollection,
17)
18β
19async function createAngularApplication(standalone = true) {
20 let tree = await angularRunner.runSchematic('workspace', {
21 name: 'workspace',
22 version: '20.0.0',
23 newProjectRoot: 'projects',
24 })
25β
26 tree = await angularRunner.runSchematic(
27 'application',
28 {
29 name: 'test-app',
30 standalone,
31 routing: false,
32 style: 'css',
33 skipInstall: true,
34 },
35 tree,
36 )
37β
38 return tree
39}
40β
41describe('ng-add schematic', () => {
42 it('configures Angular Query and devtools', async () => {
43 const tree = await createAngularApplication()
44 const result = await queryRunner.runSchematic(
45 'ng-add',
46 { project: 'test-app' },
47 tree,
48 )
49 const packageJson = JSON.parse(result.readContent('/package.json')) as {
50 dependencies: Record<string, string>
51 }
52 const devtoolsPackageName = Object.keys(packageJson.dependencies).find(
53 (packageName) => packageName.endsWith('/angular-query-devtools'),
54 )
55 const appConfig = result.readContent(
56 '/projects/test-app/src/app/app.config.ts',
57 )
58β
59 expect(devtoolsPackageName).toBeDefined()
60 expect(appConfig).toContain(`from '${angularQueryPackage.name}'`)
61 expect(appConfig).toContain(`from '${devtoolsPackageName}'`)
62 expect(appConfig).toContain(
63 'provideTanStackQuery(() => new QueryClient(), withDevtools())',
64 )
65 expect(queryRunner.tasks).toStrictEqual([
66 expect.objectContaining({ name: 'node-package' }),
67 ])
68 })
69β
70 it('adds the devtools with a caret range so they update with Angular Query', async () => {
71 const tree = await createAngularApplication()
72 const result = await queryRunner.runSchematic(
73 'ng-add',
74 { project: 'test-app' },
75 tree,
76 )
77 const packageJson = JSON.parse(result.readContent('/package.json')) as {
78 dependencies: Record<string, string>
79 }
80β
81 expect(packageJson.dependencies[devtoolsPackage.name]).toBe(
82 `^${devtoolsPackage.version}`,
83 )
84 })
85β
86 it.each([
87 { applicationType: 'standalone', standalone: true },
88 { applicationType: 'NgModule', standalone: false },
89 ])(
90 'does not duplicate configuration in a $applicationType application',
91 async ({ standalone }) => {
92 const tree = await createAngularApplication(standalone)
93 const firstResult = await queryRunner.runSchematic(
94 'ng-add',
95 { project: 'test-app' },
96 tree,
97 )
98 const secondResult = await queryRunner.runSchematic(
99 'ng-add',
100 { project: 'test-app' },
101 firstResult,
102 )
103 const applicationSources = secondResult.files
104 .filter((filePath) => filePath.endsWith('.ts'))
105 .map((filePath) => secondResult.readContent(filePath))
106 .join('\n')
107β
108 expect(applicationSources.match(/provideTanStackQuery\(/g)).toHaveLength(
109 1,
110 )
111 expect(applicationSources.match(/withDevtools\(\)/g)).toHaveLength(1)
112 expect(queryRunner.tasks).toHaveLength(0)
113 },
114 )
115})