2019-03-02 22:35:43 +05:30
|
|
|
import $ from 'jquery';
|
2022-07-16 23:28:13 +05:30
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2019-03-02 22:35:43 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2021-03-11 19:13:27 +05:30
|
|
|
import TaskList from '~/task_list';
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
describe('TaskList', () => {
|
|
|
|
let taskList;
|
|
|
|
let currentTarget;
|
|
|
|
const taskListOptions = {
|
|
|
|
selector: '.task-list',
|
|
|
|
dataType: 'issue',
|
|
|
|
fieldName: 'description',
|
|
|
|
lockVersion: 2,
|
|
|
|
};
|
|
|
|
const createTaskList = () => new TaskList(taskListOptions);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture(`
|
2019-03-02 22:35:43 +05:30
|
|
|
<div class="task-list">
|
2021-06-08 01:23:25 +05:30
|
|
|
<div class="js-task-list-container">
|
|
|
|
<ul data-sourcepos="5:1-5:11" class="task-list" dir="auto">
|
|
|
|
<li data-sourcepos="5:1-5:11" class="task-list-item enabled">
|
|
|
|
<input type="checkbox" class="task-list-item-checkbox" checked=""> markdown task
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<ul class="task-list" dir="auto">
|
|
|
|
<li class="task-list-item enabled">
|
|
|
|
<input type="checkbox" class="task-list-item-checkbox"> hand-coded checkbox
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
<textarea class="hidden js-task-list-field"></textarea>
|
|
|
|
</div>
|
2019-03-02 22:35:43 +05:30
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
|
|
|
|
currentTarget = $('<div></div>');
|
|
|
|
taskList = createTaskList();
|
|
|
|
});
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
it('should call init when the class constructed', () => {
|
2020-03-13 15:44:24 +05:30
|
|
|
jest.spyOn(TaskList.prototype, 'init');
|
|
|
|
jest.spyOn(TaskList.prototype, 'disable').mockImplementation(() => {});
|
|
|
|
jest.spyOn($.prototype, 'taskList').mockImplementation(() => {});
|
|
|
|
jest.spyOn($.prototype, 'on').mockImplementation(() => {});
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
taskList = createTaskList();
|
|
|
|
const $taskListEl = $(taskList.taskListContainerSelector);
|
|
|
|
|
|
|
|
expect(taskList.init).toHaveBeenCalled();
|
|
|
|
expect(taskList.disable).toHaveBeenCalled();
|
|
|
|
expect($taskListEl.taskList).toHaveBeenCalledWith('enable');
|
|
|
|
expect($(document).on).toHaveBeenCalledWith(
|
|
|
|
'tasklist:changed',
|
|
|
|
taskList.taskListContainerSelector,
|
|
|
|
taskList.updateHandler,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getTaskListTarget', () => {
|
|
|
|
it('should return currentTarget from event object if exists', () => {
|
|
|
|
const $target = taskList.getTaskListTarget({ currentTarget });
|
|
|
|
|
|
|
|
expect($target).toEqual(currentTarget);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return element of the taskListContainerSelector', () => {
|
|
|
|
const $target = taskList.getTaskListTarget();
|
|
|
|
|
|
|
|
expect($target).toEqual($(taskList.taskListContainerSelector));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('disableTaskListItems', () => {
|
|
|
|
it('should call taskList method with disable param', () => {
|
2021-06-08 01:23:25 +05:30
|
|
|
taskList.disableTaskListItems();
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(document.querySelectorAll('.task-list-item input:disabled').length).toEqual(2);
|
2019-03-02 22:35:43 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('enableTaskListItems', () => {
|
2021-06-08 01:23:25 +05:30
|
|
|
it('should enable markdown tasks and disable non-markdown tasks', () => {
|
|
|
|
taskList.disableTaskListItems();
|
|
|
|
taskList.enableTaskListItems();
|
|
|
|
|
|
|
|
expect(document.querySelectorAll('.task-list-item input:enabled').length).toEqual(1);
|
|
|
|
expect(document.querySelectorAll('.task-list-item input:disabled').length).toEqual(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('enable', () => {
|
|
|
|
it('should enable task list items and on document event', () => {
|
|
|
|
jest.spyOn($.prototype, 'on').mockImplementation(() => {});
|
|
|
|
|
|
|
|
taskList.enable();
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(document.querySelectorAll('.task-list-item input:enabled').length).toEqual(1);
|
|
|
|
expect(document.querySelectorAll('.task-list-item input:disabled').length).toEqual(1);
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
expect($(document).on).toHaveBeenCalledWith(
|
|
|
|
'tasklist:changed',
|
|
|
|
taskList.taskListContainerSelector,
|
|
|
|
taskList.updateHandler,
|
|
|
|
);
|
2019-03-02 22:35:43 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('disable', () => {
|
|
|
|
it('should disable task list items and off document event', () => {
|
2020-03-13 15:44:24 +05:30
|
|
|
jest.spyOn($.prototype, 'off').mockImplementation(() => {});
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
taskList.disable();
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(document.querySelectorAll('.task-list-item input:disabled').length).toEqual(2);
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
expect($(document).off).toHaveBeenCalledWith(
|
|
|
|
'tasklist:changed',
|
|
|
|
taskList.taskListContainerSelector,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('update', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should disable task list items and make a patch request then enable them again', () => {
|
2019-03-02 22:35:43 +05:30
|
|
|
const response = { data: { lock_version: 3 } };
|
2020-03-13 15:44:24 +05:30
|
|
|
jest.spyOn(taskList, 'enableTaskListItems').mockImplementation(() => {});
|
|
|
|
jest.spyOn(taskList, 'disableTaskListItems').mockImplementation(() => {});
|
2021-12-11 22:18:48 +05:30
|
|
|
jest.spyOn(taskList, 'onUpdate').mockImplementation(() => {});
|
2020-03-13 15:44:24 +05:30
|
|
|
jest.spyOn(taskList, 'onSuccess').mockImplementation(() => {});
|
|
|
|
jest.spyOn(axios, 'patch').mockReturnValue(Promise.resolve(response));
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
const value = 'hello world';
|
|
|
|
const endpoint = '/foo';
|
|
|
|
const target = $(`<input data-update-url="${endpoint}" value="${value}" />`);
|
|
|
|
const detail = {
|
|
|
|
index: 2,
|
|
|
|
checked: true,
|
|
|
|
lineNumber: 8,
|
|
|
|
lineSource: '- [ ] check item',
|
|
|
|
};
|
|
|
|
const event = { target, detail };
|
|
|
|
const patchData = {
|
|
|
|
[taskListOptions.dataType]: {
|
|
|
|
[taskListOptions.fieldName]: value,
|
|
|
|
lock_version: taskListOptions.lockVersion,
|
|
|
|
update_task: {
|
|
|
|
index: detail.index,
|
|
|
|
checked: detail.checked,
|
|
|
|
line_number: detail.lineNumber,
|
|
|
|
line_source: detail.lineSource,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
const update = taskList.update(event);
|
|
|
|
|
|
|
|
expect(taskList.onUpdate).toHaveBeenCalled();
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
return update.then(() => {
|
|
|
|
expect(taskList.disableTaskListItems).toHaveBeenCalledWith(event);
|
|
|
|
expect(axios.patch).toHaveBeenCalledWith(endpoint, patchData);
|
|
|
|
expect(taskList.enableTaskListItems).toHaveBeenCalledWith(event);
|
|
|
|
expect(taskList.onSuccess).toHaveBeenCalledWith(response.data);
|
|
|
|
expect(taskList.lockVersion).toEqual(response.data.lock_version);
|
|
|
|
});
|
2019-03-02 22:35:43 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should handle request error and enable task list items', () => {
|
2019-03-02 22:35:43 +05:30
|
|
|
const response = { data: { error: 1 } };
|
2020-03-13 15:44:24 +05:30
|
|
|
jest.spyOn(taskList, 'enableTaskListItems').mockImplementation(() => {});
|
2021-12-11 22:18:48 +05:30
|
|
|
jest.spyOn(taskList, 'onUpdate').mockImplementation(() => {});
|
2020-03-13 15:44:24 +05:30
|
|
|
jest.spyOn(taskList, 'onError').mockImplementation(() => {});
|
|
|
|
jest.spyOn(axios, 'patch').mockReturnValue(Promise.reject({ response })); // eslint-disable-line prefer-promise-reject-errors
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
const event = { detail: {} };
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
const update = taskList.update(event);
|
|
|
|
|
|
|
|
expect(taskList.onUpdate).toHaveBeenCalled();
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
return update.then(() => {
|
|
|
|
expect(taskList.enableTaskListItems).toHaveBeenCalledWith(event);
|
|
|
|
expect(taskList.onError).toHaveBeenCalledWith(response.data);
|
|
|
|
});
|
2019-03-02 22:35:43 +05:30
|
|
|
});
|
|
|
|
});
|