debian-mirror-gitlab/spec/frontend/api/user_api_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-07-16 23:28:13 +05:30
import MockAdapter from 'axios-mock-adapter';
2023-03-17 16:20:25 +05:30
import { followUser, unfollowUser, associationsCount, updateUserStatus } from '~/api/user_api';
2022-07-16 23:28:13 +05:30
import axios from '~/lib/utils/axios_utils';
2023-04-23 21:23:45 +05:30
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
2023-03-17 16:20:25 +05:30
import {
associationsCount as associationsCountData,
userStatus as mockUserStatus,
} from 'jest/admin/users/mock_data';
import { AVAILABILITY_STATUS } from '~/set_status_modal/constants';
import { timeRanges } from '~/vue_shared/constants';
2022-07-16 23:28:13 +05:30
describe('~/api/user_api', () => {
let axiosMock;
let originalGon;
beforeEach(() => {
axiosMock = new MockAdapter(axios);
originalGon = window.gon;
window.gon = { api_version: 'v4' };
});
afterEach(() => {
axiosMock.restore();
axiosMock.resetHistory();
window.gon = originalGon;
});
describe('followUser', () => {
it('calls correct URL and returns expected response', async () => {
const expectedUrl = '/api/v4/users/1/follow';
const expectedResponse = { message: 'Success' };
2023-04-23 21:23:45 +05:30
axiosMock.onPost(expectedUrl).replyOnce(HTTP_STATUS_OK, expectedResponse);
2022-07-16 23:28:13 +05:30
await expect(followUser(1)).resolves.toEqual(
expect.objectContaining({ data: expectedResponse }),
);
expect(axiosMock.history.post[0].url).toBe(expectedUrl);
});
});
describe('unfollowUser', () => {
it('calls correct URL and returns expected response', async () => {
const expectedUrl = '/api/v4/users/1/unfollow';
const expectedResponse = { message: 'Success' };
2023-04-23 21:23:45 +05:30
axiosMock.onPost(expectedUrl).replyOnce(HTTP_STATUS_OK, expectedResponse);
2022-07-16 23:28:13 +05:30
await expect(unfollowUser(1)).resolves.toEqual(
expect.objectContaining({ data: expectedResponse }),
);
expect(axiosMock.history.post[0].url).toBe(expectedUrl);
});
});
2023-01-13 00:05:48 +05:30
describe('associationsCount', () => {
it('calls correct URL and returns expected response', async () => {
const expectedUrl = '/api/v4/users/1/associations_count';
const expectedResponse = { data: associationsCountData };
2023-04-23 21:23:45 +05:30
axiosMock.onGet(expectedUrl).replyOnce(HTTP_STATUS_OK, expectedResponse);
2023-01-13 00:05:48 +05:30
await expect(associationsCount(1)).resolves.toEqual(
expect.objectContaining({ data: expectedResponse }),
);
expect(axiosMock.history.get[0].url).toBe(expectedUrl);
});
});
2023-03-17 16:20:25 +05:30
describe('updateUserStatus', () => {
it('calls correct URL and returns expected response', async () => {
const expectedUrl = '/api/v4/user/status';
const expectedData = {
emoji: 'basketball',
message: 'test',
availability: AVAILABILITY_STATUS.BUSY,
clear_status_after: timeRanges[0].shortcut,
};
const expectedResponse = { data: mockUserStatus };
2023-04-23 21:23:45 +05:30
axiosMock.onPatch(expectedUrl).replyOnce(HTTP_STATUS_OK, expectedResponse);
2023-03-17 16:20:25 +05:30
await expect(
updateUserStatus({
emoji: 'basketball',
message: 'test',
availability: AVAILABILITY_STATUS.BUSY,
clearStatusAfter: timeRanges[0].shortcut,
}),
).resolves.toEqual(expect.objectContaining({ data: expectedResponse }));
expect(axiosMock.history.patch[0].url).toBe(expectedUrl);
expect(JSON.parse(axiosMock.history.patch[0].data)).toEqual(expectedData);
});
});
2022-07-16 23:28:13 +05:30
});