Commit 2023f9f1 authored by Scott Hampton's avatar Scott Hampton

Refactor spec files

Refactor the spec files to reduce `beforeEach`
functions and calls to `createComponent`.
parent 767b6af1
...@@ -25,7 +25,7 @@ export default { ...@@ -25,7 +25,7 @@ export default {
<template> <template>
<div> <div>
<h4 class="sub-header" data-testid="test-coverage-header"> <h4 data-testid="test-coverage-header">
{{ $options.text.codeCoverageHeader }} {{ $options.text.codeCoverageHeader }}
</h4> </h4>
<download-test-coverage <download-test-coverage
......
...@@ -28,13 +28,14 @@ describe('Download test coverage component', () => { ...@@ -28,13 +28,14 @@ describe('Download test coverage component', () => {
groupFullPath: 'gitlab-org', groupFullPath: 'gitlab-org',
}; };
const createComponent = (data = {}) => { const createComponent = () => {
wrapper = shallowMount(DownloadTestCoverage, { wrapper = shallowMount(DownloadTestCoverage, {
localVue, localVue,
data() { data() {
return { return {
hasError: false, hasError: false,
...data, allProjectsSelected: false,
selectedProjectIds: [],
}; };
}, },
propsData: { propsData: {
...@@ -63,12 +64,12 @@ describe('Download test coverage component', () => { ...@@ -63,12 +64,12 @@ describe('Download test coverage component', () => {
}); });
describe('when there is an error fetching the projects', () => { describe('when there is an error fetching the projects', () => {
beforeEach(() => {
createComponent({ hasError: true });
});
it('displays an alert for the failed query', () => { it('displays an alert for the failed query', () => {
expect(findAlert().exists()).toBe(true); wrapper.setData({ hasError: true });
return wrapper.vm.$nextTick().then(() => {
expect(findAlert().exists()).toBe(true);
});
}); });
}); });
...@@ -78,59 +79,50 @@ describe('Download test coverage component', () => { ...@@ -78,59 +79,50 @@ describe('Download test coverage component', () => {
const groupAnalyticsCoverageReportsPathWithDates = `${defaultProps.groupAnalyticsCoverageReportsPath}&start_date=2020-06-06&end_date=2020-07-06`; const groupAnalyticsCoverageReportsPathWithDates = `${defaultProps.groupAnalyticsCoverageReportsPath}&start_date=2020-06-06&end_date=2020-07-06`;
describe('with all projects selected', () => { describe('with all projects selected', () => {
beforeEach(() => {
createComponent({ allProjectsSelected: true });
});
it('renders primary action as a link with no project_ids param', () => { it('renders primary action as a link with no project_ids param', () => {
expect(findCodeCoverageDownloadButton().attributes('href')).toBe( wrapper.setData({ allProjectsSelected: true, selectedProjectIds: [] });
groupAnalyticsCoverageReportsPathWithDates,
); return wrapper.vm.$nextTick().then(() => {
expect(findCodeCoverageDownloadButton().attributes('href')).toBe(
groupAnalyticsCoverageReportsPathWithDates,
);
});
}); });
}); });
describe('with two or more projects selected without selecting all projects', () => { describe('with two or more projects selected without selecting all projects', () => {
beforeEach(() => {
createComponent({ allProjectsSelected: false, selectedProjectIds: [1, 2] });
});
it('renders primary action as a link with two project IDs as parameters', () => { it('renders primary action as a link with two project IDs as parameters', () => {
wrapper.setData({ allProjectsSelected: false, selectedProjectIds: [1, 2] });
const projectIdsQueryParam = `project_ids%5B%5D=1&project_ids%5B%5D=2`; const projectIdsQueryParam = `project_ids%5B%5D=1&project_ids%5B%5D=2`;
const expectedPath = `${groupAnalyticsCoverageReportsPathWithDates}&${projectIdsQueryParam}`; const expectedPath = `${groupAnalyticsCoverageReportsPathWithDates}&${projectIdsQueryParam}`;
expect(findCodeCoverageDownloadButton().attributes('href')).toBe(expectedPath); return wrapper.vm.$nextTick().then(() => {
expect(findCodeCoverageDownloadButton().attributes('href')).toBe(expectedPath);
});
}); });
}); });
describe('with one project selected', () => { describe('with one project selected', () => {
beforeEach(() => {
createComponent({ allProjectsSelected: false, selectedProjectIds: [1] });
});
it('renders primary action as a link with one project ID as a parameter', () => { it('renders primary action as a link with one project ID as a parameter', () => {
wrapper.setData({ allProjectsSelected: false, selectedProjectIds: [1] });
const projectIdsQueryParam = `project_ids%5B%5D=1`; const projectIdsQueryParam = `project_ids%5B%5D=1`;
const expectedPath = `${groupAnalyticsCoverageReportsPathWithDates}&${projectIdsQueryParam}`; const expectedPath = `${groupAnalyticsCoverageReportsPathWithDates}&${projectIdsQueryParam}`;
expect(findCodeCoverageDownloadButton().attributes('href')).toBe(expectedPath); return wrapper.vm.$nextTick().then(() => {
expect(findCodeCoverageDownloadButton().attributes('href')).toBe(expectedPath);
});
}); });
}); });
describe('with no projects selected', () => { describe('with no projects selected', () => {
beforeEach(() => {
createComponent({ allProjectsSelected: false, selectedProjectIds: [] });
});
it('renders a disabled primary action button', () => { it('renders a disabled primary action button', () => {
expect(findCodeCoverageDownloadButton().attributes('disabled')).toBe('true'); expect(findCodeCoverageDownloadButton().attributes('disabled')).toBe('true');
}); });
}); });
describe('when clicking the select all button', () => { describe('when clicking the select all button', () => {
beforeEach(() => {
createComponent({ allProjectsSelected: false, selectedProjectIds: [] });
});
it('selects all projects and removes the disabled attribute from the download button', () => { it('selects all projects and removes the disabled attribute from the download button', () => {
wrapper.setData({ allProjectsSelected: false, selectedProjectIds: [] });
clickSelectAllProjectsButton(); clickSelectAllProjectsButton();
return wrapper.vm.$nextTick().then(() => { return wrapper.vm.$nextTick().then(() => {
......
...@@ -53,10 +53,6 @@ describe('Select projects dropdown component', () => { ...@@ -53,10 +53,6 @@ describe('Select projects dropdown component', () => {
}); });
}; };
beforeEach(() => {
createComponent();
});
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
wrapper = null; wrapper = null;
...@@ -128,6 +124,8 @@ describe('Select projects dropdown component', () => { ...@@ -128,6 +124,8 @@ describe('Select projects dropdown component', () => {
describe('when there is only one page of projects', () => { describe('when there is only one page of projects', () => {
it('should not render the intersection observer component', () => { it('should not render the intersection observer component', () => {
createComponent();
expect(findIntersectionObserver().exists()).toBe(false); expect(findIntersectionObserver().exists()).toBe(false);
}); });
}); });
...@@ -142,47 +140,42 @@ describe('Select projects dropdown component', () => { ...@@ -142,47 +140,42 @@ describe('Select projects dropdown component', () => {
}); });
describe('when the intersection observer component appears in view', () => { describe('when the intersection observer component appears in view', () => {
beforeEach(() => { it('makes a query to fetch more projects', () => {
jest jest
.spyOn(wrapper.vm.$apollo.queries.groupProjects, 'fetchMore') .spyOn(wrapper.vm.$apollo.queries.groupProjects, 'fetchMore')
.mockImplementation(jest.fn().mockResolvedValue()); .mockImplementation(jest.fn().mockResolvedValue());
findIntersectionObserver().vm.$emit('appear'); findIntersectionObserver().vm.$emit('appear');
return wrapper.vm.$nextTick();
});
it('makes a query to fetch more projects', () => { return wrapper.vm.$nextTick().then(() => {
expect(wrapper.vm.$apollo.queries.groupProjects.fetchMore).toHaveBeenCalledTimes(1); expect(wrapper.vm.$apollo.queries.groupProjects.fetchMore).toHaveBeenCalledTimes(1);
});
}); });
describe('when the fetchMore query throws an error', () => { describe('when the fetchMore query throws an error', () => {
beforeEach(() => { it('emits an error event', () => {
jest.spyOn(wrapper.vm, '$emit'); jest.spyOn(wrapper.vm, '$emit');
jest jest
.spyOn(wrapper.vm.$apollo.queries.groupProjects, 'fetchMore') .spyOn(wrapper.vm.$apollo.queries.groupProjects, 'fetchMore')
.mockImplementation(jest.fn().mockRejectedValue()); .mockImplementation(jest.fn().mockRejectedValue());
findIntersectionObserver().vm.$emit('appear'); findIntersectionObserver().vm.$emit('appear');
return wrapper.vm.$nextTick(); return wrapper.vm.$nextTick().then(() => {
}); expect(wrapper.vm.$emit).toHaveBeenCalledWith('projects-query-error');
});
it('emits an error event', () => {
expect(wrapper.vm.$emit).toHaveBeenCalledWith('projects-query-error');
}); });
}); });
}); });
describe('when a query is loading a new page of projects', () => { describe('when a query is loading a new page of projects', () => {
beforeEach(() => { it('should render the loading spinner', () => {
createComponent({ createComponent({
data: { projectsPageInfo: { hasNextPage: true } }, data: { projectsPageInfo: { hasNextPage: true } },
apolloGroupProjects: { apolloGroupProjects: {
loading: true, loading: true,
}, },
}); });
});
it('should render the loading spinner', () => {
expect(findLoadingIcon().exists()).toBe(true); expect(findLoadingIcon().exists()).toBe(true);
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment