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