Skip to content

Commit faac6ab

Browse files
committed
fix latest ng5 tests (still one with JWT but is already fixed in ng6 using interceptors)
1 parent f6cf845 commit faac6ab

File tree

1 file changed

+84
-63
lines changed
  • assets/js/form-devxpress-angular/src/services

1 file changed

+84
-63
lines changed

assets/js/form-devxpress-angular/src/services/api.spec.ts

Lines changed: 84 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,95 +6,116 @@ import { environment } from '../environments/environment'
66
import { ApiService } from './api'
77

88
describe('Http Service', () => {
9+
const baseUrl: String = environment.rest.baseUrl
10+
let httpService: ApiService
11+
let httpMock: HttpTestingController
12+
13+
beforeEach(() => {
14+
TestBed.configureTestingModule({
15+
imports: [HttpClientTestingModule],
16+
providers: [ApiService]
17+
})
918

10-
const baseUrl: String = environment.rest.baseUrl
11-
let httpService: ApiService
12-
let http: HttpTestingController
13-
14-
beforeEach(() => TestBed.configureTestingModule({
15-
imports: [HttpClientTestingModule],
16-
providers: [ApiService]
17-
}))
19+
httpService = TestBed.get(ApiService)
20+
httpMock = TestBed.get(HttpTestingController)
21+
})
1822

19-
beforeEach(() => {
20-
httpService = TestBed.get(ApiService)
21-
http = TestBed.get(HttpTestingController)
22-
})
23+
afterEach(() => {
24+
httpMock.verify();
25+
});
2326

27+
describe('Test Http Service', () => {
2428
it('should init the service', () => {
25-
expect(baseUrl.length).toBeGreaterThan(0, 'Environment api.baseUrl is not defined')
26-
expect(httpService.baseUrl)
27-
.toBe(`${baseUrl}`, 'Your service should have a field `baseUrl` correctly initialized')
29+
expect(baseUrl.length).toBeGreaterThan(0, 'Environment api.baseUrl is not defined')
30+
expect(httpService.baseUrl)
31+
.toBe(`${baseUrl}`, 'Your service should have a field `baseUrl` correctly initialized')
2832
})
2933

30-
it('should do a GET request', fakeAsync(() => {
31-
const hardcodedBooks = [{ name: 'Batman vs Superman' }, { name: 'Spirou & Fantasio' }, { name: 'Harry Potter' }]
34+
it('should do a GET request', () => {
35+
const hardcodedBooks = [{ name: 'Batman vs Superman' }, { name: 'Spirou & Fantasio' }, { name: 'Harry Potter' }]
3236

33-
httpService.get('/api/books').subscribe((res) => {
34-
expect(res).toBe(hardcodedBooks)
37+
httpService
38+
.get('/api/books')
39+
.subscribe((res) => {
40+
expect(res).toBe(hardcodedBooks)
3541
})
3642

37-
http.expectOne('/api/books')
38-
.flush(hardcodedBooks)
39-
}))
43+
httpMock
44+
.expectOne(`${baseUrl}books`)
45+
.flush(hardcodedBooks)
46+
})
4047

41-
it('should do a POST request', fakeAsync(() => {
42-
const hardcodedBooks = [{ name: 'Batman vs Superman' }, { name: 'Spirou & Fantasio' }, { name: 'Harry Potter' }]
48+
it('should do a POST request', () => {
49+
const hardcodedBooks = [{ name: 'Batman vs Superman' }, { name: 'Spirou & Fantasio' }, { name: 'Harry Potter' }]
4350

44-
httpService.post('/api/books', hardcodedBooks).subscribe((res) => {
45-
expect(res).toBe(hardcodedBooks)
51+
httpService
52+
.post('/api/books', hardcodedBooks)
53+
.subscribe((res) => {
54+
expect(res).toBe(hardcodedBooks)
4655
})
4756

48-
http.expectOne('/api/books')
49-
.flush(hardcodedBooks)
50-
}))
57+
httpMock
58+
.expectOne(`${baseUrl}books`)
59+
.flush(hardcodedBooks)
60+
})
5161

5262
it('should add/remove the JWT token to the headers', () => {
53-
// will first return a 'secret' token, then nothing on second call
54-
let firstCall = true
55-
spyOn(window.localStorage, 'getItem').and.callFake(() => {
56-
if (firstCall) {
57-
firstCall = false
63+
// will first return a 'secret' token, then nothing on second call
64+
let firstCall = true
65+
spyOn(window.localStorage, 'getItem').and.callFake(() => {
66+
if (firstCall) {
67+
firstCall = false
5868

59-
return JSON.stringify({ token: 'secret' })
60-
}
69+
return JSON.stringify({ token: 'secret' })
70+
}
6171

62-
return null
63-
})
72+
return null
73+
})
6474

65-
httpService.addJwtTokenIfExists()
75+
httpService.addJwtTokenIfExists()
6676

67-
// so we should have a header the first time
68-
expect(httpService.options.headers['Authorization'])
69-
.toBe('Bearer secret', 'The `Authorization` header is not correct after adding the JWT token')
77+
// so we should have a header the first time
78+
expect(httpService.options.headers['Authorization'])
79+
.toBe('Bearer secret', 'The `Authorization` header is not correct after adding the JWT token')
7080

71-
httpService.addJwtTokenIfExists()
81+
httpService.addJwtTokenIfExists()
7282

73-
// and no header the second time
74-
expect(httpService.options.headers['Authorization']).toBeNull('The `Authorization` header should be null after removing the JWT token')
83+
// and no header the second time
84+
expect(httpService.options.headers['Authorization']).toBeNull('The `Authorization` header should be null after removing the JWT token')
7585
})
7686

77-
it('should do an authenticated GET request', async(() => {
78-
spyOn(window.localStorage, 'getItem')
79-
.and.returnValue(JSON.stringify({ token: 'secret' }))
87+
it('should do an authenticated GET request', () => {
88+
spyOn(window.localStorage, 'getItem')
89+
.and.returnValue(JSON.stringify({ token: 'secret' }))
8090

81-
const hardcodedBooks = [{ name: 'Batman vs Superman' }, { name: 'Spirou & Fantasio' }, { name: 'Harry Potter' }]
82-
httpService.get('/api/books').subscribe((res) => {
83-
expect(res).toBe(hardcodedBooks)
91+
const hardcodedBooks = [{ name: 'Batman vs Superman' }, { name: 'Spirou & Fantasio' }, { name: 'Harry Potter' }]
92+
httpService
93+
.get('/api/books')
94+
.subscribe((res) => {
95+
expect(res).toBe(hardcodedBooks)
8496
})
85-
}))
8697

87-
it('should do an authenticated DELETE request', async(() => {
88-
spyOn(window.localStorage, 'getItem')
89-
.and.returnValue(JSON.stringify({ token: 'secret' }))
98+
httpMock
99+
.expectOne(`${baseUrl}books`)
100+
.flush(hardcodedBooks)
90101

91-
const hardcodedBooks = [{ name: 'Batman vs Superman' }, { name: 'Spirou & Fantasio' }, { name: 'Harry Potter' }]
92-
httpService.get('/api/books').subscribe((res) => {
93-
expect(res).toBe(hardcodedBooks)
94-
})
102+
expect(window.localStorage.getItem).toHaveBeenCalled();
103+
// @todo: how to check localStorage
104+
})
95105

96-
httpService.delete('/api/books/1').subscribe((res) => {
97-
expect(res.status).toBe(204, 'The delete method should return the response (and not extract the JSON).')
98-
})
99-
}))
106+
it('should do an authenticated DELETE request', () => {
107+
spyOn(window.localStorage, 'getItem')
108+
.and.returnValue(JSON.stringify({ token: 'secret' }))
109+
110+
httpService
111+
.delete('/api/books/1')
112+
.subscribe()
113+
114+
httpMock
115+
.expectOne(`${baseUrl}books/1`)
116+
.flush({}, {status: 204, statusText: 'OK'})
117+
118+
expect(window.localStorage.getItem).toHaveBeenCalled();
119+
})
120+
})
100121
})

0 commit comments

Comments
 (0)