Skip to content

Commit e45a57a

Browse files
alan-agius4kyliau
authored andcommitted
test: use toContain instead of indexOf
1 parent 55aa21e commit e45a57a

File tree

14 files changed

+185
-165
lines changed

14 files changed

+185
-165
lines changed

packages/schematics/angular/application/index_spec.ts

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,25 @@ describe('Application Schematic', () => {
4444

4545
const tree = schematicRunner.runSchematic('application', options, workspaceTree);
4646
const files = tree.files;
47-
expect(files.indexOf('/projects/foo/karma.conf.js')).toBeGreaterThanOrEqual(0);
48-
expect(files.indexOf('/projects/foo/tsconfig.app.json')).toBeGreaterThanOrEqual(0);
49-
expect(files.indexOf('/projects/foo/tsconfig.spec.json')).toBeGreaterThanOrEqual(0);
50-
expect(files.indexOf('/projects/foo/tslint.json')).toBeGreaterThanOrEqual(0);
51-
expect(files.indexOf('/projects/foo/src/environments/environment.ts')).toBeGreaterThanOrEqual(0);
52-
expect(files.indexOf('/projects/foo/src/environments/environment.prod.ts')).toBeGreaterThanOrEqual(0);
53-
expect(files.indexOf('/projects/foo/src/favicon.ico')).toBeGreaterThanOrEqual(0);
54-
expect(files.indexOf('/projects/foo/src/index.html')).toBeGreaterThanOrEqual(0);
55-
expect(files.indexOf('/projects/foo/src/main.ts')).toBeGreaterThanOrEqual(0);
56-
expect(files.indexOf('/projects/foo/src/polyfills.ts')).toBeGreaterThanOrEqual(0);
57-
expect(files.indexOf('/projects/foo/src/styles.css')).toBeGreaterThanOrEqual(0);
58-
expect(files.indexOf('/projects/foo/src/test.ts')).toBeGreaterThanOrEqual(0);
59-
expect(files.indexOf('/projects/foo/src/app/app.module.ts')).toBeGreaterThanOrEqual(0);
60-
expect(files.indexOf('/projects/foo/src/app/app.component.css')).toBeGreaterThanOrEqual(0);
61-
expect(files.indexOf('/projects/foo/src/app/app.component.html')).toBeGreaterThanOrEqual(0);
62-
expect(files.indexOf('/projects/foo/src/app/app.component.spec.ts')).toBeGreaterThanOrEqual(0);
63-
expect(files.indexOf('/projects/foo/src/app/app.component.ts')).toBeGreaterThanOrEqual(0);
47+
expect(files).toEqual(jasmine.arrayContaining([
48+
'/projects/foo/karma.conf.js',
49+
'/projects/foo/tsconfig.app.json',
50+
'/projects/foo/tsconfig.spec.json',
51+
'/projects/foo/tslint.json',
52+
'/projects/foo/src/environments/environment.ts',
53+
'/projects/foo/src/environments/environment.prod.ts',
54+
'/projects/foo/src/favicon.ico',
55+
'/projects/foo/src/index.html',
56+
'/projects/foo/src/main.ts',
57+
'/projects/foo/src/polyfills.ts',
58+
'/projects/foo/src/styles.css',
59+
'/projects/foo/src/test.ts',
60+
'/projects/foo/src/app/app.module.ts',
61+
'/projects/foo/src/app/app.component.css',
62+
'/projects/foo/src/app/app.component.html',
63+
'/projects/foo/src/app/app.component.spec.ts',
64+
'/projects/foo/src/app/app.component.ts',
65+
]));
6466
});
6567

