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