Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b65505e

Browse files
authoredAug 3, 2019
fix(jest): export mock utils (#35)
Closes #34
1 parent 0cc77ac commit b65505e

File tree

11 files changed

+54
-40
lines changed

11 files changed

+54
-40
lines changed
 

‎projects/jest-utils/src/lib/configure-test-suite.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { TestBed, getTestBed, ComponentFixture } from '@angular/core/testing';
22
import 'jest';
33

4-
// Ref: https://github.com/angular/angular/issues/12409
4+
/**
5+
* Use with caution, this might brake some tests
6+
* Ref: https://github.com/angular/angular/issues/12409
7+
*/
58
export function configureJestSetup() {
69
const testBedApi: any = getTestBed();
710
const originReset = TestBed.resetTestingModule;

‎projects/jest-utils/src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './configure-test-suite';
2+
export * from './create-mock';

‎projects/testing-library/tests/jest-utils/create-mock.spec.ts renamed to ‎projects/jest-utils/tests/create-mock.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { createMock, provideMock, Mock } from '../../src/jest-utils';
2-
import { render } from '../../src/public_api';
31
import { Component } from '@angular/core';
42
import { TestBed } from '@angular/core/testing';
53

4+
import { createMock, provideMock, Mock } from '../src/public_api';
5+
import { render } from '../../testing-library/src/public_api';
6+
67
class FixtureService {
78
constructor(private foo: string, public bar: string) {}
89

‎projects/testing-library/src/jest-utils/configure-test-suite.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎projects/testing-library/src/jest-utils/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

‎src/app/__snapshots__/app.component.spec.ts.snap

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports[`matches snapshot 1`] = `
99
style="text-align:center"
1010
>
1111
<h1>
12-
Welcome to app!
12+
Welcome to app!
1313
</h1>
1414
<img
1515
alt="Angular Logo"
@@ -18,7 +18,7 @@ exports[`matches snapshot 1`] = `
1818
/>
1919
</div>
2020
<h2>
21-
Here are some links to help you start:
21+
Here are some links to help you start:
2222
</h2>
2323
<ul>
2424
<li>
@@ -55,6 +55,9 @@ exports[`matches snapshot 1`] = `
5555
</h2>
5656
</li>
5757
</ul>
58+
<button>
59+
Greet
60+
</button>
5861
</app-root>
5962
</div>
6063
`;

‎src/app/app.component.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<!--The content below is only a placeholder and can be replaced.-->
22
<div style="text-align:center">
3-
<h1>
4-
Welcome to {{ title }}!
5-
</h1>
6-
<img width="300" alt="Angular Logo" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">
3+
<h1>Welcome to {{ title }}!</h1>
4+
<img
5+
width="300"
6+
alt="Angular Logo"
7+
src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg=="
8+
/>
79
</div>
8-
<h2>Here are some links to help you start: </h2>
10+
<h2>Here are some links to help you start:</h2>
911
<ul>
1012
<li>
1113
<h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
@@ -18,3 +20,4 @@ <h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular bl
1820
</li>
1921
</ul>
2022

23+
<button (click)="greet()">Greet</button>

‎src/app/app.component.spec.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { TestBed } from '@angular/core/testing';
22
import { Store } from '@ngrx/store';
33
import { provideMockStore } from '@ngrx/store/testing';
4+
45
import { render } from '@testing-library/angular';
5-
import { configureJestSetup } from '@testing-library/angular/jest-utils';
6-
import { AppComponent } from './app.component';
6+
import { provideMock } from '@testing-library/angular/jest-utils';
77

8-
configureJestSetup();
8+
import { AppComponent } from './app.component';
9+
import { GreetService } from './greet.service';
910

1011
test(`matches snapshot`, async () => {
1112
const { container } = await render('<app-root></app-root>', {
@@ -36,5 +37,17 @@ test(`should be able to get the Store`, async () => {
3637
declarations: [AppComponent],
3738
providers: [provideMockStore()],
3839
});
39-
expect(TestBed.get(Store)).toBeDefined();
40+
expect(TestBed.get<Store<any>>(Store)).toBeDefined();
41+
});
42+
43+
test(`should provide a mock greet service`, async () => {
44+
const component = await render(AppComponent, {
45+
declarations: [AppComponent],
46+
providers: [provideMockStore(), provideMock(GreetService)],
47+
});
48+
const service: GreetService = TestBed.get<GreetService>(GreetService);
49+
50+
component.click(component.getByText('Greet'));
51+
52+
expect(service.greet).toHaveBeenCalled();
4053
});

‎src/app/app.component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component } from '@angular/core';
22
import { Store } from '@ngrx/store';
3+
import { GreetService } from './greet.service';
34

45
@Component({
56
selector: 'app-root',
@@ -9,5 +10,9 @@ import { Store } from '@ngrx/store';
910
export class AppComponent {
1011
title = 'app';
1112

12-
constructor(private store: Store<any>) {}
13+
constructor(private store: Store<any>, private greetService: GreetService) {}
14+
15+
greet() {
16+
this.greetService.greet();
17+
}
1318
}

‎src/app/greet.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Injectable } from '@angular/core';
2+
3+
@Injectable({
4+
providedIn: 'root',
5+
})
6+
export class GreetService {
7+
greet(): void {
8+
console.log('👋👋');
9+
}
10+
}

0 commit comments

Comments
 (0)
Please sign in to comment.