BigW Consortium Gitlab

fixed failing karma specs caused by TaskList

parent 8d83e517
...@@ -8,11 +8,11 @@ export default class TaskList { ...@@ -8,11 +8,11 @@ export default class TaskList {
this.dataType = options.dataType; this.dataType = options.dataType;
this.fieldName = options.fieldName; this.fieldName = options.fieldName;
this.onSuccess = options.onSuccess || (() => {}); this.onSuccess = options.onSuccess || (() => {});
this.onError = function showFlash(response) { this.onError = function showFlash(e) {
let errorMessages = ''; let errorMessages = '';
if (response.responseJSON) { if (e.response.data && typeof e.response.data === 'object') {
errorMessages = response.responseJSON.errors.join(' '); errorMessages = e.response.data.errors.join(' ');
} }
return new Flash(errorMessages || 'Update failed', 'alert'); return new Flash(errorMessages || 'Update failed', 'alert');
...@@ -42,6 +42,6 @@ export default class TaskList { ...@@ -42,6 +42,6 @@ export default class TaskList {
return axios.patch($target.data('update-url') || $('form.js-issuable-update').attr('action'), patchData) return axios.patch($target.data('update-url') || $('form.js-issuable-update').attr('action'), patchData)
.then(({ data }) => this.onSuccess(data)) .then(({ data }) => this.onSuccess(data))
.catch(() => this.onError()); .catch(err => this.onError(err));
} }
} }
/* eslint-disable no-new */ /* eslint-disable no-new */
import _ from 'underscore'; import _ from 'underscore';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import Sidebar from '~/right_sidebar'; import Sidebar from '~/right_sidebar';
import timeoutPromise from './helpers/set_timeout_promise_helper';
describe('Issuable right sidebar collapsed todo toggle', () => { describe('Issuable right sidebar collapsed todo toggle', () => {
const fixtureName = 'issues/open-issue.html.raw'; const fixtureName = 'issues/open-issue.html.raw';
const jsonFixtureName = 'todos/todos.json'; const jsonFixtureName = 'todos/todos.json';
let mock;
preloadFixtures(fixtureName); preloadFixtures(fixtureName);
preloadFixtures(jsonFixtureName); preloadFixtures(jsonFixtureName);
...@@ -19,19 +23,26 @@ describe('Issuable right sidebar collapsed todo toggle', () => { ...@@ -19,19 +23,26 @@ describe('Issuable right sidebar collapsed todo toggle', () => {
document.querySelector('.js-right-sidebar') document.querySelector('.js-right-sidebar')
.classList.toggle('right-sidebar-collapsed'); .classList.toggle('right-sidebar-collapsed');
spyOn(jQuery, 'ajax').and.callFake((res) => { mock = new MockAdapter(axios);
const d = $.Deferred();
mock.onPost(`${gl.TEST_HOST}/frontend-fixtures/issues-project/todos`).reply(() => {
const response = _.clone(todoData); const response = _.clone(todoData);
if (res.type === 'DELETE') { return [200, response];
delete response.delete_path; });
}
d.resolve(response); mock.onDelete(/(.*)\/dashboard\/todos\/\d+$/).reply(() => {
return d.promise(); const response = _.clone(todoData);
delete response.delete_path;
return [200, response];
}); });
}); });
afterEach(() => {
mock.restore();
});
it('shows add todo button', () => { it('shows add todo button', () => {
expect( expect(
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon'), document.querySelector('.js-issuable-todo.sidebar-collapsed-icon'),
...@@ -52,71 +63,101 @@ describe('Issuable right sidebar collapsed todo toggle', () => { ...@@ -52,71 +63,101 @@ describe('Issuable right sidebar collapsed todo toggle', () => {
).toBe('Add todo'); ).toBe('Add todo');
}); });
it('toggle todo state', () => { it('toggle todo state', (done) => {
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click(); document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click();
expect( setTimeout(() => {
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon .todo-undone'), expect(
).not.toBeNull(); document.querySelector('.js-issuable-todo.sidebar-collapsed-icon .todo-undone'),
).not.toBeNull();
expect( expect(
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon .fa-check-square'), document.querySelector('.js-issuable-todo.sidebar-collapsed-icon .fa-check-square'),
).not.toBeNull(); ).not.toBeNull();
});
it('toggle todo state of expanded todo toggle', () => { done();
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click(); });
expect(
document.querySelector('.issuable-sidebar-header .js-issuable-todo').textContent.trim(),
).toBe('Mark done');
}); });
it('toggles todo button tooltip', () => { it('toggle todo state of expanded todo toggle', (done) => {
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click(); document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click();
expect( setTimeout(() => {
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').getAttribute('data-original-title'), expect(
).toBe('Mark done'); document.querySelector('.issuable-sidebar-header .js-issuable-todo').textContent.trim(),
}); ).toBe('Mark done');
it('marks todo as done', () => {
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click();
expect( done();
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon .todo-undone'), });
).not.toBeNull(); });
it('toggles todo button tooltip', (done) => {
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click(); document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click();
expect( setTimeout(() => {
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon .todo-undone'), expect(
).toBeNull(); document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').getAttribute('data-original-title'),
).toBe('Mark done');
expect( done();
document.querySelector('.issuable-sidebar-header .js-issuable-todo').textContent.trim(), });
).toBe('Add todo');
}); });
it('updates aria-label to mark done', () => { it('marks todo as done', (done) => {
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click(); document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click();
expect( timeoutPromise()
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').getAttribute('aria-label'), .then(() => {
).toBe('Mark done'); expect(
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon .todo-undone'),
).not.toBeNull();
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click();
})
.then(timeoutPromise)
.then(() => {
expect(
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon .todo-undone'),
).toBeNull();
expect(
document.querySelector('.issuable-sidebar-header .js-issuable-todo').textContent.trim(),
).toBe('Add todo');
})
.then(done)
.catch(done.fail);
}); });
it('updates aria-label to add todo', () => { it('updates aria-label to mark done', (done) => {
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click(); document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click();
expect( setTimeout(() => {
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').getAttribute('aria-label'), expect(
).toBe('Mark done'); document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').getAttribute('aria-label'),
).toBe('Mark done');
done();
});
});
it('updates aria-label to add todo', (done) => {
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click(); document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click();
expect( timeoutPromise()
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').getAttribute('aria-label'), .then(() => {
).toBe('Add todo'); expect(
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').getAttribute('aria-label'),
).toBe('Mark done');
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').click();
})
.then(timeoutPromise)
.then(() => {
expect(
document.querySelector('.js-issuable-todo.sidebar-collapsed-icon').getAttribute('aria-label'),
).toBe('Add todo');
})
.then(done)
.catch(done.fail);
}); });
}); });
...@@ -70,23 +70,6 @@ describe('Issue', function() { ...@@ -70,23 +70,6 @@ describe('Issue', function() {
expect($btn).toHaveText(isIssueInitiallyOpen ? 'Close issue' : 'Reopen issue'); expect($btn).toHaveText(isIssueInitiallyOpen ? 'Close issue' : 'Reopen issue');
} }
describe('task lists', function() {
beforeEach(function() {
loadFixtures('issues/issue-with-task-list.html.raw');
this.issue = new Issue();
});
it('submits an ajax request on tasklist:changed', function() {
spyOn(jQuery, 'ajax').and.callFake(function(req) {
expect(req.type).toBe('PATCH');
expect(req.url).toBe(gl.TEST_HOST + '/frontend-fixtures/issues-project/issues/1.json'); // eslint-disable-line prefer-template
expect(req.data.issue.description).not.toBe(null);
});
$('.js-task-list-field').trigger('tasklist:changed');
});
});
[true, false].forEach((isIssueInitiallyOpen) => { [true, false].forEach((isIssueInitiallyOpen) => {
describe(`with ${isIssueInitiallyOpen ? 'open' : 'closed'} issue`, function() { describe(`with ${isIssueInitiallyOpen ? 'open' : 'closed'} issue`, function() {
const action = isIssueInitiallyOpen ? 'close' : 'reopen'; const action = isIssueInitiallyOpen ? 'close' : 'reopen';
......
/* eslint-disable space-before-function-paren, no-return-assign */ /* eslint-disable space-before-function-paren, no-return-assign */
import axios from '~/lib/utils/axios_utils';
import MergeRequest from '~/merge_request'; import MergeRequest from '~/merge_request';
import CloseReopenReportToggle from '~/close_reopen_report_toggle'; import CloseReopenReportToggle from '~/close_reopen_report_toggle';
import IssuablesHelper from '~/helpers/issuables_helper'; import IssuablesHelper from '~/helpers/issuables_helper';
...@@ -21,11 +22,12 @@ import IssuablesHelper from '~/helpers/issuables_helper'; ...@@ -21,11 +22,12 @@ import IssuablesHelper from '~/helpers/issuables_helper';
}); });
it('submits an ajax request on tasklist:changed', (done) => { it('submits an ajax request on tasklist:changed', (done) => {
spyOn(jQuery, 'ajax').and.callFake((req) => { spyOn(axios, 'patch').and.callFake((url, data) => {
expect(req.type).toBe('PATCH'); expect(url).toBe(`${gl.TEST_HOST}/frontend-fixtures/merge-requests-project/merge_requests/1.json`);
expect(req.url).toBe(`${gl.TEST_HOST}/frontend-fixtures/merge-requests-project/merge_requests/1.json`); expect(data.merge_request.description).not.toBe(null);
expect(req.data.merge_request.description).not.toBe(null);
done(); done();
return Promise.resolve({ data: {} });
}); });
$('.js-task-list-field').trigger('tasklist:changed'); $('.js-task-list-field').trigger('tasklist:changed');
......
...@@ -65,14 +65,16 @@ import timeoutPromise from './helpers/set_timeout_promise_helper'; ...@@ -65,14 +65,16 @@ import timeoutPromise from './helpers/set_timeout_promise_helper';
expect($('.js-task-list-field.original-task-list').val()).toBe('- [x] Task List Item'); expect($('.js-task-list-field.original-task-list').val()).toBe('- [x] Task List Item');
}); });
it('submits an ajax request on tasklist:changed', function() { it('submits an ajax request on tasklist:changed', function(done) {
spyOn(jQuery, 'ajax').and.callFake(function(req) { spyOn(axios, 'patch').and.callFake((url, data) => {
expect(req.type).toBe('PATCH'); expect(url).toBe(`${gl.TEST_HOST}/frontend-fixtures/merge-requests-project/merge_requests/1.json`);
expect(req.url).toBe('http://test.host/frontend-fixtures/merge-requests-project/merge_requests/1.json'); expect(data.note.note).not.toBe(null);
return expect(req.data.note).not.toBe(null); done();
return Promise.resolve({ data: {} });
}); });
$('.js-task-list-field.js-note-text').trigger('tasklist:changed'); $('.js-task-list-container').trigger('tasklist:changed');
}); });
}); });
......
...@@ -49,6 +49,11 @@ import Sidebar from '~/right_sidebar'; ...@@ -49,6 +49,11 @@ import Sidebar from '~/right_sidebar';
$toggle = $aside.find('.js-sidebar-toggle'); $toggle = $aside.find('.js-sidebar-toggle');
return $labelsIcon = $aside.find('.sidebar-collapsed-icon'); return $labelsIcon = $aside.find('.sidebar-collapsed-icon');
}); });
afterEach(() => {
mock.restore();
});
it('should expand/collapse the sidebar when arrow is clicked', function() { it('should expand/collapse the sidebar when arrow is clicked', function() {
assertSidebarState('expanded'); assertSidebarState('expanded');
$toggle.click(); $toggle.click();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment