Newer
Older
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
67
68
import { HttpClientModule } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { ApplicationConfigService } from 'app/core/config/application-config.service';
import { LocalStorageService, SessionStorageService } from 'ngx-webstorage';
import { ExerciseService } from './exercise.service';
describe('ExerciseService', () => {
let service: ExerciseService;
let httpMock: HttpTestingController;
let applicationConfigService: ApplicationConfigService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, HttpClientModule, TranslateModule.forRoot(), RouterTestingModule],
declarations: [],
providers: [ApplicationConfigService, LocalStorageService, SessionStorageService],
});
applicationConfigService = TestBed.inject(ApplicationConfigService);
service = TestBed.inject(ExerciseService);
httpMock = TestBed.inject(HttpTestingController);
});
it('should inject', () => {
expect(service).toBeTruthy();
});
it('test Caching', () => {
let isInvoked443 = 0;
let isInvoked444 = 0;
service.loadExercise('443').subscribe(() => {
isInvoked443++;
});
service.loadExercise('444').subscribe(() => {
isInvoked444++;
});
expect(isInvoked443).toEqual(0); // still waiting for response
expect(isInvoked444).toEqual(0); // still waiting for response
const testRequest443 = httpMock.expectOne({
method: 'GET',
url: applicationConfigService.getEndpointFor('api/exercise/443'),
});
const testRequest444 = httpMock.expectOne({
method: 'GET',
url: applicationConfigService.getEndpointFor('api/exercise/444'),
});
testRequest443.flush({
kaese: 'kaese',
});
testRequest444.flush({
kaese: 'kaese',
});
expect(isInvoked443).toEqual(1);
expect(isInvoked444).toEqual(1);
service.loadExercise('444').subscribe(() => {
isInvoked444++;
});
expect(isInvoked444).toEqual(3); // result was returned immediately from cache
expect(isInvoked443).toEqual(2);
httpMock.expectNone({});
});
});