2022-05-13 08:08:49 -04:00
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2021-02-14 13:09:20 -05:00
|
|
|
import * as UserApi from '~/api/user_api';
|
2019-07-09 04:44:19 -04:00
|
|
|
import {
|
|
|
|
openUserCountsBroadcast,
|
|
|
|
closeUserCountsBroadcast,
|
|
|
|
refreshUserMergeRequestCounts,
|
|
|
|
} from '~/commons/nav/user_merge_requests';
|
|
|
|
|
|
|
|
jest.mock('~/api');
|
|
|
|
|
|
|
|
const TEST_COUNT = 1000;
|
2021-01-04 19:10:20 -05:00
|
|
|
const MR_COUNT_CLASS = 'js-merge-requests-count';
|
2019-07-09 04:44:19 -04:00
|
|
|
|
|
|
|
describe('User Merge Requests', () => {
|
|
|
|
let channelMock;
|
|
|
|
let newBroadcastChannelMock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
global.gon.current_user_id = 123;
|
|
|
|
|
|
|
|
channelMock = {
|
|
|
|
postMessage: jest.fn(),
|
|
|
|
close: jest.fn(),
|
|
|
|
};
|
|
|
|
newBroadcastChannelMock = jest.fn().mockImplementation(() => channelMock);
|
|
|
|
|
|
|
|
global.BroadcastChannel = newBroadcastChannelMock;
|
2022-05-13 08:08:49 -04:00
|
|
|
setHTMLFixture(
|
2021-01-04 19:10:20 -05:00
|
|
|
`<div><div class="${MR_COUNT_CLASS}">0</div><div class="js-assigned-mr-count"></div><div class="js-reviewer-mr-count"></div></div>`,
|
|
|
|
);
|
2019-07-09 04:44:19 -04:00
|
|
|
});
|
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2019-07-09 04:44:19 -04:00
|
|
|
const findMRCountText = () => document.body.querySelector(`.${MR_COUNT_CLASS}`).textContent;
|
|
|
|
|
|
|
|
describe('refreshUserMergeRequestCounts', () => {
|
|
|
|
beforeEach(() => {
|
2021-01-14 10:10:46 -05:00
|
|
|
jest.spyOn(UserApi, 'getUserCounts').mockResolvedValue({
|
|
|
|
data: {
|
|
|
|
assigned_merge_requests: TEST_COUNT,
|
|
|
|
review_requested_merge_requests: TEST_COUNT,
|
|
|
|
},
|
|
|
|
});
|
2019-07-09 04:44:19 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('with open broadcast channel', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
openUserCountsBroadcast();
|
|
|
|
|
|
|
|
return refreshUserMergeRequestCounts();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates the top count of merge requests', () => {
|
2021-01-04 19:10:20 -05:00
|
|
|
expect(findMRCountText()).toEqual(Number(TEST_COUNT + TEST_COUNT).toLocaleString());
|
2019-07-09 04:44:19 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calls the API', () => {
|
2021-01-14 10:10:46 -05:00
|
|
|
expect(UserApi.getUserCounts).toHaveBeenCalled();
|
2019-07-09 04:44:19 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('posts count to BroadcastChannel', () => {
|
2021-01-04 19:10:20 -05:00
|
|
|
expect(channelMock.postMessage).toHaveBeenCalledWith(TEST_COUNT + TEST_COUNT);
|
2019-07-09 04:44:19 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('without open broadcast channel', () => {
|
|
|
|
beforeEach(() => refreshUserMergeRequestCounts());
|
|
|
|
|
|
|
|
it('does not post anything', () => {
|
|
|
|
expect(channelMock.postMessage).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(findMRCountText()).toEqual(TEST_COUNT.toLocaleString());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('closes if called while already open', () => {
|
|
|
|
expect(channelMock.close).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
openUserCountsBroadcast();
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|