-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathfake-http.service.spec.ts
More file actions
95 lines (81 loc) · 2.17 KB
/
fake-http.service.spec.ts
File metadata and controls
95 lines (81 loc) · 2.17 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {
HttpClientTestingModule,
HttpTestingController,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { FakeHttpService } from './fake-http.service';
describe('FakeHTTPService', () => {
let service: FakeHttpService;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
});
service = TestBed.inject(FakeHttpService);
httpMock = TestBed.inject(HttpTestingController);
});
test('should be created', () => {
expect(service).toBeTruthy();
});
test('should fetch all todos and update signals', () => {
// Arrange
const mockTodos = [
{
id: 1,
title: 'Test todo',
body: 'Todo body',
completed: 'false',
},
];
// Act
service.getAllTodos();
// Assert request
const req = httpMock.expectOne(
'https://jsonplaceholder.typicode.com/todos',
);
expect(req.request.method).toBe('GET');
req.flush(mockTodos);
// Assert signal update
expect(service.todoSignal()).toEqual(mockTodos);
});
test('should pass updated todo and update signal', () => {
// Arrange
const initialTodo = [
{
id: 1,
title: 'old',
completed: false,
},
];
service.todoSignal.set(initialTodo);
const updatedTodo = {
id: 1,
title: 'new',
completed: true,
};
service.updateTodo(updatedTodo);
const req = httpMock.expectOne(
'https://jsonplaceholder.typicode.com/todos/1',
);
expect(req.request.method).toBe('PUT');
req.flush(updatedTodo);
expect(service.todoSignal()[0]).toEqual(updatedTodo);
});
test('should delete the delted todo and update the signal', () => {
const initalTodo = [
{
id: 1,
title: 'old',
completed: false,
},
];
service.todoSignal.set(initalTodo);
service.deleteTodo(initalTodo[0]);
const req = httpMock.expectOne(
'https://jsonplaceholder.typicode.com/todos/1',
);
expect(req.request.method).toBe('DELETE');
req.flush({});
expect(service.todoSignal().length).toBe(0);
});
});