diff options
Diffstat (limited to 'spec/frontend/commit/components')
-rw-r--r-- | spec/frontend/commit/components/commit_refs_spec.js | 82 | ||||
-rw-r--r-- | spec/frontend/commit/components/refs_list_spec.js | 70 |
2 files changed, 152 insertions, 0 deletions
diff --git a/spec/frontend/commit/components/commit_refs_spec.js b/spec/frontend/commit/components/commit_refs_spec.js new file mode 100644 index 00000000000..7c35ff1969c --- /dev/null +++ b/spec/frontend/commit/components/commit_refs_spec.js @@ -0,0 +1,82 @@ +import { shallowMount } from '@vue/test-utils'; +import Vue from 'vue'; +import VueApollo from 'vue-apollo'; +import { createAlert } from '~/alert'; +import commitReferences from '~/projects/commit_box/info/graphql/queries/commit_references.query.graphql'; +import containingBranchesQuery from '~/projects/commit_box/info/graphql/queries/commit_containing_branches.query.graphql'; +import RefsList from '~/projects/commit_box/info/components/refs_list.vue'; +import createMockApollo from 'helpers/mock_apollo_helper'; +import waitForPromises from 'helpers/wait_for_promises'; +import { + FETCH_CONTAINING_REFS_EVENT, + FETCH_COMMIT_REFERENCES_ERROR, +} from '~/projects/commit_box/info/constants'; +import CommitRefs from '~/projects/commit_box/info/components/commit_refs.vue'; + +import { + mockCommitReferencesResponse, + mockOnlyBranchesResponse, + mockContainingBranchesResponse, + refsListPropsMock, +} from '../mock_data'; + +Vue.use(VueApollo); + +jest.mock('~/alert'); + +describe('Commit references component', () => { + let wrapper; + + const successQueryHandler = (mockResponse) => jest.fn().mockResolvedValue(mockResponse); + const failedQueryHandler = jest.fn().mockRejectedValue(new Error('GraphQL error')); + const containingBranchesQueryHandler = successQueryHandler(mockContainingBranchesResponse); + const findRefsLists = () => wrapper.findAllComponents(RefsList); + const branchesList = () => findRefsLists().at(0); + + const createComponent = async ( + commitReferencesQueryHandler = successQueryHandler(mockCommitReferencesResponse), + ) => { + wrapper = shallowMount(CommitRefs, { + apolloProvider: createMockApollo([ + [commitReferences, commitReferencesQueryHandler], + [containingBranchesQuery, containingBranchesQueryHandler], + ]), + provide: { + fullPath: '/some/project', + commitSha: 'xxx', + }, + }); + + await waitForPromises(); + }; + + it('renders component correcrly', async () => { + await createComponent(); + expect(findRefsLists()).toHaveLength(2); + }); + + it('passes props to refs list', async () => { + await createComponent(); + expect(branchesList().props()).toEqual(refsListPropsMock); + }); + + it('shows alert when response fails', async () => { + await createComponent(failedQueryHandler); + expect(createAlert).toHaveBeenCalledWith({ + message: FETCH_COMMIT_REFERENCES_ERROR, + captureError: true, + }); + }); + + it('fetches containing refs on the fetch event', async () => { + await createComponent(); + branchesList().vm.$emit(FETCH_CONTAINING_REFS_EVENT); + await waitForPromises(); + expect(containingBranchesQueryHandler).toHaveBeenCalledTimes(1); + }); + + it('does not render list when there is no branches or tags', async () => { + await createComponent(successQueryHandler(mockOnlyBranchesResponse)); + expect(findRefsLists()).toHaveLength(1); + }); +}); diff --git a/spec/frontend/commit/components/refs_list_spec.js b/spec/frontend/commit/components/refs_list_spec.js new file mode 100644 index 00000000000..d124f4a41b8 --- /dev/null +++ b/spec/frontend/commit/components/refs_list_spec.js @@ -0,0 +1,70 @@ +import { GlCollapse, GlButton, GlBadge, GlSkeletonLoader } from '@gitlab/ui'; +import RefsList from '~/projects/commit_box/info/components/refs_list.vue'; +import { shallowMountExtended } from 'helpers/vue_test_utils_helper'; +import { + CONTAINING_COMMIT, + FETCH_CONTAINING_REFS_EVENT, +} from '~/projects/commit_box/info/constants'; +import { refsListPropsMock, containingBranchesMock } from '../mock_data'; + +describe('Commit references component', () => { + let wrapper; + const createComponent = (props = {}) => { + wrapper = shallowMountExtended(RefsList, { + propsData: { + ...refsListPropsMock, + ...props, + }, + }); + }; + + const findTitle = () => wrapper.findByTestId('title'); + const findCollapseButton = () => wrapper.findComponent(GlButton); + const findSkeletonLoader = () => wrapper.findComponent(GlSkeletonLoader); + const findTippingRefs = () => wrapper.findAllComponents(GlBadge); + const findContainingRefs = () => wrapper.findComponent(GlCollapse); + + beforeEach(() => { + createComponent(); + }); + + it('renders the namespace passed', () => { + expect(findTitle().text()).toEqual(refsListPropsMock.namespace); + }); + + it('renders list of tipping branches or tags', () => { + expect(findTippingRefs()).toHaveLength(refsListPropsMock.tippingRefs.length); + }); + + it('does not render collapse with containing branches ot tags when there is no data', () => { + createComponent({ hasContainingRefs: false }); + expect(findCollapseButton().exists()).toBe(false); + }); + + it('renders collapse component if commit has containing branches', () => { + expect(findCollapseButton().text()).toContain(CONTAINING_COMMIT); + }); + + it('Emits event when collapse button is clicked', () => { + findCollapseButton().vm.$emit('click'); + expect(wrapper.emitted()[FETCH_CONTAINING_REFS_EVENT]).toHaveLength(1); + }); + + it('Renders the list of containing branches or tags when collapse is expanded', () => { + createComponent({ containingRefs: containingBranchesMock }); + const containingRefsList = findContainingRefs(); + expect(containingRefsList.findAllComponents(GlBadge)).toHaveLength( + containingBranchesMock.length, + ); + }); + + it('Does not reneder list of tipping branches or tags if there is no data', () => { + createComponent({ tippingRefs: [] }); + expect(findTippingRefs().exists()).toBe(false); + }); + + it('Renders skeleton loader when isLoading prop has true value', () => { + createComponent({ isLoading: true, containingRefs: [] }); + expect(findSkeletonLoader().exists()).toBe(true); + }); +}); |