|
| 1 | +import { |
| 2 | + SchematicTestRunner, |
| 3 | + UnitTestTree, |
| 4 | +} from '@angular-devkit/schematics/testing'; |
| 5 | +import * as path from 'path'; |
| 6 | +import { Schema as DataOptions } from './schema'; |
| 7 | +import { |
| 8 | + getTestProjectPath, |
| 9 | + createWorkspace, |
| 10 | + defaultWorkspaceOptions, |
| 11 | + defaultAppOptions, |
| 12 | +} from '../../../schematics-core/testing'; |
| 13 | + |
| 14 | +describe('Data Schematic', () => { |
| 15 | + const schematicRunner = new SchematicTestRunner( |
| 16 | + '@ngrx/schematics', |
| 17 | + path.join(__dirname, '../../collection.json') |
| 18 | + ); |
| 19 | + const defaultOptions: DataOptions = { |
| 20 | + name: 'foo', |
| 21 | + project: 'bar', |
| 22 | + spec: false, |
| 23 | + group: false, |
| 24 | + flat: true, |
| 25 | + }; |
| 26 | + |
| 27 | + const projectPath = getTestProjectPath(); |
| 28 | + |
| 29 | + let appTree: UnitTestTree; |
| 30 | + |
| 31 | + beforeEach(async () => { |
| 32 | + appTree = await createWorkspace(schematicRunner, appTree); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should create the data service to a specified project if provided', () => { |
| 36 | + const options = { ...defaultOptions, project: 'baz' }; |
| 37 | + |
| 38 | + const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, { |
| 39 | + ...defaultAppOptions, |
| 40 | + name: 'baz', |
| 41 | + }); |
| 42 | + |
| 43 | + const tree = schematicRunner.runSchematic('data', options, appTree); |
| 44 | + const files = tree.files; |
| 45 | + expect( |
| 46 | + files.indexOf(`${specifiedProjectPath}/src/lib/foo.service.ts`) |
| 47 | + ).toBeGreaterThanOrEqual(0); |
| 48 | + expect( |
| 49 | + files.indexOf(`${specifiedProjectPath}/src/lib/foo.ts`) |
| 50 | + ).toBeGreaterThanOrEqual(0); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should create the service and model files', () => { |
| 54 | + const tree = schematicRunner.runSchematic('data', defaultOptions, appTree); |
| 55 | + expect( |
| 56 | + tree.files.indexOf(`${projectPath}/src/app/foo.service.ts`) |
| 57 | + ).toBeGreaterThanOrEqual(0); |
| 58 | + expect( |
| 59 | + tree.files.indexOf(`${projectPath}/src/app/foo.ts`) |
| 60 | + ).toBeGreaterThanOrEqual(0); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should create two files if spec is true', () => { |
| 64 | + const options = { |
| 65 | + ...defaultOptions, |
| 66 | + spec: true, |
| 67 | + }; |
| 68 | + const tree = schematicRunner.runSchematic('data', options, appTree); |
| 69 | + expect( |
| 70 | + tree.files.indexOf(`${projectPath}/src/app/foo.service.spec.ts`) |
| 71 | + ).toBeGreaterThanOrEqual(0); |
| 72 | + expect( |
| 73 | + tree.files.indexOf(`${projectPath}/src/app/foo.service.ts`) |
| 74 | + ).toBeGreaterThanOrEqual(0); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('service class', () => { |
| 78 | + it('should import the correct model', () => { |
| 79 | + const options = { ...defaultOptions, spec: true }; |
| 80 | + const tree = schematicRunner.runSchematic('data', options, appTree); |
| 81 | + const fileContent = tree.readContent( |
| 82 | + `${projectPath}/src/app/foo.service.ts` |
| 83 | + ); |
| 84 | + |
| 85 | + expect(fileContent).toMatch(/import { Foo } from '.\/foo';/); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should extending EntityCollectionServiceBase', () => { |
| 89 | + const options = { ...defaultOptions, spec: true }; |
| 90 | + const tree = schematicRunner.runSchematic('data', options, appTree); |
| 91 | + const fileContent = tree.readContent( |
| 92 | + `${projectPath}/src/app/foo.service.ts` |
| 93 | + ); |
| 94 | + |
| 95 | + expect(fileContent).toMatch( |
| 96 | + /export class FooService extends EntityCollectionServiceBase<Foo>/ |
| 97 | + ); |
| 98 | + |
| 99 | + expect(fileContent).toMatch(/super\('Foo', serviceElementsFactory\);/); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should create model', () => { |
| 104 | + const options = { ...defaultOptions, spec: true }; |
| 105 | + const tree = schematicRunner.runSchematic('data', options, appTree); |
| 106 | + const fileContent = tree.readContent(`${projectPath}/src/app/foo.ts`); |
| 107 | + |
| 108 | + expect(fileContent).toMatch(/export interface Foo {/); |
| 109 | + expect(fileContent).toMatch(/id\?: any;/); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should create spec class with right imports', () => { |
| 113 | + const options = { ...defaultOptions, spec: true }; |
| 114 | + const tree = schematicRunner.runSchematic('data', options, appTree); |
| 115 | + const fileContent = tree.readContent( |
| 116 | + `${projectPath}/src/app/foo.service.spec.ts` |
| 117 | + ); |
| 118 | + |
| 119 | + expect(fileContent).toMatch(/import { FooService } from '.\/foo.service'/); |
| 120 | + }); |
| 121 | +}); |
0 commit comments