1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import * as UserApi from '~/api/user_api';
import {
openUserCountsBroadcast,
closeUserCountsBroadcast,
refreshUserMergeRequestCounts,
} from '~/commons/nav/user_merge_requests';
jest.mock('~/api');
const TEST_COUNT = 1000;
const MR_COUNT_CLASS = 'js-merge-requests-count';
describe('User Merge Requests', () => {
let channelMock;
let newBroadcastChannelMock;
beforeEach(() => {
jest.spyOn(document, 'dispatchEvent').mockReturnValue(false);
global.gon.current_user_id = 123;
global.gon.use_new_navigation = false;
channelMock = {
postMessage: jest.fn(),
close: jest.fn(),
};
newBroadcastChannelMock = jest.fn().mockImplementation(() => channelMock);
global.BroadcastChannel = newBroadcastChannelMock;
setHTMLFixture(
`<div><div class="${MR_COUNT_CLASS}">0</div><div class="js-assigned-mr-count"></div><div class="js-reviewer-mr-count"></div></div>`,
);
});
afterEach(() => {
resetHTMLFixture();
});
const findMRCountText = () => document.body.querySelector(`.${MR_COUNT_CLASS}`).textContent;
describe('refreshUserMergeRequestCounts', () => {
beforeEach(() => {
jest.spyOn(UserApi, 'getUserCounts').mockResolvedValue({
data: {
assigned_merge_requests: TEST_COUNT,
review_requested_merge_requests: TEST_COUNT,
},
});
});
describe('with open broadcast channel', () => {
beforeEach(() => {
openUserCountsBroadcast();
return refreshUserMergeRequestCounts();
});
it('updates the top count of merge requests', () => {
expect(findMRCountText()).toEqual(Number(TEST_COUNT + TEST_COUNT).toLocaleString());
});
it('calls the API', () => {
expect(UserApi.getUserCounts).toHaveBeenCalled();
});
it('posts count to BroadcastChannel', () => {
expect(channelMock.postMessage).toHaveBeenCalledWith(TEST_COUNT + TEST_COUNT);
});
});
describe('without open broadcast channel', () => {
beforeEach(() => refreshUserMergeRequestCounts());
it('does not post anything', () => {
expect(channelMock.postMessage).not.toHaveBeenCalled();
});
});
it('does not emit event to refetch counts', () => {
expect(document.dispatchEvent).not.toHaveBeenCalled();
});
});
describe('openUserCountsBroadcast', () => {
beforeEach(() => {
openUserCountsBroadcast();
});
it('creates BroadcastChannel that updates DOM on message received', () => {
expect(findMRCountText()).toEqual('0');
channelMock.onmessage({ data: TEST_COUNT });
expect(newBroadcastChannelMock).toHaveBeenCalled();
expect(findMRCountText()).toEqual(TEST_COUNT.toLocaleString());
});
it('closes if called while already open', () => {
expect(channelMock.close).not.toHaveBeenCalled();
openUserCountsBroadcast();
expect(newBroadcastChannelMock).toHaveBeenCalled();
expect(channelMock.close).toHaveBeenCalled();
});
});
describe('closeUserCountsBroadcast', () => {
describe('when not opened', () => {
it('does nothing', () => {
expect(channelMock.close).not.toHaveBeenCalled();
});
});
describe('when opened', () => {
beforeEach(() => {
openUserCountsBroadcast();
});
it('closes', () => {
expect(channelMock.close).not.toHaveBeenCalled();
closeUserCountsBroadcast();
expect(channelMock.close).toHaveBeenCalled();
});
});
});
describe('if new navigation is enabled', () => {
beforeEach(() => {
global.gon.use_new_navigation = true;
jest.spyOn(UserApi, 'getUserCounts');
});
it('openUserCountsBroadcast is a noop', () => {
openUserCountsBroadcast();
expect(newBroadcastChannelMock).not.toHaveBeenCalled();
});
describe('refreshUserMergeRequestCounts', () => {
it('does not call api', async () => {
await refreshUserMergeRequestCounts();
expect(UserApi.getUserCounts).not.toHaveBeenCalled();
});
it('emits event to refetch counts', async () => {
await refreshUserMergeRequestCounts();
expect(document.dispatchEvent).toHaveBeenCalledWith(new CustomEvent('todo:toggle'));
});
});
});
});
|