|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 10 | +import { Schema as LibraryOptions } from '../library/schema'; |
| 11 | +import { Schema as WorkspaceOptions } from '../workspace/schema'; |
| 12 | +import { Schema as GenerateLibrarySchema } from './schema'; |
| 13 | +import { parse as parseJson } from 'jsonc-parser'; |
| 14 | + |
| 15 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 16 | +function getJsonFileContent(tree: UnitTestTree, path: string): any { |
| 17 | + return parseJson(tree.readContent(path).toString()); |
| 18 | +} |
| 19 | + |
| 20 | +describe('Secondary Entrypoint Schematic', () => { |
| 21 | + const schematicRunner = new SchematicTestRunner( |
| 22 | + '@schematics/ng_packagr', |
| 23 | + require.resolve('../collection.json'), |
| 24 | + ); |
| 25 | + const defaultOptions: GenerateLibrarySchema = { |
| 26 | + name: 'foo-secondary', |
| 27 | + project: 'foo', |
| 28 | + }; |
| 29 | + |
| 30 | + const workspaceOptions: WorkspaceOptions = { |
| 31 | + name: 'workspace', |
| 32 | + newProjectRoot: 'projects', |
| 33 | + |
| 34 | + version: '6.0.0', |
| 35 | + }; |
| 36 | + const libaryOptions: LibraryOptions = { |
| 37 | + name: 'foo', |
| 38 | + entryFile: 'my-index', |
| 39 | + standalone: true, |
| 40 | + skipPackageJson: false, |
| 41 | + skipTsConfig: false, |
| 42 | + skipInstall: false, |
| 43 | + }; |
| 44 | + |
| 45 | + let workspaceTree: UnitTestTree; |
| 46 | + beforeEach(async () => { |
| 47 | + workspaceTree = await schematicRunner.runSchematic('workspace', workspaceOptions); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should create correct files', async () => { |
| 51 | + workspaceTree = await schematicRunner.runSchematic('library', libaryOptions, workspaceTree); |
| 52 | + const tree = await schematicRunner.runSchematic( |
| 53 | + 'secondary', |
| 54 | + { ...defaultOptions }, |
| 55 | + workspaceTree, |
| 56 | + ); |
| 57 | + const files = tree.files; |
| 58 | + |
| 59 | + expect(files).toEqual( |
| 60 | + jasmine.arrayContaining([ |
| 61 | + '/projects/foo/src/lib/foo-secondary/README.md', |
| 62 | + '/projects/foo/src/lib/foo-secondary/ng-package.json', |
| 63 | + '/projects/foo/src/lib/foo-secondary/src/public-api.ts', |
| 64 | + ]), |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should set correct main and secondary entrypoints in the README', async () => { |
| 69 | + workspaceTree = await schematicRunner.runSchematic('library', libaryOptions, workspaceTree); |
| 70 | + const tree = await schematicRunner.runSchematic( |
| 71 | + 'secondary', |
| 72 | + { ...defaultOptions }, |
| 73 | + workspaceTree, |
| 74 | + ); |
| 75 | + const content = tree.readContent('/projects/foo/src/lib/foo-secondary/README.md'); |
| 76 | + expect(content).toMatch('# foo/foo-secondary'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should set a custom entryfile', async () => { |
| 80 | + workspaceTree = await schematicRunner.runSchematic('library', libaryOptions, workspaceTree); |
| 81 | + const tree = await schematicRunner.runSchematic( |
| 82 | + 'secondary', |
| 83 | + { ...defaultOptions, entryFile: 'my-index' }, |
| 84 | + workspaceTree, |
| 85 | + ); |
| 86 | + const files = tree.files; |
| 87 | + expect(files).toEqual( |
| 88 | + jasmine.arrayContaining([ |
| 89 | + '/projects/foo/src/lib/foo-secondary/README.md', |
| 90 | + '/projects/foo/src/lib/foo-secondary/ng-package.json', |
| 91 | + '/projects/foo/src/lib/foo-secondary/src/my-index.ts', |
| 92 | + ]), |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should handle scope packages', async () => { |
| 97 | + workspaceTree = await schematicRunner.runSchematic( |
| 98 | + 'library', |
| 99 | + { ...libaryOptions, name: '@scope/package' }, |
| 100 | + workspaceTree, |
| 101 | + ); |
| 102 | + const tree = await schematicRunner.runSchematic( |
| 103 | + 'secondary', |
| 104 | + { ...defaultOptions, name: 'testing', project: '@scope/package' }, |
| 105 | + workspaceTree, |
| 106 | + ); |
| 107 | + const files = tree.files; |
| 108 | + expect(files).toEqual( |
| 109 | + jasmine.arrayContaining([ |
| 110 | + '/projects/scope/package/src/lib/testing/README.md', |
| 111 | + '/projects/scope/package/src/lib/testing/ng-package.json', |
| 112 | + '/projects/scope/package/src/lib/testing/src/public-api.ts', |
| 113 | + ]), |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it(`should add paths mapping to the tsconfig`, async () => { |
| 118 | + workspaceTree = await schematicRunner.runSchematic( |
| 119 | + 'library', |
| 120 | + { ...libaryOptions, name: '@scope/package' }, |
| 121 | + workspaceTree, |
| 122 | + ); |
| 123 | + const tree = await schematicRunner.runSchematic( |
| 124 | + 'secondary', |
| 125 | + { ...defaultOptions, name: 'testing', project: '@scope/package' }, |
| 126 | + workspaceTree, |
| 127 | + ); |
| 128 | + |
| 129 | + const tsConfigJson = getJsonFileContent(tree, 'tsconfig.json'); |
| 130 | + expect(tsConfigJson.compilerOptions.paths['@scope/package/testing']).toEqual([ |
| 131 | + './dist/scope/package/testing', |
| 132 | + ]); |
| 133 | + }); |
| 134 | + |
| 135 | + it(`should append to existing paths mappings`, async () => { |
| 136 | + workspaceTree = await schematicRunner.runSchematic( |
| 137 | + 'library', |
| 138 | + { ...libaryOptions, name: '@scope/package' }, |
| 139 | + workspaceTree, |
| 140 | + ); |
| 141 | + workspaceTree.overwrite( |
| 142 | + 'tsconfig.json', |
| 143 | + JSON.stringify({ |
| 144 | + compilerOptions: { |
| 145 | + paths: { |
| 146 | + 'unrelated': ['./something/else.ts'], |
| 147 | + '@scope/package/testing': ['libs/*'], |
| 148 | + }, |
| 149 | + }, |
| 150 | + }), |
| 151 | + ); |
| 152 | + const tree = await schematicRunner.runSchematic( |
| 153 | + 'secondary', |
| 154 | + { ...defaultOptions, name: 'testing', project: '@scope/package' }, |
| 155 | + workspaceTree, |
| 156 | + ); |
| 157 | + |
| 158 | + const tsConfigJson = getJsonFileContent(tree, 'tsconfig.json'); |
| 159 | + expect(tsConfigJson.compilerOptions.paths['@scope/package/testing']).toEqual([ |
| 160 | + 'libs/*', |
| 161 | + './dist/scope/package/testing', |
| 162 | + ]); |
| 163 | + }); |
| 164 | +}); |
0 commit comments