diff options
Diffstat (limited to 'spec/frontend/api')
-rw-r--r-- | spec/frontend/api/tags_api_spec.js | 37 | ||||
-rw-r--r-- | spec/frontend/api/user_api_spec.js | 50 |
2 files changed, 87 insertions, 0 deletions
diff --git a/spec/frontend/api/tags_api_spec.js b/spec/frontend/api/tags_api_spec.js new file mode 100644 index 00000000000..a7436bf6a50 --- /dev/null +++ b/spec/frontend/api/tags_api_spec.js @@ -0,0 +1,37 @@ +import MockAdapter from 'axios-mock-adapter'; +import * as tagsApi from '~/api/tags_api'; +import axios from '~/lib/utils/axios_utils'; +import httpStatus from '~/lib/utils/http_status'; + +describe('~/api/tags_api.js', () => { + let mock; + let originalGon; + + const projectId = 1; + + beforeEach(() => { + mock = new MockAdapter(axios); + + originalGon = window.gon; + window.gon = { api_version: 'v7' }; + }); + + afterEach(() => { + mock.restore(); + window.gon = originalGon; + }); + + describe('getTag', () => { + it('fetches a tag of a given tag name of a particular project', () => { + const tagName = 'tag-name'; + const expectedUrl = `/api/v7/projects/${projectId}/repository/tags/${tagName}`; + mock.onGet(expectedUrl).reply(httpStatus.OK, { + name: tagName, + }); + + return tagsApi.getTag(projectId, tagName).then(({ data }) => { + expect(data.name).toBe(tagName); + }); + }); + }); +}); diff --git a/spec/frontend/api/user_api_spec.js b/spec/frontend/api/user_api_spec.js new file mode 100644 index 00000000000..ee7194bdf5f --- /dev/null +++ b/spec/frontend/api/user_api_spec.js @@ -0,0 +1,50 @@ +import MockAdapter from 'axios-mock-adapter'; + +import { followUser, unfollowUser } from '~/api/user_api'; +import axios from '~/lib/utils/axios_utils'; + +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' }; + + axiosMock.onPost(expectedUrl).replyOnce(200, 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(200, expectedResponse); + + await expect(unfollowUser(1)).resolves.toEqual( + expect.objectContaining({ data: expectedResponse }), + ); + expect(axiosMock.history.post[0].url).toBe(expectedUrl); + }); + }); +}); |