Welcome to mirror list, hosted at ThFree Co, Russian Federation.

user_api_spec.js « api « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b2ecfeb839488b6227b1ad58b276cd5610694fbb (plain)
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
import MockAdapter from 'axios-mock-adapter';

import projects from 'test_fixtures/api/users/projects/get.json';
import followers from 'test_fixtures/api/users/followers/get.json';
import {
  followUser,
  unfollowUser,
  associationsCount,
  updateUserStatus,
  getUserProjects,
  getUserFollowers,
} from '~/api/user_api';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
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';
import { DEFAULT_PER_PAGE } from '~/api';

describe('~/api/user_api', () => {
  let axiosMock;

  beforeEach(() => {
    axiosMock = new MockAdapter(axios);

    window.gon = { api_version: 'v4' };
  });

  afterEach(() => {
    axiosMock.restore();
    axiosMock.resetHistory();
  });

  describe('followUser', () => {
    it('calls correct URL and returns expected response', async () => {
      const expectedUrl = '/api/v4/users/1/follow';
      const expectedResponse = { message: 'Success' };

      axiosMock.onPost(expectedUrl).replyOnce(HTTP_STATUS_OK, expectedResponse);

      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' };

      axiosMock.onPost(expectedUrl).replyOnce(HTTP_STATUS_OK, expectedResponse);

      await expect(unfollowUser(1)).resolves.toEqual(
        expect.objectContaining({ data: expectedResponse }),
      );
      expect(axiosMock.history.post[0].url).toBe(expectedUrl);
    });
  });

  describe('associationsCount', () => {
    it('calls correct URL and returns expected response', async () => {
      const expectedUrl = '/api/v4/users/1/associations_count';
      const expectedResponse = { data: associationsCountData };

      axiosMock.onGet(expectedUrl).replyOnce(HTTP_STATUS_OK, expectedResponse);

      await expect(associationsCount(1)).resolves.toEqual(
        expect.objectContaining({ data: expectedResponse }),
      );
      expect(axiosMock.history.get[0].url).toBe(expectedUrl);
    });
  });

  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 };

      axiosMock.onPatch(expectedUrl).replyOnce(HTTP_STATUS_OK, expectedResponse);

      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);
    });
  });

  describe('getUserProjects', () => {
    it('calls correct URL and returns expected response', async () => {
      const expectedUrl = '/api/v4/users/1/projects';
      const expectedResponse = { data: projects };

      axiosMock.onGet(expectedUrl).replyOnce(HTTP_STATUS_OK, expectedResponse);

      await expect(getUserProjects(1)).resolves.toEqual(
        expect.objectContaining({ data: expectedResponse }),
      );
      expect(axiosMock.history.get[0].url).toBe(expectedUrl);
    });
  });

  describe('getUserFollowers', () => {
    it('calls correct URL and returns expected response', async () => {
      const expectedUrl = '/api/v4/users/1/followers';
      const expectedResponse = { data: followers };
      const params = { page: 2 };

      axiosMock.onGet(expectedUrl).replyOnce(HTTP_STATUS_OK, expectedResponse);

      await expect(getUserFollowers(1, params)).resolves.toEqual(
        expect.objectContaining({ data: expectedResponse }),
      );
      expect(axiosMock.history.get[0].url).toBe(expectedUrl);
      expect(axiosMock.history.get[0].params).toEqual({ ...params, per_page: DEFAULT_PER_PAGE });
    });
  });
});