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 MockAdapter from 'axios-mock-adapter';
import htmlTodos from 'test_fixtures/todos/todos.html';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import waitForPromises from 'helpers/wait_for_promises';
import '~/lib/utils/common_utils';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
import { addDelimiter } from '~/lib/utils/text_utility';
import Todos from '~/pages/dashboard/todos/index/todos';
jest.mock('~/lib/utils/url_utility', () => ({
visitUrl: jest.fn().mockName('visitUrl'),
}));
const TEST_COUNT_BIG = 2000;
const TEST_DONE_COUNT_BIG = 7300;
describe('Todos', () => {
let mock;
beforeEach(() => {
setHTMLFixture(htmlTodos);
mock = new MockAdapter(axios);
return new Todos();
});
afterEach(() => {
resetHTMLFixture();
});
afterEach(() => {
mock.restore();
});
describe('on done todo click', () => {
let onToggleSpy;
beforeEach(() => {
const el = document.querySelector('.js-done-todo');
const path = el.dataset.href;
// Arrange
mock
.onDelete(path)
.replyOnce(HTTP_STATUS_OK, { count: TEST_COUNT_BIG, done_count: TEST_DONE_COUNT_BIG });
onToggleSpy = jest.fn();
document.addEventListener('todo:toggle', onToggleSpy);
// Act
el.click();
// Wait for axios and HTML to udpate
return waitForPromises();
});
it('dispatches todo:toggle', () => {
expect(onToggleSpy).toHaveBeenCalledWith(
expect.objectContaining({
detail: {
count: TEST_COUNT_BIG,
},
}),
);
});
it('updates pending text', () => {
expect(document.querySelector('.js-todos-pending .js-todos-badge').innerHTML).toEqual(
addDelimiter(TEST_COUNT_BIG),
);
});
it('updates done text', () => {
expect(document.querySelector('.js-todos-done .js-todos-badge').innerHTML).toEqual(
addDelimiter(TEST_DONE_COUNT_BIG),
);
});
});
});
|