Commit a5698e3f authored by Nicolò Maria Mezzopera's avatar Nicolò Maria Mezzopera

Merge branch 'design-management-update-todo-counter' into 'master'

Update global To-Do counter when Design Management To-Do toggled

See merge request gitlab-org/gitlab!41960
parents e550811c 738a7a9f
...@@ -52,7 +52,7 @@ export default { ...@@ -52,7 +52,7 @@ export default {
}, },
methods: { methods: {
updateToDoCount(add) { updateToDoCount(add) {
const oldCount = parseInt(document.querySelector('.todos-count').innerText, 10); const oldCount = parseInt(document.querySelector('.js-todos-count').innerText, 10);
const count = add ? oldCount + 1 : oldCount - 1; const count = add ? oldCount + 1 : oldCount - 1;
const headerTodoEvent = new CustomEvent('todo:toggle', { const headerTodoEvent = new CustomEvent('todo:toggle', {
detail: { detail: {
......
...@@ -60,6 +60,22 @@ export default { ...@@ -60,6 +60,22 @@ export default {
}, },
}, },
methods: { methods: {
updateGlobalTodoCount(additionalTodoCount) {
const currentCount = parseInt(document.querySelector('.js-todos-count').innerText, 10);
const todoToggleEvent = new CustomEvent('todo:toggle', {
detail: {
count: Math.max(currentCount + additionalTodoCount, 0),
},
});
document.dispatchEvent(todoToggleEvent);
},
incrementGlobalTodoCount() {
this.updateGlobalTodoCount(1);
},
decrementGlobalTodoCount() {
this.updateGlobalTodoCount(-1);
},
createTodo() { createTodo() {
this.todoLoading = true; this.todoLoading = true;
return this.$apollo return this.$apollo
...@@ -76,6 +92,9 @@ export default { ...@@ -76,6 +92,9 @@ export default {
} }
}, },
}) })
.then(() => {
this.incrementGlobalTodoCount();
})
.catch(err => { .catch(err => {
this.$emit('error', Error(CREATE_DESIGN_TODO_ERROR)); this.$emit('error', Error(CREATE_DESIGN_TODO_ERROR));
throw err; throw err;
...@@ -116,6 +135,9 @@ export default { ...@@ -116,6 +135,9 @@ export default {
} }
}, },
}) })
.then(() => {
this.decrementGlobalTodoCount();
})
.catch(err => { .catch(err => {
this.$emit('error', Error(DELETE_DESIGN_TODO_ERROR)); this.$emit('error', Error(DELETE_DESIGN_TODO_ERROR));
throw err; throw err;
......
...@@ -14,7 +14,7 @@ import Tracking from '~/tracking'; ...@@ -14,7 +14,7 @@ import Tracking from '~/tracking';
export default function initTodoToggle() { export default function initTodoToggle() {
$(document).on('todo:toggle', (e, count) => { $(document).on('todo:toggle', (e, count) => {
const updatedCount = count || e?.detail?.count || 0; const updatedCount = count || e?.detail?.count || 0;
const $todoPendingCount = $('.todos-count'); const $todoPendingCount = $('.js-todos-count');
$todoPendingCount.text(highCountTrim(updatedCount)); $todoPendingCount.text(highCountTrim(updatedCount));
$todoPendingCount.toggleClass('hidden', updatedCount === 0); $todoPendingCount.toggleClass('hidden', updatedCount === 0);
......
...@@ -66,7 +66,7 @@ ...@@ -66,7 +66,7 @@
track_property: 'navigation', track_property: 'navigation',
container: 'body' } do container: 'body' } do
= sprite_icon('todo-done') = sprite_icon('todo-done')
%span.badge.badge-pill.todos-count{ class: ('hidden' if todos_pending_count == 0) } %span.badge.badge-pill.todos-count.js-todos-count{ class: ('hidden' if todos_pending_count == 0) }
= todos_count_format(todos_pending_count) = todos_count_format(todos_pending_count)
%li.nav-item.header-help.dropdown.d-none.d-md-block{ **tracking_attrs('main_navigation', 'click_question_mark_link', 'navigation') } %li.nav-item.header-help.dropdown.d-none.d-md-block{ **tracking_attrs('main_navigation', 'click_question_mark_link', 'navigation') }
= link_to help_path, class: 'header-help-dropdown-toggle', data: { toggle: "dropdown" } do = link_to help_path, class: 'header-help-dropdown-toggle', data: { toggle: "dropdown" } do
......
...@@ -52,6 +52,7 @@ describe('Design management design todo button', () => { ...@@ -52,6 +52,7 @@ describe('Design management design todo button', () => {
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
wrapper = null; wrapper = null;
jest.clearAllMocks();
}); });
it('renders TodoButton component', () => { it('renders TodoButton component', () => {
...@@ -68,7 +69,14 @@ describe('Design management design todo button', () => { ...@@ -68,7 +69,14 @@ describe('Design management design todo button', () => {
}); });
describe('when clicked', () => { describe('when clicked', () => {
let dispatchEventSpy;
beforeEach(() => { beforeEach(() => {
dispatchEventSpy = jest.spyOn(document, 'dispatchEvent');
jest.spyOn(document, 'querySelector').mockReturnValue({
innerText: 2,
});
createComponent({ design: mockDesignWithPendingTodos }, { mountFn: mount }); createComponent({ design: mockDesignWithPendingTodos }, { mountFn: mount });
wrapper.trigger('click'); wrapper.trigger('click');
return wrapper.vm.$nextTick(); return wrapper.vm.$nextTick();
...@@ -86,6 +94,14 @@ describe('Design management design todo button', () => { ...@@ -86,6 +94,14 @@ describe('Design management design todo button', () => {
expect(mutate).toHaveBeenCalledTimes(1); expect(mutate).toHaveBeenCalledTimes(1);
expect(mutate).toHaveBeenCalledWith(todoMarkDoneMutationVariables); expect(mutate).toHaveBeenCalledWith(todoMarkDoneMutationVariables);
}); });
it('calls dispatchDocumentEvent to update global To-Do counter correctly', () => {
const dispatchedEvent = dispatchEventSpy.mock.calls[0][0];
expect(dispatchEventSpy).toHaveBeenCalledTimes(1);
expect(dispatchedEvent.detail).toEqual({ count: 1 });
expect(dispatchedEvent.type).toBe('todo:toggle');
});
}); });
}); });
...@@ -99,7 +115,14 @@ describe('Design management design todo button', () => { ...@@ -99,7 +115,14 @@ describe('Design management design todo button', () => {
}); });
describe('when clicked', () => { describe('when clicked', () => {
let dispatchEventSpy;
beforeEach(() => { beforeEach(() => {
dispatchEventSpy = jest.spyOn(document, 'dispatchEvent');
jest.spyOn(document, 'querySelector').mockReturnValue({
innerText: 2,
});
createComponent({}, { mountFn: mount }); createComponent({}, { mountFn: mount });
wrapper.trigger('click'); wrapper.trigger('click');
return wrapper.vm.$nextTick(); return wrapper.vm.$nextTick();
...@@ -122,6 +145,14 @@ describe('Design management design todo button', () => { ...@@ -122,6 +145,14 @@ describe('Design management design todo button', () => {
expect(mutate).toHaveBeenCalledTimes(1); expect(mutate).toHaveBeenCalledTimes(1);
expect(mutate).toHaveBeenCalledWith(createDesignTodoMutationVariables); expect(mutate).toHaveBeenCalledWith(createDesignTodoMutationVariables);
}); });
it('calls dispatchDocumentEvent to update global To-Do counter correctly', () => {
const dispatchedEvent = dispatchEventSpy.mock.calls[0][0];
expect(dispatchEventSpy).toHaveBeenCalledTimes(1);
expect(dispatchedEvent.detail).toEqual({ count: 3 });
expect(dispatchedEvent.type).toBe('todo:toggle');
});
}); });
}); });
}); });
...@@ -4,7 +4,7 @@ import initTodoToggle, { initNavUserDropdownTracking } from '~/header'; ...@@ -4,7 +4,7 @@ import initTodoToggle, { initNavUserDropdownTracking } from '~/header';
describe('Header', () => { describe('Header', () => {
describe('Todos notification', () => { describe('Todos notification', () => {
const todosPendingCount = '.todos-count'; const todosPendingCount = '.js-todos-count';
const fixtureTemplate = 'issues/open-issue.html'; const fixtureTemplate = 'issues/open-issue.html';
function isTodosCountHidden() { function isTodosCountHidden() {
......
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