Commit 738a7a9f authored by Tom Quirk's avatar Tom Quirk

Call dispatchEvent directly for design todos

Addessing reviewer feedback. Tests have been updated
to mock and check dispatchEvent appropriately
parent e74df4c3
...@@ -5,7 +5,7 @@ import createDesignTodoMutation from '../graphql/mutations/create_design_todo.mu ...@@ -5,7 +5,7 @@ import createDesignTodoMutation from '../graphql/mutations/create_design_todo.mu
import TodoButton from '~/vue_shared/components/todo_button.vue'; import TodoButton from '~/vue_shared/components/todo_button.vue';
import allVersionsMixin from '../mixins/all_versions'; import allVersionsMixin from '../mixins/all_versions';
import { updateStoreAfterDeleteDesignTodo } from '../utils/cache_update'; import { updateStoreAfterDeleteDesignTodo } from '../utils/cache_update';
import { findIssueId, dispatchDocumentEvent } from '../utils/design_management_utils'; import { findIssueId } from '../utils/design_management_utils';
import { CREATE_DESIGN_TODO_ERROR, DELETE_DESIGN_TODO_ERROR } from '../utils/error_messages'; import { CREATE_DESIGN_TODO_ERROR, DELETE_DESIGN_TODO_ERROR } from '../utils/error_messages';
export default { export default {
...@@ -61,12 +61,13 @@ export default { ...@@ -61,12 +61,13 @@ export default {
methods: { methods: {
updateGlobalTodoCount(additionalTodoCount) { updateGlobalTodoCount(additionalTodoCount) {
const currentCount = parseInt(document.querySelector('.js-todos-count').innerText, 10); const currentCount = parseInt(document.querySelector('.js-todos-count').innerText, 10);
const todoToggleEvent = new CustomEvent('todo:toggle', {
dispatchDocumentEvent('todo:toggle', {
detail: { detail: {
count: Math.max(currentCount + additionalTodoCount, 0), count: Math.max(currentCount + additionalTodoCount, 0),
}, },
}); });
document.dispatchEvent(todoToggleEvent);
}, },
incrementGlobalTodoCount() { incrementGlobalTodoCount() {
this.updateGlobalTodoCount(1); this.updateGlobalTodoCount(1);
......
...@@ -167,12 +167,3 @@ export const createPendingTodo = todoId => { ...@@ -167,12 +167,3 @@ export const createPendingTodo = todoId => {
id: createTodoGid(todoId), id: createTodoGid(todoId),
}; };
}; };
/**
* Dispatch an event on the document
* @param {String} eventName
* @param {Object} eventData
*/
export const dispatchDocumentEvent = (eventName, eventData) => {
document.dispatchEvent(new CustomEvent(eventName, eventData));
};
...@@ -4,7 +4,6 @@ import DesignTodoButton from '~/design_management/components/design_todo_button. ...@@ -4,7 +4,6 @@ import DesignTodoButton from '~/design_management/components/design_todo_button.
import createDesignTodoMutation from '~/design_management/graphql/mutations/create_design_todo.mutation.graphql'; import createDesignTodoMutation from '~/design_management/graphql/mutations/create_design_todo.mutation.graphql';
import todoMarkDoneMutation from '~/graphql_shared/mutations/todo_mark_done.mutation.graphql'; import todoMarkDoneMutation from '~/graphql_shared/mutations/todo_mark_done.mutation.graphql';
import mockDesign from '../mock_data/design'; import mockDesign from '../mock_data/design';
import * as utils from '~/design_management/utils/design_management_utils';
const mockDesignWithPendingTodos = { const mockDesignWithPendingTodos = {
...mockDesign, ...mockDesign,
...@@ -70,8 +69,10 @@ describe('Design management design todo button', () => { ...@@ -70,8 +69,10 @@ describe('Design management design todo button', () => {
}); });
describe('when clicked', () => { describe('when clicked', () => {
let dispatchEventSpy;
beforeEach(() => { beforeEach(() => {
utils.dispatchDocumentEvent = jest.fn(); dispatchEventSpy = jest.spyOn(document, 'dispatchEvent');
jest.spyOn(document, 'querySelector').mockReturnValue({ jest.spyOn(document, 'querySelector').mockReturnValue({
innerText: 2, innerText: 2,
}); });
...@@ -95,12 +96,11 @@ describe('Design management design todo button', () => { ...@@ -95,12 +96,11 @@ describe('Design management design todo button', () => {
}); });
it('calls dispatchDocumentEvent to update global To-Do counter correctly', () => { it('calls dispatchDocumentEvent to update global To-Do counter correctly', () => {
expect(utils.dispatchDocumentEvent).toHaveBeenCalledTimes(1); const dispatchedEvent = dispatchEventSpy.mock.calls[0][0];
expect(utils.dispatchDocumentEvent).toHaveBeenCalledWith('todo:toggle', {
detail: { expect(dispatchEventSpy).toHaveBeenCalledTimes(1);
count: 1, expect(dispatchedEvent.detail).toEqual({ count: 1 });
}, expect(dispatchedEvent.type).toBe('todo:toggle');
});
}); });
}); });
}); });
...@@ -115,8 +115,10 @@ describe('Design management design todo button', () => { ...@@ -115,8 +115,10 @@ describe('Design management design todo button', () => {
}); });
describe('when clicked', () => { describe('when clicked', () => {
let dispatchEventSpy;
beforeEach(() => { beforeEach(() => {
utils.dispatchDocumentEvent = jest.fn(); dispatchEventSpy = jest.spyOn(document, 'dispatchEvent');
jest.spyOn(document, 'querySelector').mockReturnValue({ jest.spyOn(document, 'querySelector').mockReturnValue({
innerText: 2, innerText: 2,
}); });
...@@ -144,12 +146,11 @@ describe('Design management design todo button', () => { ...@@ -144,12 +146,11 @@ describe('Design management design todo button', () => {
}); });
it('calls dispatchDocumentEvent to update global To-Do counter correctly', () => { it('calls dispatchDocumentEvent to update global To-Do counter correctly', () => {
expect(utils.dispatchDocumentEvent).toHaveBeenCalledTimes(1); const dispatchedEvent = dispatchEventSpy.mock.calls[0][0];
expect(utils.dispatchDocumentEvent).toHaveBeenCalledWith('todo:toggle', {
detail: { expect(dispatchEventSpy).toHaveBeenCalledTimes(1);
count: 3, expect(dispatchedEvent.detail).toEqual({ count: 3 });
}, expect(dispatchedEvent.type).toBe('todo:toggle');
});
}); });
}); });
}); });
......
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