-
-
Notifications
You must be signed in to change notification settings - Fork 375
/
app.service.spec.ts
66 lines (62 loc) · 1.81 KB
/
app.service.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { Test } from '@nestjs/testing';
import { throwError, timer } from 'rxjs';
import { AppService } from './app.service';
import { RxjsService } from './rxjs/rxjs.service';
describe('AppService', () => {
let service: AppService;
let rxjsService: RxjsService;
beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
AppService,
{
provide: RxjsService,
useValue: {
genericRetryStrategy: jest.fn().mockReturnValue(timer(50)),
},
},
],
}).compile();
service = module.get<AppService>(AppService);
rxjsService = module.get<RxjsService>(RxjsService);
});
describe('playWithRxJS', () => {
it('should return a valid value', (done) => {
let counter = 0;
let finalVal: number;
service.playWithRxJS(50).subscribe({
next: (val) => {
// checks each value as it is sent back
// as we have an incremental observable
// we need to check each value sent
expect(val).toBe(++counter);
finalVal = val;
},
error: (err) => {
throw err;
},
complete: () => {
expect(finalVal).toBe(10);
done();
},
});
});
it('should return with an error', (done) => {
let counter = 0;
rxjsService.genericRetryStrategy = jest
.fn()
.mockReturnValueOnce(() => throwError(new Error('This should error')));
service.playWithRxJS().subscribe({
next: (val) => {
expect(val).toBe(++counter);
},
error: (err) => {
expect(err.message).toBe('This should error');
expect(rxjsService.genericRetryStrategy).toBeCalledTimes(1);
done();
},
complete: () => done(),
});
});
});
});