|
| 1 | +import { Tree } from '@angular-devkit/schematics'; |
| 2 | +import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { firstValueFrom } from 'rxjs'; |
| 5 | + |
| 6 | +import { |
| 7 | + DEFAULT_MAGENTO_VERSION, |
| 8 | + addBuildCondition, |
| 9 | + createDaffJson, |
| 10 | +} from './daff-config'; |
| 11 | +import { NgAddOptions } from '../../schema'; |
| 12 | + |
| 13 | +const collectionPath = path.join(__dirname, '../../../collection.json'); |
| 14 | + |
| 15 | +const buildAngularJson = (projectName: string) => ({ |
| 16 | + version: 1, |
| 17 | + newProjectRoot: 'projects', |
| 18 | + projects: { |
| 19 | + [projectName]: { |
| 20 | + projectType: 'application', |
| 21 | + root: `projects/${projectName}`, |
| 22 | + sourceRoot: `projects/${projectName}/src`, |
| 23 | + architect: { |
| 24 | + build: { |
| 25 | + builder: '@angular-devkit/build-angular:application', |
| 26 | + options: {}, |
| 27 | + configurations: { |
| 28 | + production: {}, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | +}); |
| 35 | + |
| 36 | +describe('createDaffJson', () => { |
| 37 | + let tree: Tree; |
| 38 | + const projectName = 'test-app'; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + tree = Tree.empty(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('creates daff.json with the default magento version when driver is magento', () => { |
| 45 | + const options: NgAddOptions = { project: projectName, driver: 'magento' }; |
| 46 | + const rule = createDaffJson(options, projectName); |
| 47 | + |
| 48 | + rule(tree, <any>{ logger: { warn: () => undefined }}); |
| 49 | + |
| 50 | + expect(tree.exists('daff.json')).toBe(true); |
| 51 | + const body = JSON.parse(tree.read('daff.json')?.toString() ?? ''); |
| 52 | + expect(body.projects[projectName].drivers.magento).toBe(DEFAULT_MAGENTO_VERSION); |
| 53 | + }); |
| 54 | + |
| 55 | + it('creates daff.json for the demo driver', () => { |
| 56 | + const options: NgAddOptions = { project: projectName, driver: 'demo' }; |
| 57 | + const rule = createDaffJson(options, projectName); |
| 58 | + |
| 59 | + rule(tree, <any>{ logger: { warn: () => undefined }}); |
| 60 | + |
| 61 | + expect(tree.exists('daff.json')).toBe(true); |
| 62 | + const body = JSON.parse(tree.read('daff.json')?.toString() ?? ''); |
| 63 | + expect(body.projects[projectName].drivers.magento).toBe(DEFAULT_MAGENTO_VERSION); |
| 64 | + }); |
| 65 | + |
| 66 | + it('does not create daff.json for shopify or in-memory drivers', () => { |
| 67 | + for (const driver of <const>['shopify', 'in-memory']) { |
| 68 | + const scopedTree = Tree.empty(); |
| 69 | + const options: NgAddOptions = { project: projectName, driver }; |
| 70 | + const rule = createDaffJson(options, projectName); |
| 71 | + |
| 72 | + rule(scopedTree, <any>{ logger: { warn: () => undefined }}); |
| 73 | + |
| 74 | + expect(scopedTree.exists('daff.json')).toBe(false); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + it('leaves an existing daff.json untouched', () => { |
| 79 | + const existing = '{"projects":{"other":{"drivers":{"magento":"2.4.1"}}}}\n'; |
| 80 | + tree.create('daff.json', existing); |
| 81 | + const options: NgAddOptions = { project: projectName, driver: 'magento' }; |
| 82 | + const rule = createDaffJson(options, projectName); |
| 83 | + |
| 84 | + rule(tree, <any>{ logger: { warn: () => undefined }}); |
| 85 | + |
| 86 | + expect(tree.read('daff.json')?.toString()).toBe(existing); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('addBuildCondition', () => { |
| 91 | + const projectName = 'test-app'; |
| 92 | + let runner: SchematicTestRunner; |
| 93 | + let tree: Tree; |
| 94 | + |
| 95 | + beforeEach(() => { |
| 96 | + runner = new SchematicTestRunner('schematics', collectionPath); |
| 97 | + tree = Tree.empty(); |
| 98 | + tree.create('/angular.json', JSON.stringify(buildAngularJson(projectName), null, 2)); |
| 99 | + }); |
| 100 | + |
| 101 | + it('adds the magento condition to the production build configuration', async () => { |
| 102 | + const options: NgAddOptions = { project: projectName, driver: 'magento' }; |
| 103 | + const rule = addBuildCondition(options, projectName); |
| 104 | + |
| 105 | + const resultTree = await firstValueFrom(runner.callRule(rule, tree)); |
| 106 | + |
| 107 | + const angular = JSON.parse(resultTree.read('/angular.json')?.toString() ?? ''); |
| 108 | + const conditions = angular.projects[projectName].architect.build.configurations.production.conditions; |
| 109 | + expect(conditions).toEqual([`magento-${DEFAULT_MAGENTO_VERSION}`]); |
| 110 | + }); |
| 111 | + |
| 112 | + it('adds the magento condition for the demo driver', async () => { |
| 113 | + const options: NgAddOptions = { project: projectName, driver: 'demo' }; |
| 114 | + const rule = addBuildCondition(options, projectName); |
| 115 | + |
| 116 | + const resultTree = await firstValueFrom(runner.callRule(rule, tree)); |
| 117 | + |
| 118 | + const angular = JSON.parse(resultTree.read('/angular.json')?.toString() ?? ''); |
| 119 | + const conditions = angular.projects[projectName].architect.build.configurations.production.conditions; |
| 120 | + expect(conditions).toEqual([`magento-${DEFAULT_MAGENTO_VERSION}`]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('leaves conditions untouched for shopify or in-memory drivers', async () => { |
| 124 | + for (const driver of <const>['shopify', 'in-memory']) { |
| 125 | + const scopedTree = Tree.empty(); |
| 126 | + scopedTree.create('/angular.json', JSON.stringify(buildAngularJson(projectName), null, 2)); |
| 127 | + const options: NgAddOptions = { project: projectName, driver }; |
| 128 | + const rule = addBuildCondition(options, projectName); |
| 129 | + |
| 130 | + const resultTree = await firstValueFrom(runner.callRule(rule, scopedTree)); |
| 131 | + |
| 132 | + const angular = JSON.parse(resultTree.read('/angular.json')?.toString() ?? ''); |
| 133 | + expect(angular.projects[projectName].architect.build.configurations.production.conditions).toBeUndefined(); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + it('deduplicates when the condition is already present', async () => { |
| 138 | + const baseline = buildAngularJson(projectName); |
| 139 | + (<any>baseline.projects[projectName].architect.build.configurations.production).conditions = [ |
| 140 | + `magento-${DEFAULT_MAGENTO_VERSION}`, |
| 141 | + ]; |
| 142 | + tree.overwrite('/angular.json', JSON.stringify(baseline, null, 2)); |
| 143 | + |
| 144 | + const options: NgAddOptions = { project: projectName, driver: 'magento' }; |
| 145 | + const rule = addBuildCondition(options, projectName); |
| 146 | + |
| 147 | + const resultTree = await firstValueFrom(runner.callRule(rule, tree)); |
| 148 | + |
| 149 | + const angular = JSON.parse(resultTree.read('/angular.json')?.toString() ?? ''); |
| 150 | + const conditions = angular.projects[projectName].architect.build.configurations.production.conditions; |
| 151 | + expect(conditions).toEqual([`magento-${DEFAULT_MAGENTO_VERSION}`]); |
| 152 | + }); |
| 153 | +}); |
0 commit comments