|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { main } from './schematics'; |
| 10 | + |
| 11 | +// We only care about the write method in these mocks of NodeJS.WriteStream. |
| 12 | +class MockWriteStream { |
| 13 | + lines: string[] = []; |
| 14 | + write(str: string) { |
| 15 | + // Strip color control characters. |
| 16 | + this.lines.push(str.replace(/[^\x20-\x7F]\[\d+m/g, '')); |
| 17 | + |
| 18 | + return true; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +describe('schematics-cli binary', () => { |
| 23 | + let stdout: MockWriteStream, stderr: MockWriteStream; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + stdout = new MockWriteStream(); |
| 27 | + stderr = new MockWriteStream(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('list-schematics works', async () => { |
| 31 | + const args = ['--list-schematics']; |
| 32 | + const res = await main({ args, stdout, stderr }); |
| 33 | + expect(stdout.lines).toMatch(/blank/); |
| 34 | + expect(stdout.lines).toMatch(/schematic/); |
| 35 | + expect(res).toEqual(0); |
| 36 | + }); |
| 37 | + |
| 38 | + it('dry-run works', async () => { |
| 39 | + const args = ['blank', 'foo', '--dry-run']; |
| 40 | + const res = await main({ args, stdout, stderr }); |
| 41 | + expect(stdout.lines).toMatch(/CREATE \/foo\/README.md/); |
| 42 | + expect(stdout.lines).toMatch(/CREATE \/foo\/.gitignore/); |
| 43 | + expect(stdout.lines).toMatch(/CREATE \/foo\/src\/foo\/index.ts/); |
| 44 | + expect(stdout.lines).toMatch(/CREATE \/foo\/src\/foo\/index_spec.ts/); |
| 45 | + expect(res).toEqual(0); |
| 46 | + }); |
| 47 | + |
| 48 | + it('error when no name is provided', async () => { |
| 49 | + const args = ['blank']; |
| 50 | + const res = await main({ args, stdout, stderr }); |
| 51 | + expect(stderr.lines).toMatch(/Error: name option is required/); |
| 52 | + expect(res).toEqual(1); |
| 53 | + }); |
| 54 | +}); |
0 commit comments