|
| 1 | +import { tags } from '@angular-devkit/core'; |
| 2 | +import { |
| 3 | + SchematicTestRunner, |
| 4 | + UnitTestTree, |
| 5 | +} from '@angular-devkit/schematics/testing'; |
| 6 | +import * as path from 'path'; |
| 7 | +import { Schema as SelectorOptions } from './schema'; |
| 8 | +import { |
| 9 | + getTestProjectPath, |
| 10 | + createWorkspace, |
| 11 | +} from '../../../schematics-core/testing'; |
| 12 | + |
| 13 | +describe('Selector Schematic', () => { |
| 14 | + const schematicRunner = new SchematicTestRunner( |
| 15 | + '@ngrx/schematics', |
| 16 | + path.join(__dirname, '../../collection.json') |
| 17 | + ); |
| 18 | + const defaultOptions: SelectorOptions = { |
| 19 | + name: 'foo', |
| 20 | + project: 'bar', |
| 21 | + spec: true, |
| 22 | + }; |
| 23 | + |
| 24 | + const projectPath = getTestProjectPath(); |
| 25 | + |
| 26 | + let appTree: UnitTestTree; |
| 27 | + |
| 28 | + beforeEach(async () => { |
| 29 | + appTree = await createWorkspace(schematicRunner, appTree); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should create selector files', () => { |
| 33 | + const tree = schematicRunner.runSchematic( |
| 34 | + 'selector', |
| 35 | + defaultOptions, |
| 36 | + appTree |
| 37 | + ); |
| 38 | + |
| 39 | + const selectorsContent = tree.readContent( |
| 40 | + `${projectPath}/src/app/foo.selectors.ts` |
| 41 | + ); |
| 42 | + const specContent = tree.readContent( |
| 43 | + `${projectPath}/src/app/foo.selectors.spec.ts` |
| 44 | + ); |
| 45 | + |
| 46 | + expect(cleanString(selectorsContent)).toBe( |
| 47 | + cleanString( |
| 48 | + tags.stripIndent`import { createFeatureSelector, createSelector } from '@ngrx/store';` |
| 49 | + ) |
| 50 | + ); |
| 51 | + |
| 52 | + expect(cleanString(specContent)).toBe( |
| 53 | + cleanString(tags.stripIndent` |
| 54 | + describe('Foo Selectors', () => { |
| 55 | + it('should select the feature state', () => { |
| 56 | + ** |
| 57 | + }); |
| 58 | + });`) |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should not create a spec file if spec is false', () => { |
| 63 | + const options = { |
| 64 | + ...defaultOptions, |
| 65 | + spec: false, |
| 66 | + }; |
| 67 | + const tree = schematicRunner.runSchematic('selector', options, appTree); |
| 68 | + |
| 69 | + expect( |
| 70 | + tree.files.includes(`${projectPath}/src/app/foo.selectors.spec.ts`) |
| 71 | + ).toBeFalsy(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should group selectors if group is true', () => { |
| 75 | + const options = { |
| 76 | + ...defaultOptions, |
| 77 | + group: true, |
| 78 | + }; |
| 79 | + const tree = schematicRunner.runSchematic('selector', options, appTree); |
| 80 | + |
| 81 | + expect( |
| 82 | + tree.files.includes(`${projectPath}/src/app/selectors/foo.selectors.ts`) |
| 83 | + ).toBeTruthy(); |
| 84 | + expect( |
| 85 | + tree.files.includes( |
| 86 | + `${projectPath}/src/app/selectors/foo.selectors.spec.ts` |
| 87 | + ) |
| 88 | + ).toBeTruthy(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should not flatten selectors if flat is false', () => { |
| 92 | + const options = { |
| 93 | + ...defaultOptions, |
| 94 | + flat: false, |
| 95 | + }; |
| 96 | + const tree = schematicRunner.runSchematic('selector', options, appTree); |
| 97 | + |
| 98 | + expect( |
| 99 | + tree.files.includes(`${projectPath}/src/app/foo/foo.selectors.ts`) |
| 100 | + ).toBeTruthy(); |
| 101 | + expect( |
| 102 | + tree.files.includes(`${projectPath}/src/app/foo/foo.selectors.spec.ts`) |
| 103 | + ).toBeTruthy(); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('With feature flag', () => { |
| 107 | + it('should create a selector', () => { |
| 108 | + const options = { |
| 109 | + ...defaultOptions, |
| 110 | + feature: true, |
| 111 | + }; |
| 112 | + |
| 113 | + const tree = schematicRunner.runSchematic('selector', options, appTree); |
| 114 | + const selectorsContent = tree.readContent( |
| 115 | + `${projectPath}/src/app/foo.selectors.ts` |
| 116 | + ); |
| 117 | + const specContent = tree.readContent( |
| 118 | + `${projectPath}/src/app/foo.selectors.spec.ts` |
| 119 | + ); |
| 120 | + |
| 121 | + expect(cleanString(selectorsContent)).toBe( |
| 122 | + cleanString(tags.stripIndent` |
| 123 | + import { createFeatureSelector, createSelector } from '@ngrx/store'; |
| 124 | + import * as fromFoo from './foo.reducer'; |
| 125 | +
|
| 126 | + export const selectFooState = createFeatureSelector<fromFoo.State>( |
| 127 | + fromFoo.fooFeatureKey |
| 128 | + ); |
| 129 | + `) |
| 130 | + ); |
| 131 | + |
| 132 | + expect(cleanString(specContent)).toBe( |
| 133 | + cleanString(tags.stripIndent` |
| 134 | + import * as fromFoo from './foo.reducer'; |
| 135 | + import { selectFooState } from './foo.selectors'; |
| 136 | +
|
| 137 | + describe('Foo Selectors', () => { |
| 138 | + it('should select the feature state', () => { |
| 139 | + const result = selectFooState({ |
| 140 | + [fromFoo.fooFeatureKey]: {} |
| 141 | + }); |
| 142 | +
|
| 143 | + expect(result).toEqual({}); |
| 144 | + }); |
| 145 | + }); |
| 146 | + `) |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should group and nest the selectors within a feature', () => { |
| 151 | + const options = { |
| 152 | + ...defaultOptions, |
| 153 | + feature: true, |
| 154 | + group: true, |
| 155 | + flat: false, |
| 156 | + }; |
| 157 | + |
| 158 | + const tree = schematicRunner.runSchematic('selector', options, appTree); |
| 159 | + const selectorPath = `${projectPath}/src/app/selectors/foo/foo.selectors.ts`; |
| 160 | + const specPath = `${projectPath}/src/app/selectors/foo/foo.selectors.spec.ts`; |
| 161 | + |
| 162 | + expect(tree.files.includes(selectorPath)).toBeTruthy(); |
| 163 | + expect(tree.files.includes(specPath)).toBeTruthy(); |
| 164 | + |
| 165 | + const selectorContent = tree.readContent(selectorPath); |
| 166 | + expect(selectorContent).toMatch( |
| 167 | + /import\ \* as fromFoo from\ \'\.\.\/\.\.\/reducers\/foo\/foo\.reducer';/ |
| 168 | + ); |
| 169 | + |
| 170 | + const specContent = tree.readContent(specPath); |
| 171 | + expect(specContent).toMatch( |
| 172 | + /import\ \* as fromFoo from\ \'\.\.\/\.\.\/reducers\/foo\/foo\.reducer';/ |
| 173 | + ); |
| 174 | + expect(specContent).toMatch( |
| 175 | + /import\ \{ selectFooState \} from\ \'\.\/foo\.selectors';/ |
| 176 | + ); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + function cleanString(value: string) { |
| 181 | + // ** to mark an empty line (VSCode removes whitespace lines) |
| 182 | + return value |
| 183 | + .replace(/\r\n/g, '\n') |
| 184 | + .replace(/\*\*/g, '') |
| 185 | + .trim(); |
| 186 | + } |
| 187 | +}); |
0 commit comments