-
Notifications
You must be signed in to change notification settings - Fork 0
/
news-backend.service.spec.ts
79 lines (70 loc) · 1.98 KB
/
news-backend.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
67
68
69
70
71
72
73
74
75
76
77
78
79
import { TestBed } from '@angular/core/testing';
import { NewsBackendService } from './news-backend.service';
import { HttpTestingController, HttpClientTestingModule } from '@angular/common/http/testing';
import { INewsItem } from './common/INewsItem';
describe('NewsBackendService', () => {
let service: NewsBackendService;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
NewsBackendService
]
});
service = TestBed.inject(NewsBackendService);
httpMock = TestBed.inject(HttpTestingController);
});
afterEach(() => {
httpMock.verify();
});
it('expect correct get news', () => {
const responce: { articles: Array<INewsItem>} = {
articles: [
{
idArticle: 1,
name: 'Test',
datePublication: new Date(),
author: 'Tester',
active: true,
},
{
idArticle: 2,
name: 'Test',
datePublication: new Date(),
author: 'Tester2',
active: false,
},
{
idArticle: 3,
name: 'Test',
datePublication: new Date(),
author: 'Tester3',
active: true,
}
]
};
const result: Array<INewsItem> =
responce.articles;
service.getAllNews().subscribe(x => {
expect(x).toBeDefined();
expect(x).toEqual(result);
});
const req = httpMock.expectOne('../../../assets/news.json');
expect(req.request.method).toBe('GET');
req.flush(responce);
});
it('expect correct catch error get news', () => {
const result: Array<INewsItem> =
[];
service.getAllNews().subscribe(x => {
expect(x).toBeDefined();
expect(x).toEqual(result);
});
const req = httpMock.expectOne('../../../assets/news.json');
expect(req.request.method).toBe('GET');
req.flush(null, {status: 500, statusText: 'Error'});
});
});