UNPKG

2.71 kBMarkdownView Raw
1Asynchronous test tools for rxjs and Angular
2
3Requires @angular/core since the tooling relies on zones to schedule asynchronous actions
4
5## Installation
6Install rl-async-testing from npm:
7```
8npm install rl-async-testing --save
9```
10Configure with Angular-CLI SystemJs:
11```
12/** Map relative paths to URLs. */
13const map: any = {
14 'rl-async-testing': 'vendor/rl-async-testing'
15};
16
17/** User packages configuration. */
18const packages: any = {
19 'rl-async-testing': {
20 main: 'index.js'
21 }
22};
23```
24Modify angular-cli-build.js by adding this line to vendorNpmFiles:
25```
26'rl-async-testing/**/*.+(js|js.map)'
27```
28
29## Usage
30The 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```
32import { rlFakeAsync, mock, IMockedPromise, IMockedRequest } from 'rl-async-testing';
33
34interface IMockDataService {
35 getWithPromise: IMockedPromise<number>;
36 getWithObservable: IMockedRequest<number>;
37}
38
39// mock definition
40let dataService: IMockDataService = {
41 getWithPromise: mock.promise(3),
42 getWithObservable: mock.request(3),
43};
44// or
45let dataService: IMockDataService = {
46 getWithPromise: mock.promise(x => x.value),
47 getWithObservable: mock.request(x => x.value),
48};
49
50
51it('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
68The 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```
70import { rlFakeAsync, rlTick, flushMicrotasks } from 'rl-async-testing';
71
72it('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
85This project uses systemjs and karma to run the tests. Simply run the following:
86```
87npm install
88npm test
89```
90Alternately, if you encounter an issue and want to debug it, run:
91```
92npm run test.debug
93```
\No newline at end of file