|
| 1 | +import { |
| 2 | + SchematicTestRunner, |
| 3 | + UnitTestTree, |
| 4 | +} from '@angular-devkit/schematics/testing'; |
| 5 | +import * as path from 'path'; |
| 6 | +import { Schema as RouterStoreOptions } from './schema'; |
| 7 | +import { |
| 8 | + getTestProjectPath, |
| 9 | + createWorkspace, |
| 10 | +} from '../../../schematics-core/testing'; |
| 11 | + |
| 12 | +describe('Router Store ng-add Schematic', () => { |
| 13 | + const schematicRunner = new SchematicTestRunner( |
| 14 | + '@ngrx/router-store', |
| 15 | + path.join(__dirname, '../collection.json') |
| 16 | + ); |
| 17 | + const defaultOptions: RouterStoreOptions = { |
| 18 | + skipPackageJson: false, |
| 19 | + module: 'app', |
| 20 | + }; |
| 21 | + |
| 22 | + const projectPath = getTestProjectPath(); |
| 23 | + |
| 24 | + let appTree: UnitTestTree; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + appTree = createWorkspace(schematicRunner, appTree); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should update package.json', () => { |
| 31 | + const options = { ...defaultOptions }; |
| 32 | + |
| 33 | + const tree = schematicRunner.runSchematic('ng-add', options, appTree); |
| 34 | + const packageJson = JSON.parse(tree.readContent('/package.json')); |
| 35 | + |
| 36 | + expect(packageJson.dependencies['@ngrx/router-store']).toBeDefined(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should skip package.json update', () => { |
| 40 | + const options = { ...defaultOptions, skipPackageJson: true }; |
| 41 | + |
| 42 | + const tree = schematicRunner.runSchematic('ng-add', options, appTree); |
| 43 | + const packageJson = JSON.parse(tree.readContent('/package.json')); |
| 44 | + |
| 45 | + expect(packageJson.dependencies['@ngrx/router-store']).toBeUndefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should be provided by default', () => { |
| 49 | + const options = { ...defaultOptions }; |
| 50 | + |
| 51 | + const tree = schematicRunner.runSchematic('ng-add', options, appTree); |
| 52 | + const content = tree.readContent(`${projectPath}/src/app/app.module.ts`); |
| 53 | + expect(content).toMatch( |
| 54 | + /import { StoreRouterConnectingModule } from '@ngrx\/router-store';/ |
| 55 | + ); |
| 56 | + expect(content).toMatch(/StoreRouterConnectingModule.forRoot\(\)/); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should import into a specified module', () => { |
| 60 | + const options = { ...defaultOptions }; |
| 61 | + |
| 62 | + const tree = schematicRunner.runSchematic('ng-add', options, appTree); |
| 63 | + const content = tree.readContent(`${projectPath}/src/app/app.module.ts`); |
| 64 | + expect(content).toMatch( |
| 65 | + /import { StoreRouterConnectingModule } from '@ngrx\/router-store';/ |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should fail if specified module does not exist', () => { |
| 70 | + const options = { ...defaultOptions, module: '/src/app/app.moduleXXX.ts' }; |
| 71 | + let thrownError: Error | null = null; |
| 72 | + try { |
| 73 | + schematicRunner.runSchematic('ng-add', options, appTree); |
| 74 | + } catch (err) { |
| 75 | + thrownError = err; |
| 76 | + } |
| 77 | + expect(thrownError).toBeDefined(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments