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