2019-12-21 20:55:43 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-03-11 19:13:27 +05:30
|
|
|
import $ from 'jquery';
|
2022-04-04 11:22:00 +05:30
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2017-09-10 17:25:29 +05:30
|
|
|
import '~/lib/utils/common_utils';
|
2019-12-21 20:55:43 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import { addDelimiter } from '~/lib/utils/text_utility';
|
2020-06-23 00:09:42 +05:30
|
|
|
import { visitUrl } from '~/lib/utils/url_utility';
|
2021-03-11 19:13:27 +05:30
|
|
|
import Todos from '~/pages/dashboard/todos/index/todos';
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
jest.mock('~/lib/utils/url_utility', () => ({
|
|
|
|
visitUrl: jest.fn().mockName('visitUrl'),
|
|
|
|
}));
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
const TEST_COUNT_BIG = 2000;
|
|
|
|
const TEST_DONE_COUNT_BIG = 7300;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe('Todos', () => {
|
|
|
|
let todoItem;
|
2019-12-21 20:55:43 +05:30
|
|
|
let mock;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-07-07 11:18:12 +05:30
|
|
|
loadFixtures('todos/todos.html');
|
2017-08-17 22:00:37 +05:30
|
|
|
todoItem = document.querySelector('.todos-list .todo');
|
2019-12-21 20:55:43 +05:30
|
|
|
mock = new MockAdapter(axios);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
return new Todos();
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe('goToTodoUrl', () => {
|
2021-03-08 18:12:59 +05:30
|
|
|
it('opens the todo url', (done) => {
|
2017-08-17 22:00:37 +05:30
|
|
|
const todoLink = todoItem.dataset.url;
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
visitUrl.mockImplementation((url) => {
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(url).toEqual(todoLink);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
todoItem.click();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('meta click', () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
let windowOpenSpy;
|
|
|
|
let metakeyEvent;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-03-17 18:26:18 +05:30
|
|
|
metakeyEvent = $.Event('click', { keyCode: 91, ctrlKey: true });
|
2020-06-23 00:09:42 +05:30
|
|
|
windowOpenSpy = jest.spyOn(window, 'open').mockImplementation(() => {});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('opens the todo url in another tab', () => {
|
2017-08-17 22:00:37 +05:30
|
|
|
const todoLink = todoItem.dataset.url;
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
$('.todos-list .todo').trigger(metakeyEvent);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(visitUrl).not.toHaveBeenCalled();
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(windowOpenSpy).toHaveBeenCalledWith(todoLink, '_blank');
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('run native funcionality when avatar is clicked', () => {
|
2021-03-08 18:12:59 +05:30
|
|
|
$('.todos-list a').on('click', (e) => e.preventDefault());
|
2018-03-17 18:26:18 +05:30
|
|
|
$('.todos-list img').trigger(metakeyEvent);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(visitUrl).not.toHaveBeenCalled();
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(windowOpenSpy).not.toHaveBeenCalled();
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
describe('on done todo click', () => {
|
|
|
|
let onToggleSpy;
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
beforeEach(() => {
|
2019-12-21 20:55:43 +05:30
|
|
|
const el = document.querySelector('.js-done-todo');
|
|
|
|
const path = el.dataset.href;
|
|
|
|
|
|
|
|
// Arrange
|
|
|
|
mock
|
|
|
|
.onDelete(path)
|
|
|
|
.replyOnce(200, { count: TEST_COUNT_BIG, done_count: TEST_DONE_COUNT_BIG });
|
2020-06-23 00:09:42 +05:30
|
|
|
onToggleSpy = jest.fn();
|
2019-12-21 20:55:43 +05:30
|
|
|
$(document).on('todo:toggle', onToggleSpy);
|
|
|
|
|
|
|
|
// Act
|
|
|
|
el.click();
|
|
|
|
|
|
|
|
// Wait for axios and HTML to udpate
|
2022-04-04 11:22:00 +05:30
|
|
|
return waitForPromises();
|
2019-12-21 20:55:43 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches todo:toggle', () => {
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(onToggleSpy).toHaveBeenCalledWith(expect.anything(), TEST_COUNT_BIG);
|
2019-12-21 20:55:43 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('updates pending text', () => {
|
2022-03-02 08:16:31 +05:30
|
|
|
expect(document.querySelector('.js-todos-pending .js-todos-badge').innerHTML).toEqual(
|
2019-12-21 20:55:43 +05:30
|
|
|
addDelimiter(TEST_COUNT_BIG),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates done text', () => {
|
2022-03-02 08:16:31 +05:30
|
|
|
expect(document.querySelector('.js-todos-done .js-todos-badge').innerHTML).toEqual(
|
2019-12-21 20:55:43 +05:30
|
|
|
addDelimiter(TEST_DONE_COUNT_BIG),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|