|
| 1 | +import { Tree } from '@angular-devkit/schematics'; |
| 2 | +import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; |
| 3 | +import { NxJson, readJsonInTree } from '@nrwl/workspace'; |
| 4 | +import { createEmptyWorkspace } from '@nrwl/workspace/testing'; |
| 5 | +import { join } from 'path'; |
| 6 | +import { UtilOptions } from './schema'; |
| 7 | + |
| 8 | +describe('util', () => { |
| 9 | + let appTree: Tree; |
| 10 | + |
| 11 | + const testRunner = new SchematicTestRunner( |
| 12 | + '@angular-architects/ddd', |
| 13 | + join(__dirname, '../../../collection.json') |
| 14 | + ); |
| 15 | + |
| 16 | + function runSchematic<SchemaOptions = any>( |
| 17 | + schematicName: string, |
| 18 | + options: SchemaOptions, |
| 19 | + tree: Tree |
| 20 | + ) { |
| 21 | + return testRunner |
| 22 | + .runSchematicAsync(schematicName, options, tree) |
| 23 | + .toPromise(); |
| 24 | + } |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + appTree = Tree.empty(); |
| 28 | + appTree = createEmptyWorkspace(appTree); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should add correct tags if util lib is shared', async () => { |
| 32 | + const tree = await runSchematic<UtilOptions>( |
| 33 | + 'util', |
| 34 | + { name: 'form-validation', shared: true }, |
| 35 | + appTree |
| 36 | + ); |
| 37 | + |
| 38 | + const nxJson = readJsonInTree<NxJson>(tree, '/nx.json'); |
| 39 | + expect(nxJson.projects).toEqual({ |
| 40 | + 'shared-util-form-validation': { |
| 41 | + tags: ['domain:shared', 'type:util'], |
| 42 | + }, |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should add correct tags if util lib belongs to a domain', async () => { |
| 47 | + const tree = await runSchematic<UtilOptions>( |
| 48 | + 'util', |
| 49 | + { name: 'form-validation', domain: 'customer' }, |
| 50 | + appTree |
| 51 | + ); |
| 52 | + |
| 53 | + const nxJson = readJsonInTree<NxJson>(tree, '/nx.json'); |
| 54 | + expect(nxJson.projects).toEqual({ |
| 55 | + 'customer-util-form-validation': { |
| 56 | + tags: ['domain:customer', 'type:util'], |
| 57 | + }, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should throw error if neither domain nor shared option is provided', async () => { |
| 62 | + const schematicFunc = async () => |
| 63 | + await runSchematic<UtilOptions>('util', { name: 'form-validation' }, appTree); |
| 64 | + await expect(schematicFunc()).rejects.toThrowError(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should throw error if domain and shared option is provided', async () => { |
| 68 | + const schematicFunc = async () => |
| 69 | + await runSchematic<UtilOptions>( |
| 70 | + 'util', |
| 71 | + { name: 'form-validation', domain: 'customer', shared: true }, |
| 72 | + appTree |
| 73 | + ); |
| 74 | + await expect(schematicFunc()).rejects.toThrowError(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should be able to customize the directory of the library within the domain / shared folder', async () => { |
| 78 | + const tree = await runSchematic<UtilOptions>( |
| 79 | + 'util', |
| 80 | + { name: 'form-validation', domain: 'customer', directory: 'forms' }, |
| 81 | + appTree |
| 82 | + ); |
| 83 | + |
| 84 | + const workspaceJson = readJsonInTree(tree, '/workspace.json'); |
| 85 | + expect(workspaceJson.projects).toHaveProperty( |
| 86 | + 'customer-forms-util-form-validation' |
| 87 | + ); |
| 88 | + expect( |
| 89 | + workspaceJson.projects['customer-forms-util-form-validation'].root |
| 90 | + ).toEqual('libs/customer/forms/util-form-validation'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should keep correct tags with a customized directory', async () => { |
| 94 | + const tree = await runSchematic<UtilOptions>( |
| 95 | + 'util', |
| 96 | + { name: 'form-validation', domain: 'customer', directory: 'forms' }, |
| 97 | + appTree |
| 98 | + ); |
| 99 | + |
| 100 | + const nxJson = readJsonInTree<NxJson>(tree, '/nx.json'); |
| 101 | + expect(nxJson.projects).toEqual({ |
| 102 | + 'customer-forms-util-form-validation': { |
| 103 | + tags: ['domain:customer', 'type:util'], |
| 104 | + }, |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should add valid import path to publishable lib', async () => { |
| 109 | + const tree = await runSchematic<UtilOptions>( |
| 110 | + 'util', |
| 111 | + { name: 'form-validation', shared: true, type: 'publishable' }, |
| 112 | + appTree |
| 113 | + ); |
| 114 | + |
| 115 | + let ngPackage = readJsonInTree( |
| 116 | + tree, |
| 117 | + 'libs/shared/util-form-validation/ng-package.json' |
| 118 | + ); |
| 119 | + expect(ngPackage).toBeDefined(); |
| 120 | + const packageJson = readJsonInTree( |
| 121 | + tree, |
| 122 | + 'libs/shared/util-form-validation/package.json' |
| 123 | + ); |
| 124 | + expect(packageJson.name).toEqual('@proj/shared-util-form-validation'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should add valid import path to publishable lib with customized directory', async () => { |
| 128 | + const tree = await runSchematic<UtilOptions>( |
| 129 | + 'util', |
| 130 | + { |
| 131 | + name: 'form-validation', |
| 132 | + shared: true, |
| 133 | + type: 'publishable', |
| 134 | + directory: 'forms', |
| 135 | + }, |
| 136 | + appTree |
| 137 | + ); |
| 138 | + |
| 139 | + let ngPackage = readJsonInTree( |
| 140 | + tree, |
| 141 | + 'libs/shared/forms/util-form-validation/ng-package.json' |
| 142 | + ); |
| 143 | + expect(ngPackage).toBeDefined(); |
| 144 | + const packageJson = readJsonInTree( |
| 145 | + tree, |
| 146 | + 'libs/shared/forms/util-form-validation/package.json' |
| 147 | + ); |
| 148 | + expect(packageJson.name).toEqual('@proj/shared-forms-util-form-validation'); |
| 149 | + }); |
| 150 | +}); |
0 commit comments