1- import { SchematicTestRunner } from '@angular-devkit/schematics/testing' ;
1+ import {
2+ SchematicTestRunner ,
3+ UnitTestTree ,
4+ } from '@angular-devkit/schematics/testing' ;
25import * as path from 'path' ;
36import { Schema as ActionOptions } from './schema' ;
7+ import {
8+ getProjectPath ,
9+ createWorkspace ,
10+ } from '../utility/test/create-workspace' ;
411
512describe ( 'Action Schematic' , ( ) => {
613 const schematicRunner = new SchematicTestRunner (
@@ -9,49 +16,70 @@ describe('Action Schematic', () => {
916 ) ;
1017 const defaultOptions : ActionOptions = {
1118 name : 'foo' ,
12- path : 'app' ,
13- sourceDir : 'src ' ,
19+ // path: 'app',
20+ project : 'bar ' ,
1421 spec : false ,
1522 group : false ,
1623 flat : true ,
1724 } ;
1825
26+ const projectPath = getProjectPath ( ) ;
27+
28+ let appTree : UnitTestTree ;
29+
30+ beforeEach ( ( ) => {
31+ appTree = createWorkspace ( schematicRunner , appTree ) ;
32+ } ) ;
33+
1934 it ( 'should create one file' , ( ) => {
20- const tree = schematicRunner . runSchematic ( 'action' , defaultOptions ) ;
21- expect ( tree . files . length ) . toEqual ( 1 ) ;
22- expect ( tree . files [ 0 ] ) . toEqual ( '/src/app/foo.actions.ts' ) ;
35+ const tree = schematicRunner . runSchematic (
36+ 'action' ,
37+ defaultOptions ,
38+ appTree
39+ ) ;
40+ expect (
41+ tree . files . indexOf ( `${ projectPath } /src/app/foo.actions.ts` )
42+ ) . toBeGreaterThanOrEqual ( 0 ) ;
2343 } ) ;
2444
2545 it ( 'should create two files if spec is true' , ( ) => {
2646 const options = {
2747 ...defaultOptions ,
2848 spec : true ,
2949 } ;
30- const tree = schematicRunner . runSchematic ( 'action' , options ) ;
31- expect ( tree . files . length ) . toEqual ( 2 ) ;
50+ const tree = schematicRunner . runSchematic ( 'action' , options , appTree ) ;
3251 expect (
33- tree . files . indexOf ( ' /src/app/foo.actions.spec.ts' )
52+ tree . files . indexOf ( ` ${ projectPath } /src/app/foo.actions.spec.ts` )
3453 ) . toBeGreaterThanOrEqual ( 0 ) ;
3554 expect (
36- tree . files . indexOf ( ' /src/app/foo.actions.ts' )
55+ tree . files . indexOf ( ` ${ projectPath } /src/app/foo.actions.ts` )
3756 ) . toBeGreaterThanOrEqual ( 0 ) ;
3857 } ) ;
3958
4059 it ( 'should create an enum named "Foo"' , ( ) => {
41- const tree = schematicRunner . runSchematic ( 'action' , defaultOptions ) ;
42- const fileEntry = tree . get ( tree . files [ 0 ] ) ;
43- if ( fileEntry ) {
44- const fileContent = fileEntry . content . toString ( ) ;
45- expect ( fileContent ) . toMatch ( / e x p o r t e n u m F o o A c t i o n T y p e s / ) ;
46- }
60+ const tree = schematicRunner . runSchematic (
61+ 'action' ,
62+ defaultOptions ,
63+ appTree
64+ ) ;
65+ const fileContent = tree . readContent (
66+ `${ projectPath } /src/app/foo.actions.ts`
67+ ) ;
68+
69+ expect ( fileContent ) . toMatch ( / e x p o r t e n u m F o o A c t i o n T y p e s / ) ;
4770 } ) ;
4871
4972 it ( 'should group within an "actions" folder if group is set' , ( ) => {
50- const tree = schematicRunner . runSchematic ( 'action' , {
51- ...defaultOptions ,
52- group : true ,
53- } ) ;
54- expect ( tree . files . length ) . toEqual ( 1 ) ;
55- expect ( tree . files [ 0 ] ) . toEqual ( '/src/app/actions/foo.actions.ts' ) ;
73+ const tree = schematicRunner . runSchematic (
74+ 'action' ,
75+ {
76+ ...defaultOptions ,
77+ group : true ,
78+ } ,
79+ appTree
80+ ) ;
81+ expect (
82+ tree . files . indexOf ( `${ projectPath } /src/app/actions/foo.actions.ts` )
83+ ) . toBeGreaterThanOrEqual ( 0 ) ;
5684 } ) ;
5785} ) ;
0 commit comments