2021-04-08 15:09:06 +00:00
|
|
|
import * as Sentry from '@sentry/browser';
|
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import testAction from 'helpers/vuex_action_helper';
|
|
|
|
import * as actions from '~/emoji/awards_app/store/actions';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
|
|
|
|
jest.mock('@sentry/browser');
|
2021-06-24 09:07:22 +00:00
|
|
|
jest.mock('~/vue_shared/plugins/global_toast');
|
2021-04-08 15:09:06 +00:00
|
|
|
|
|
|
|
describe('Awards app actions', () => {
|
2021-05-27 06:10:47 +00:00
|
|
|
afterEach(() => {
|
|
|
|
window.gon = {};
|
|
|
|
});
|
|
|
|
|
2021-04-08 15:09:06 +00:00
|
|
|
describe('setInitialData', () => {
|
|
|
|
it('commits SET_INITIAL_DATA', async () => {
|
|
|
|
await testAction(
|
|
|
|
actions.setInitialData,
|
|
|
|
{ path: 'https://gitlab.com' },
|
|
|
|
{},
|
|
|
|
[{ type: 'SET_INITIAL_DATA', payload: { path: 'https://gitlab.com' } }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchAwards', () => {
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('success', () => {
|
2021-06-08 15:10:00 +00:00
|
|
|
describe.each`
|
|
|
|
relativeRootUrl
|
|
|
|
${null}
|
|
|
|
${'/gitlab'}
|
|
|
|
`('with relative_root_url as $relativeRootUrl', ({ relativeRootUrl }) => {
|
|
|
|
beforeEach(() => {
|
|
|
|
window.gon = { relative_url_root: relativeRootUrl };
|
|
|
|
mock
|
|
|
|
.onGet(`${relativeRootUrl || ''}/awards`, { params: { per_page: 100, page: '1' } })
|
|
|
|
.reply(200, ['thumbsup'], { 'x-next-page': '2' });
|
|
|
|
mock
|
|
|
|
.onGet(`${relativeRootUrl || ''}/awards`, { params: { per_page: 100, page: '2' } })
|
|
|
|
.reply(200, ['thumbsdown']);
|
|
|
|
});
|
2021-04-08 15:09:06 +00:00
|
|
|
|
2021-06-08 15:10:00 +00:00
|
|
|
it('commits FETCH_AWARDS_SUCCESS', async () => {
|
|
|
|
window.gon.current_user_id = 1;
|
2021-05-27 06:10:47 +00:00
|
|
|
|
2021-06-08 15:10:00 +00:00
|
|
|
await testAction(
|
|
|
|
actions.fetchAwards,
|
|
|
|
'1',
|
|
|
|
{ path: '/awards' },
|
|
|
|
[{ type: 'FETCH_AWARDS_SUCCESS', payload: ['thumbsup'] }],
|
|
|
|
[{ type: 'fetchAwards', payload: '2' }],
|
|
|
|
);
|
|
|
|
});
|
2021-05-27 06:10:47 +00:00
|
|
|
|
2021-06-08 15:10:00 +00:00
|
|
|
it('does not commit FETCH_AWARDS_SUCCESS when user signed out', async () => {
|
|
|
|
await testAction(actions.fetchAwards, '1', { path: '/awards' }, [], []);
|
|
|
|
});
|
2021-05-27 06:10:47 +00:00
|
|
|
});
|
2021-04-08 15:09:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('error', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mock.onGet('/awards').reply(500);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls Sentry.captureException', async () => {
|
2021-05-27 06:10:47 +00:00
|
|
|
window.gon = { current_user_id: 1 };
|
|
|
|
|
2021-04-08 15:09:06 +00:00
|
|
|
await testAction(actions.fetchAwards, null, { path: '/awards' }, [], [], () => {
|
|
|
|
expect(Sentry.captureException).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('toggleAward', () => {
|
|
|
|
let mock;
|
|
|
|
|
2022-02-02 03:17:34 +00:00
|
|
|
const optimisticAwardId = Number.MAX_SAFE_INTEGER - 1;
|
|
|
|
const makeOptimisticAddMutation = (
|
|
|
|
id = optimisticAwardId,
|
|
|
|
name = null,
|
|
|
|
userId = window.gon.current_user_id,
|
|
|
|
) => ({
|
|
|
|
type: 'ADD_NEW_AWARD',
|
|
|
|
payload: {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
user: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const makeOptimisticRemoveMutation = (id = optimisticAwardId) => ({
|
|
|
|
type: 'REMOVE_AWARD',
|
|
|
|
payload: id,
|
|
|
|
});
|
|
|
|
|
2021-04-08 15:09:06 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
2021-06-08 15:10:00 +00:00
|
|
|
describe.each`
|
|
|
|
relativeRootUrl
|
|
|
|
${null}
|
|
|
|
${'/gitlab'}
|
|
|
|
`('with relative_root_url as $relativeRootUrl', ({ relativeRootUrl }) => {
|
|
|
|
beforeEach(() => {
|
|
|
|
window.gon = { relative_url_root: relativeRootUrl };
|
2021-04-08 15:09:06 +00:00
|
|
|
});
|
|
|
|
|
2021-06-08 15:10:00 +00:00
|
|
|
describe('adding new award', () => {
|
|
|
|
describe('success', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mock.onPost(`${relativeRootUrl || ''}/awards`).reply(200, { id: 1 });
|
|
|
|
});
|
|
|
|
|
2022-02-02 03:17:34 +00:00
|
|
|
it('adds an optimistic award, removes it, and then commits ADD_NEW_AWARD', async () => {
|
2021-06-08 15:10:00 +00:00
|
|
|
testAction(actions.toggleAward, null, { path: '/awards', awards: [] }, [
|
2022-02-02 03:17:34 +00:00
|
|
|
makeOptimisticAddMutation(),
|
|
|
|
makeOptimisticRemoveMutation(),
|
2021-06-08 15:10:00 +00:00
|
|
|
{ type: 'ADD_NEW_AWARD', payload: { id: 1 } },
|
|
|
|
]);
|
|
|
|
});
|
2021-04-08 15:09:06 +00:00
|
|
|
});
|
|
|
|
|
2021-06-08 15:10:00 +00:00
|
|
|
describe('error', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mock.onPost(`${relativeRootUrl || ''}/awards`).reply(500);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls Sentry.captureException', async () => {
|
|
|
|
await testAction(
|
|
|
|
actions.toggleAward,
|
|
|
|
null,
|
|
|
|
{ path: '/awards', awards: [] },
|
2022-02-02 03:17:34 +00:00
|
|
|
[makeOptimisticAddMutation(), makeOptimisticRemoveMutation()],
|
2021-06-08 15:10:00 +00:00
|
|
|
[],
|
|
|
|
() => {
|
|
|
|
expect(Sentry.captureException).toHaveBeenCalled();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2021-04-08 15:09:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-02 03:17:34 +00:00
|
|
|
describe('removing an award', () => {
|
2021-06-08 15:10:00 +00:00
|
|
|
const mockData = { id: 1, name: 'thumbsup', user: { id: 1 } };
|
|
|
|
|
|
|
|
describe('success', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mock.onDelete(`${relativeRootUrl || ''}/awards/1`).reply(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('commits REMOVE_AWARD', async () => {
|
|
|
|
testAction(
|
|
|
|
actions.toggleAward,
|
|
|
|
'thumbsup',
|
|
|
|
{
|
|
|
|
path: '/awards',
|
|
|
|
currentUserId: 1,
|
|
|
|
awards: [mockData],
|
|
|
|
},
|
|
|
|
[{ type: 'REMOVE_AWARD', payload: 1 }],
|
|
|
|
);
|
|
|
|
});
|
2021-04-08 15:09:06 +00:00
|
|
|
});
|
|
|
|
|
2021-06-08 15:10:00 +00:00
|
|
|
describe('error', () => {
|
2022-02-02 03:17:34 +00:00
|
|
|
const currentUserId = 1;
|
|
|
|
const name = 'thumbsup';
|
|
|
|
|
2021-06-08 15:10:00 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
mock.onDelete(`${relativeRootUrl || ''}/awards/1`).reply(500);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls Sentry.captureException', async () => {
|
|
|
|
await testAction(
|
|
|
|
actions.toggleAward,
|
2022-02-02 03:17:34 +00:00
|
|
|
name,
|
2021-06-08 15:10:00 +00:00
|
|
|
{
|
|
|
|
path: '/awards',
|
2022-02-02 03:17:34 +00:00
|
|
|
currentUserId,
|
2021-06-08 15:10:00 +00:00
|
|
|
awards: [mockData],
|
|
|
|
},
|
2022-02-02 03:17:34 +00:00
|
|
|
[makeOptimisticRemoveMutation(1), makeOptimisticAddMutation(1, name, currentUserId)],
|
2021-06-08 15:10:00 +00:00
|
|
|
[],
|
|
|
|
() => {
|
|
|
|
expect(Sentry.captureException).toHaveBeenCalled();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2021-04-08 15:09:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|