diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
commit | a09983ae35713f5a2bbb100981116d31ce99826e (patch) | |
tree | 2ee2af7bd104d57086db360a7e6d8c9d5d43667a /spec/frontend/ref/stores/actions_spec.js | |
parent | 18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff) |
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'spec/frontend/ref/stores/actions_spec.js')
-rw-r--r-- | spec/frontend/ref/stores/actions_spec.js | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/spec/frontend/ref/stores/actions_spec.js b/spec/frontend/ref/stores/actions_spec.js new file mode 100644 index 00000000000..32966354c95 --- /dev/null +++ b/spec/frontend/ref/stores/actions_spec.js @@ -0,0 +1,180 @@ +import testAction from 'helpers/vuex_action_helper'; +import createState from '~/ref/stores/state'; +import * as actions from '~/ref/stores/actions'; +import * as types from '~/ref/stores/mutation_types'; + +let mockBranchesReturnValue; +let mockTagsReturnValue; +let mockCommitReturnValue; + +jest.mock('~/api', () => ({ + // `__esModule: true` is required when mocking modules with default exports: + // https://jestjs.io/docs/en/jest-object#jestmockmodulename-factory-options + __esModule: true, + default: { + branches: () => mockBranchesReturnValue, + tags: () => mockTagsReturnValue, + commit: () => mockCommitReturnValue, + }, +})); + +describe('Ref selector Vuex store actions', () => { + let state; + + beforeEach(() => { + state = createState(); + }); + + describe('setProjectId', () => { + it(`commits ${types.SET_PROJECT_ID} with the new project ID`, () => { + const projectId = '4'; + testAction(actions.setProjectId, projectId, state, [ + { type: types.SET_PROJECT_ID, payload: projectId }, + ]); + }); + }); + + describe('setSelectedRef', () => { + it(`commits ${types.SET_SELECTED_REF} with the new selected ref name`, () => { + const selectedRef = 'v1.2.3'; + testAction(actions.setSelectedRef, selectedRef, state, [ + { type: types.SET_SELECTED_REF, payload: selectedRef }, + ]); + }); + }); + + describe('search', () => { + it(`commits ${types.SET_QUERY} with the new search query`, () => { + const query = 'hello'; + testAction( + actions.search, + query, + state, + [{ type: types.SET_QUERY, payload: query }], + [{ type: 'searchBranches' }, { type: 'searchTags' }, { type: 'searchCommits' }], + ); + }); + }); + + describe('searchBranches', () => { + describe('when the search is successful', () => { + const branchesApiResponse = { data: [{ name: 'my-feature-branch' }] }; + + beforeEach(() => { + mockBranchesReturnValue = Promise.resolve(branchesApiResponse); + }); + + it(`commits ${types.REQUEST_START}, ${types.RECEIVE_BRANCHES_SUCCESS} with the response from the API, and ${types.REQUEST_FINISH}`, () => { + return testAction(actions.searchBranches, undefined, state, [ + { type: types.REQUEST_START }, + { type: types.RECEIVE_BRANCHES_SUCCESS, payload: branchesApiResponse }, + { type: types.REQUEST_FINISH }, + ]); + }); + }); + + describe('when the search fails', () => { + const error = new Error('Something went wrong!'); + + beforeEach(() => { + mockBranchesReturnValue = Promise.reject(error); + }); + + it(`commits ${types.REQUEST_START}, ${types.RECEIVE_BRANCHES_ERROR} with the error object, and ${types.REQUEST_FINISH}`, () => { + return testAction(actions.searchBranches, undefined, state, [ + { type: types.REQUEST_START }, + { type: types.RECEIVE_BRANCHES_ERROR, payload: error }, + { type: types.REQUEST_FINISH }, + ]); + }); + }); + }); + + describe('searchTags', () => { + describe('when the search is successful', () => { + const tagsApiResponse = { data: [{ name: 'v1.2.3' }] }; + + beforeEach(() => { + mockTagsReturnValue = Promise.resolve(tagsApiResponse); + }); + + it(`commits ${types.REQUEST_START}, ${types.RECEIVE_TAGS_SUCCESS} with the response from the API, and ${types.REQUEST_FINISH}`, () => { + return testAction(actions.searchTags, undefined, state, [ + { type: types.REQUEST_START }, + { type: types.RECEIVE_TAGS_SUCCESS, payload: tagsApiResponse }, + { type: types.REQUEST_FINISH }, + ]); + }); + }); + + describe('when the search fails', () => { + const error = new Error('Something went wrong!'); + + beforeEach(() => { + mockTagsReturnValue = Promise.reject(error); + }); + + it(`commits ${types.REQUEST_START}, ${types.RECEIVE_TAGS_ERROR} with the error object, and ${types.REQUEST_FINISH}`, () => { + return testAction(actions.searchTags, undefined, state, [ + { type: types.REQUEST_START }, + { type: types.RECEIVE_TAGS_ERROR, payload: error }, + { type: types.REQUEST_FINISH }, + ]); + }); + }); + }); + + describe('searchCommits', () => { + describe('when the search query potentially matches a commit SHA', () => { + beforeEach(() => { + state.isQueryPossiblyASha = true; + }); + + describe('when the search is successful', () => { + const commitApiResponse = { data: [{ id: 'abcd1234' }] }; + + beforeEach(() => { + mockCommitReturnValue = Promise.resolve(commitApiResponse); + }); + + it(`commits ${types.REQUEST_START}, ${types.RECEIVE_COMMITS_SUCCESS} with the response from the API, and ${types.REQUEST_FINISH}`, () => { + return testAction(actions.searchCommits, undefined, state, [ + { type: types.REQUEST_START }, + { type: types.RECEIVE_COMMITS_SUCCESS, payload: commitApiResponse }, + { type: types.REQUEST_FINISH }, + ]); + }); + }); + + describe('when the search fails', () => { + const error = new Error('Something went wrong!'); + + beforeEach(() => { + mockCommitReturnValue = Promise.reject(error); + }); + + describe('when the search query might match a commit SHA', () => { + it(`commits ${types.REQUEST_START}, ${types.RECEIVE_COMMITS_ERROR} with the error object, and ${types.REQUEST_FINISH}`, () => { + return testAction(actions.searchCommits, undefined, state, [ + { type: types.REQUEST_START }, + { type: types.RECEIVE_COMMITS_ERROR, payload: error }, + { type: types.REQUEST_FINISH }, + ]); + }); + }); + }); + }); + + describe('when the search query will not match a commit SHA', () => { + beforeEach(() => { + state.isQueryPossiblyASha = false; + }); + + it(`commits ${types.RESET_COMMIT_MATCHES}`, () => { + return testAction(actions.searchCommits, undefined, state, [ + { type: types.RESET_COMMIT_MATCHES }, + ]); + }); + }); + }); +}); |