6668
it('should add the application to the workspace', () => {
@@ -93,8 +95,8 @@ describe('Application Schematic', () => {
9395

9496
const tree = schematicRunner.runSchematic('application', options, workspaceTree);
9597
const files = tree.files;
96-
expect(files.indexOf('/projects/foo/src/app/app.module.ts')).toBeGreaterThanOrEqual(0);
97-
expect(files.indexOf('/projects/foo/src/app/app-routing.module.ts')).toBeGreaterThanOrEqual(0);
98+
expect(files).toContain('/projects/foo/src/app/app.module.ts');
99+
expect(files).toContain('/projects/foo/src/app/app-routing.module.ts');
98100
const moduleContent = tree.readContent('/projects/foo/src/app/app.module.ts');
99101
expect(moduleContent).toMatch(/import { AppRoutingModule } from '.\/app-routing.module'/);
100102
const routingModuleContent = tree.readContent('/projects/foo/src/app/app-routing.module.ts');
@@ -141,7 +143,7 @@ describe('Application Schematic', () => {
141143

142144
const tree = schematicRunner.runSchematic('application', options, workspaceTree);
143145
const files = tree.files;
144-
expect(files.indexOf('/projects/foo-e2e')).toEqual(-1);
146+
expect(files).not.toContain('/projects/foo-e2e');
145147
const confContent = JSON.parse(tree.readContent('/angular.json'));
146148
expect(confContent.projects['foo-e2e']).toBeUndefined();
147149
});
@@ -150,24 +152,28 @@ describe('Application Schematic', () => {
150152
const options = { ...defaultOptions, minimal: true };
151153
const tree = schematicRunner.runSchematic('application', options, workspaceTree);
152154
const files = tree.files;
153-
154-
expect(files.indexOf('/projects/foo/karma.conf.js')).toBe(-1);
155-
expect(files.indexOf('/projects/foo/tsconfig.app.json')).toBeGreaterThanOrEqual(0);
156-
expect(files.indexOf('/projects/foo/tsconfig.spec.json')).toBe(-1);
157-
expect(files.indexOf('/projects/foo/tslint.json')).toBe(-1);
158-
expect(files.indexOf('/projects/foo/src/environments/environment.ts')).toBeGreaterThanOrEqual(0);
159-
expect(files.indexOf('/projects/foo/src/environments/environment.prod.ts')).toBeGreaterThanOrEqual(0);
160-
expect(files.indexOf('/projects/foo/src/favicon.ico')).toBeGreaterThanOrEqual(0);
161-
expect(files.indexOf('/projects/foo/src/index.html')).toBeGreaterThanOrEqual(0);
162-
expect(files.indexOf('/projects/foo/src/main.ts')).toBeGreaterThanOrEqual(0);
163-
expect(files.indexOf('/projects/foo/src/polyfills.ts')).toBeGreaterThanOrEqual(0);
164-
expect(files.indexOf('/projects/foo/src/styles.css')).toBeGreaterThanOrEqual(0);
165-
expect(files.indexOf('/projects/foo/src/test.ts')).toBe(-1);
166-
expect(files.indexOf('/projects/foo/src/app/app.module.ts')).toBeGreaterThanOrEqual(0);
167-
expect(files.indexOf('/projects/foo/src/app/app.component.css')).toBe(-1);
168-
expect(files.indexOf('/projects/foo/src/app/app.component.html')).toBe(-1);
169-
expect(files.indexOf('/projects/foo/src/app/app.component.spec.ts')).toBe(-1);
170-
expect(files.indexOf('/projects/foo/src/app/app.component.ts')).toBeGreaterThanOrEqual(0);
155+
[
156+
'/projects/foo/tsconfig.spec.json',
157+
'/projects/foo/tslint.json',
158+
'/projects/foo/karma.conf.js',
159+
'/projects/foo/src/test.ts',
160+
'/projects/foo/src/app/app.component.css',
161+
'/projects/foo/src/app/app.component.html',
162+
'/projects/foo/src/app/app.component.spec.ts',
163+
].forEach(x => expect(files).not.toContain(x));
164+
165+
expect(files).toEqual(jasmine.arrayContaining([
166+
'/projects/foo/tsconfig.app.json',
167+
'/projects/foo/src/environments/environment.ts',
168+
'/projects/foo/src/environments/environment.prod.ts',
169+
'/projects/foo/src/favicon.ico',
170+
'/projects/foo/src/index.html',
171+
'/projects/foo/src/main.ts',
172+
'/projects/foo/src/polyfills.ts',
173+
'/projects/foo/src/styles.css',
174+
'/projects/foo/src/app/app.module.ts',
175+
'/projects/foo/src/app/app.component.ts',
176+
]));
171177
});
172178

173179
describe(`update package.json`, () => {
@@ -215,23 +221,25 @@ describe('Application Schematic', () => {
215221

216222
const tree = schematicRunner.runSchematic('application', options, workspaceTree);
217223
const files = tree.files;
218-
expect(files.indexOf('/src/karma.conf.js')).toBeGreaterThanOrEqual(0);
219-
expect(files.indexOf('/src/tsconfig.app.json')).toBeGreaterThanOrEqual(0);
220-
expect(files.indexOf('/src/tsconfig.spec.json')).toBeGreaterThanOrEqual(0);
221-
expect(files.indexOf('/src/tslint.json')).toBeGreaterThanOrEqual(0);
222-
expect(files.indexOf('/src/environments/environment.ts')).toBeGreaterThanOrEqual(0);
223-
expect(files.indexOf('/src/environments/environment.prod.ts')).toBeGreaterThanOrEqual(0);
224-
expect(files.indexOf('/src/favicon.ico')).toBeGreaterThanOrEqual(0);
225-
expect(files.indexOf('/src/index.html')).toBeGreaterThanOrEqual(0);
226-
expect(files.indexOf('/src/main.ts')).toBeGreaterThanOrEqual(0);
227-
expect(files.indexOf('/src/polyfills.ts')).toBeGreaterThanOrEqual(0);
228-
expect(files.indexOf('/src/styles.css')).toBeGreaterThanOrEqual(0);
229-
expect(files.indexOf('/src/test.ts')).toBeGreaterThanOrEqual(0);
230-
expect(files.indexOf('/src/app/app.module.ts')).toBeGreaterThanOrEqual(0);
231-
expect(files.indexOf('/src/app/app.component.css')).toBeGreaterThanOrEqual(0);
232-
expect(files.indexOf('/src/app/app.component.html')).toBeGreaterThanOrEqual(0);
233-
expect(files.indexOf('/src/app/app.component.spec.ts')).toBeGreaterThanOrEqual(0);
234-
expect(files.indexOf('/src/app/app.component.ts')).toBeGreaterThanOrEqual(0);
224+
expect(files).toEqual(jasmine.arrayContaining([
225+
'/src/karma.conf.js',
226+
'/src/tsconfig.app.json',
227+
'/src/tsconfig.spec.json',
228+
'/src/tslint.json',
229+
'/src/environments/environment.ts',
230+
'/src/environments/environment.prod.ts',
231+
'/src/favicon.ico',
232+
'/src/index.html',
233+
'/src/main.ts',
234+
'/src/polyfills.ts',
235+
'/src/styles.css',
236+
'/src/test.ts',
237+
'/src/app/app.module.ts',
238+
'/src/app/app.component.css',
239+
'/src/app/app.component.html',
240+
'/src/app/app.component.spec.ts',
241+
'/src/app/app.component.ts',
242+
]));
235243
});
236244

237245
it('should set values in angular.json correctly', () => {

packages/schematics/angular/class/index_spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ describe('Class Schematic', () => {
4747

4848
it('should create just the class file', () => {
4949
const tree = schematicRunner.runSchematic('class', defaultOptions, appTree);
50-
expect(tree.files.indexOf('/projects/bar/src/app/foo.ts')).toBeGreaterThanOrEqual(0);
51-
expect(tree.files.indexOf('/projects/bar/src/app/foo.spec.ts')).toBeLessThan(0);
50+
expect(tree.files).toContain('/projects/bar/src/app/foo.ts');
51+
expect(tree.files).not.toContain('/projects/bar/src/app/foo.spec.ts');
5252
});
5353

5454
it('should create the class and spec file', () => {
@@ -57,8 +57,8 @@ describe('Class Schematic', () => {
5757
spec: true,
5858
};
5959
const tree = schematicRunner.runSchematic('class', options, appTree);
60-
expect(tree.files.indexOf('/projects/bar/src/app/foo.ts')).toBeGreaterThanOrEqual(0);
61-
expect(tree.files.indexOf('/projects/bar/src/app/foo.spec.ts')).toBeGreaterThanOrEqual(0);
60+
expect(tree.files).toContain('/projects/bar/src/app/foo.ts');
61+
expect(tree.files).toContain('/projects/bar/src/app/foo.spec.ts');
6262
});
6363

6464
it('should create an class named "Foo"', () => {
@@ -71,7 +71,7 @@ describe('Class Schematic', () => {
7171
const options = { ...defaultOptions, type: 'model' };
7272

7373
const tree = schematicRunner.runSchematic('class', options, appTree);
74-
expect(tree.files.indexOf('/projects/bar/src/app/foo.model.ts')).toBeGreaterThanOrEqual(0);
74+
expect(tree.files).toContain('/projects/bar/src/app/foo.model.ts');
7575
});
7676

7777
it('should split the name to name & type with split on "."', () => {
@@ -85,14 +85,14 @@ describe('Class Schematic', () => {
8585
it('should respect the path option', () => {
8686
const options = { ...defaultOptions, path: 'zzz' };
8787
const tree = schematicRunner.runSchematic('class', options, appTree);
88-
expect(tree.files.indexOf('/zzz/foo.ts')).toBeGreaterThanOrEqual(0);
88+
expect(tree.files).toContain('/zzz/foo.ts');
8989
});
9090

9191
it('should respect the sourceRoot value', () => {
9292
const config = JSON.parse(appTree.readContent('/angular.json'));
9393
config.projects.bar.sourceRoot = 'projects/bar/custom';
9494
appTree.overwrite('/angular.json', JSON.stringify(config, null, 2));
9595
appTree = schematicRunner.runSchematic('class', defaultOptions, appTree);
96-
expect(appTree.files.indexOf('/projects/bar/custom/app/foo.ts')).toBeGreaterThanOrEqual(0);
96+
expect(appTree.files).toContain('/projects/bar/custom/app/foo.ts');
9797
});
9898
});

packages/schematics/angular/component/index_spec.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ describe('Component Schematic', () => {
5757
const options = { ...defaultOptions };
5858
const tree = schematicRunner.runSchematic('component', options, appTree);
5959
const files = tree.files;
60-
expect(files.indexOf('/projects/bar/src/app/foo/foo.component.css')).toBeGreaterThanOrEqual(0);
61-
expect(files.indexOf('/projects/bar/src/app/foo/foo.component.html')).toBeGreaterThanOrEqual(0);
62-
expect(files.indexOf('/projects/bar/src/app/foo/foo.component.spec.ts')).toBeGreaterThanOrEqual(0);
63-
expect(files.indexOf('/projects/bar/src/app/foo/foo.component.ts')).toBeGreaterThanOrEqual(0);
60+
expect(files).toEqual(jasmine.arrayContaining([
61+
'/projects/bar/src/app/foo/foo.component.css',
62+
'/projects/bar/src/app/foo/foo.component.html',
63+
'/projects/bar/src/app/foo/foo.component.spec.ts',
64+
'/projects/bar/src/app/foo/foo.component.ts',
65+
]));
6466
const moduleContent = tree.readContent('/projects/bar/src/app/app.module.ts');
6567
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo.component'/);
6668
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
@@ -103,10 +105,12 @@ describe('Component Schematic', () => {
103105

104106
const tree = schematicRunner.runSchematic('component', options, appTree);
105107
const files = tree.files;
106-
expect(files.indexOf('/projects/bar/src/app/foo.component.css')).toBeGreaterThanOrEqual(0);
107-
expect(files.indexOf('/projects/bar/src/app/foo.component.html')).toBeGreaterThanOrEqual(0);
108-
expect(files.indexOf('/projects/bar/src/app/foo.component.spec.ts')).toBeGreaterThanOrEqual(0);
109-
expect(files.indexOf('/projects/bar/src/app/foo.component.ts')).toBeGreaterThanOrEqual(0);
108+
expect(files).toEqual(jasmine.arrayContaining([
109+
'/projects/bar/src/app/foo.component.css',
110+
'/projects/bar/src/app/foo.component.html',
111+
'/projects/bar/src/app/foo.component.spec.ts',
112+
'/projects/bar/src/app/foo.component.ts',
113+
]));
110114
});
111115

112116
it('should find the closest module', () => {
@@ -170,19 +174,23 @@ describe('Component Schematic', () => {
170174
const tree = schematicRunner.runSchematic('component', options, appTree);
171175
let files = tree.files;
172176
let root = `/${pathOption}/foo/foo.component`;
173-
expect(files.indexOf(`${root}.css`)).toBeGreaterThanOrEqual(0);
174-
expect(files.indexOf(`${root}.html`)).toBeGreaterThanOrEqual(0);
175-
expect(files.indexOf(`${root}.spec.ts`)).toBeGreaterThanOrEqual(0);
176-
expect(files.indexOf(`${root}.ts`)).toBeGreaterThanOrEqual(0);
177+
expect(files).toEqual(jasmine.arrayContaining([
178+
`${root}.css`,
179+
`${root}.html`,
180+
`${root}.spec.ts`,
181+
`${root}.ts`,
182+
]));
177183

178184
const options2 = { ...options, name: 'BAR' };
179185
const tree2 = schematicRunner.runSchematic('component', options2, tree);
180186
files = tree2.files;
181187
root = `/${pathOption}/bar/bar.component`;
182-
expect(files.indexOf(`${root}.css`)).toBeGreaterThanOrEqual(0);
183-
expect(files.indexOf(`${root}.html`)).toBeGreaterThanOrEqual(0);
184-
expect(files.indexOf(`${root}.spec.ts`)).toBeGreaterThanOrEqual(0);
185-
expect(files.indexOf(`${root}.ts`)).toBeGreaterThanOrEqual(0);
188+
expect(files).toEqual(jasmine.arrayContaining([
189+
`${root}.css`,
190+
`${root}.html`,
191+
`${root}.spec.ts`,
192+
`${root}.ts`,
193+
]));
186194
});
187195

188196
it('should create a component in a sub-directory', () => {
@@ -191,10 +199,12 @@ describe('Component Schematic', () => {
191199
const tree = schematicRunner.runSchematic('component', options, appTree);
192200
const files = tree.files;
193201
const root = `/${options.path}/foo/foo.component`;
194-
expect(files.indexOf(`${root}.css`)).toBeGreaterThanOrEqual(0);
195-
expect(files.indexOf(`${root}.html`)).toBeGreaterThanOrEqual(0);
196-
expect(files.indexOf(`${root}.spec.ts`)).toBeGreaterThanOrEqual(0);
197-
expect(files.indexOf(`${root}.ts`)).toBeGreaterThanOrEqual(0);
202+
expect(files).toEqual(jasmine.arrayContaining([
203+
`${root}.css`,
204+
`${root}.html`,
205+
`${root}.spec.ts`,
206+
`${root}.ts`,
207+
]));
198208
});
199209

200210
it('should use the prefix', () => {
@@ -227,7 +237,7 @@ describe('Component Schematic', () => {
227237
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
228238
expect(content).toMatch(/template: /);
229239
expect(content).not.toMatch(/templateUrl: /);
230-
expect(tree.files.indexOf('/projects/bar/src/app/foo/foo.component.html')).toEqual(-1);
240+
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.html');
231241
});
232242

233243
it('should respect the inlineStyle option', () => {
@@ -236,17 +246,16 @@ describe('Component Schematic', () => {
236246
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
237247
expect(content).toMatch(/styles: \[/);
238248
expect(content).not.toMatch(/styleUrls: /);
239-
expect(tree.files.indexOf('/projects/bar/src/app/foo/foo.component.css')).toEqual(-1);
249+
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
240250
});
241251

242252
it('should respect the styleext option', () => {
243253
const options = { ...defaultOptions, styleext: 'scss' };
244254
const tree = schematicRunner.runSchematic('component', options, appTree);
245255
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
246256
expect(content).toMatch(/styleUrls: \['.\/foo.component.scss/);
247-
expect(tree.files.indexOf('/projects/bar/src/app/foo/foo.component.scss'))
248-
.toBeGreaterThanOrEqual(0);
249-
expect(tree.files.indexOf('/projects/bar/src/app/foo/foo.component.css')).toEqual(-1);
257+
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.component.scss');
258+
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
250259
});
251260

252261
it('should use the module flag even if the module is a routing module', () => {
@@ -299,7 +308,6 @@ describe('Component Schematic', () => {
299308
// move the module
300309
appTree.rename('/projects/bar/src/app/app.module.ts', '/projects/bar/custom/app/app.module.ts');
301310
appTree = schematicRunner.runSchematic('component', defaultOptions, appTree);
302-
expect(appTree.files.indexOf('/projects/bar/custom/app/foo/foo.component.ts'))
303-
.toBeGreaterThanOrEqual(0);
311+
expect(appTree.files).toContain('/projects/bar/custom/app/foo/foo.component.ts');
304312
});
305313
});

packages/schematics/angular/directive/index_spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ describe('Directive Schematic', () => {
5252

5353
const tree = schematicRunner.runSchematic('directive', options, appTree);
5454
const files = tree.files;
55-
expect(files.indexOf('/projects/bar/src/app/foo.directive.spec.ts')).toBeGreaterThanOrEqual(0);
56-
expect(files.indexOf('/projects/bar/src/app/foo.directive.ts')).toBeGreaterThanOrEqual(0);
55+
expect(files).toContain('/projects/bar/src/app/foo.directive.spec.ts');
56+
expect(files).toContain('/projects/bar/src/app/foo.directive.ts');
5757
const moduleContent = tree.readContent('/projects/bar/src/app/app.module.ts');
5858
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo.directive'/);
5959
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooDirective\r?\n/m);
@@ -64,8 +64,8 @@ describe('Directive Schematic', () => {
6464

6565
const tree = schematicRunner.runSchematic('directive', options, appTree);
6666
const files = tree.files;
67-
expect(files.indexOf('/projects/bar/src/app/foo/foo.directive.spec.ts')).toBeGreaterThanOrEqual(0);
68-
expect(files.indexOf('/projects/bar/src/app/foo/foo.directive.ts')).toBeGreaterThanOrEqual(0);
67+
expect(files).toContain('/projects/bar/src/app/foo/foo.directive.spec.ts');
68+
expect(files).toContain('/projects/bar/src/app/foo/foo.directive.ts');
6969
});
7070

7171
it('should find the closest module', () => {
@@ -165,7 +165,6 @@ describe('Directive Schematic', () => {
165165
// move the module
166166
appTree.rename('/projects/bar/src/app/app.module.ts', '/projects/bar/custom/app/app.module.ts');
167167
appTree = schematicRunner.runSchematic('directive', defaultOptions, appTree);
168-
expect(appTree.files.indexOf('/projects/bar/custom/app/foo.directive.ts'))
169-
.toBeGreaterThanOrEqual(0);
168+
expect(appTree.files).toContain('/projects/bar/custom/app/foo.directive.ts');
170169
});
171170
});

0 commit comments

Comments
 (0)