1 | Asynchronous test tools for rxjs and Angular
|
2 |
|
3 | Requires @angular/core since the tooling relies on zones to schedule asynchronous actions
|
4 |
|
5 | ## Installation
|
6 | Install rl-async-testing from npm:
|
7 | ```
|
8 | npm install rl-async-testing --save
|
9 | ```
|
10 | Configure with Angular-CLI and SystemJs:
|
11 | ```
|
12 | /** Map relative paths to URLs. */
|
13 | const map: any = {
|
14 | 'rl-async-testing': 'vendor/rl-async-testing'
|
15 | };
|
16 |
|
17 | /** User packages configuration. */
|
18 | const packages: any = {
|
19 | 'rl-async-testing': {
|
20 | main: 'index.js'
|
21 | }
|
22 | };
|
23 | ```
|
24 | Modify angular-cli-build.js by adding this line to vendorNpmFiles:
|
25 | ```
|
26 | 'rl-async-testing/**/*.+(js|js.map)'
|
27 | ```
|
28 |
|
29 | ## Usage
|
30 | The rlFakeAsync zone can be used in place of the angular fakeAsync zone. The mock tools can be used in conjunction or independently of this, but if combined, the zone will track requests against the mocks and fail the test if there are any requests pending.
|
31 | ```
|
32 | import { rlFakeAsync, mock, IMockedPromise, IMockedRequest } from 'rl-async-testing';
|
33 |
|
34 | interface IMockDataService {
|
35 | getWithPromise: IMockedPromise<number>;
|
36 | getWithObservable: IMockedRequest<number>;
|
37 | }
|
38 |
|
39 | // mock definition
|
40 | let dataService: IMockDataService = {
|
41 | getWithPromise: mock.promise(3),
|
42 | getWithObservable: mock.request(3),
|
43 | };
|
44 | // or
|
45 | let dataService: IMockDataService = {
|
46 | getWithPromise: mock.promise(x => x.value),
|
47 | getWithObservable: mock.request(x => x.value),
|
48 | };
|
49 |
|
50 |
|
51 | it('should schedule mock async requests synchronously', rlFakeAsync(() => {
|
52 | dataService.getWithPromise();
|
53 | dataService.getWithObservable();
|
54 |
|
55 | // IMockedPromise and IMockedRequest extend Sinon.SinonSpy
|
56 | sinon.assert.calledOnce(getWithPromise);
|
57 | sinon.assert.calledOnce(getWithObservable);
|
58 |
|
59 | // assert data not loaded
|
60 |
|
61 | dataService.getWithPromise.flush();
|
62 | dataService.getWithObservable.flush();
|
63 |
|
64 | // assert data loaded
|
65 | }));
|
66 | ```
|
67 | ### rxjs scheduling
|
68 | The rlFakeAsync zone hooks into the rxjs async scheduler to schedule observables based on 'fake' time. This requires using the rlTick wrapper, which calls into angular's tick, but also tracks time for the rxjs scheduler.
|
69 | ```
|
70 | import { rlFakeAsync, rlTick, flushMicrotasks } from 'rl-async-testing';
|
71 |
|
72 | it('should schedule rxjs observables synchronously', rlFakeAsync(() => {
|
73 | Observable.of(3).delay(1000);
|
74 |
|
75 | // assert data not loaded
|
76 |
|
77 | rlTick(1000);
|
78 | flushMicrotasks();
|
79 |
|
80 | // assert data loaded
|
81 | }));
|
82 | ```
|
83 |
|
84 | ## Tests
|
85 | This project uses systemjs and karma to run unit tests.
|
86 | ```
|
87 | npm install
|
88 | npm run build
|
89 | npm test
|
90 | ```
|
91 | Alternately:
|
92 | ```
|
93 | npm install
|
94 | npm run build-test
|
95 | ```
|
96 | If you encounter an issue and want to debug it:
|
97 | ```
|
98 | npm run test.debug
|
99 | ```
|
100 | or
|
101 | ```
|
102 | npm run build-test.watch
|
103 | ```
|
104 | (Runs tsc in watch mode and concurrently runs the test debugger) |
\ | No newline at end of file |