Add tests for Repo

Signed-off-by: Erik Engervall <erik.engervall@gmail.com>
This commit is contained in:
Erik Engervall
2021-04-18 12:44:34 +02:00
parent 3e308bc4ca
commit 3c32dccfde
8 changed files with 136 additions and 9 deletions
@@ -587,7 +587,7 @@ type GetOwners = () => Promise<{
export type GetOwnersResult = UnboxReturnedPromise<GetOwners>;
type GetRepositories = (args: {
owner: string;
owner: OwnerRepo['owner'];
}) => Promise<{
repositories: string[];
}>;
@@ -20,13 +20,14 @@ import { render, waitFor, screen } from '@testing-library/react';
import {
mockApiClient,
mockCalverProject,
mockSearchCalver,
} from '../../test-helpers/test-helpers';
import { TEST_IDS } from '../../test-helpers/test-ids';
jest.mock('react-router', () => ({
useNavigate: jest.fn(),
useLocation: jest.fn(() => ({
search: `?versioningStrategy=${mockCalverProject.versioningStrategy}&owner=${mockCalverProject.owner}&repo=${mockCalverProject.repo}`,
search: mockSearchCalver,
})),
}));
jest.mock('../../contexts/PluginApiClientContext', () => ({
@@ -0,0 +1,110 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { render, waitFor, screen } from '@testing-library/react';
import {
mockApiClient,
mockCalverProject,
mockSearchCalver,
} from '../../test-helpers/test-helpers';
import { TEST_IDS } from '../../test-helpers/test-ids';
jest.mock('react-router', () => ({
useNavigate: jest.fn(),
useLocation: jest.fn(() => ({
search: mockSearchCalver,
})),
}));
jest.mock('../../contexts/PluginApiClientContext', () => ({
usePluginApiClientContext: jest.fn(() => mockApiClient),
}));
jest.mock('../../contexts/ProjectContext', () => ({
useProjectContext: jest.fn(() => mockCalverProject),
}));
import { useProjectContext } from '../../contexts/ProjectContext';
import { Repo } from './Repo';
describe('Repo', () => {
beforeEach(jest.clearAllMocks);
it('should render select', async () => {
const { getByTestId } = render(<Repo />);
expect(getByTestId(TEST_IDS.form.repo.loading)).toBeInTheDocument();
await waitFor(() => screen.getByTestId(TEST_IDS.form.repo.select));
expect(getByTestId(TEST_IDS.form.repo.select)).toBeInTheDocument();
});
it('should render select for empty repo', async () => {
(useProjectContext as jest.Mock).mockImplementation(() => ({
...mockCalverProject,
repo: '',
}));
const { getAllByTestId, getByTestId } = render(<Repo />);
expect(getByTestId(TEST_IDS.form.repo.loading)).toBeInTheDocument();
await waitFor(() => screen.getAllByTestId(TEST_IDS.form.repo.empty));
expect(getAllByTestId(TEST_IDS.form.repo.empty)).toMatchInlineSnapshot(`
Array [
<p
class="MuiFormHelperText-root Mui-required"
data-testid="grm--form--repo--empty"
>
Select a repository
</p>,
<p
class="MuiFormHelperText-root Mui-required"
data-testid="grm--form--repo--empty"
>
Custom queries can be made via the query param
<strong>
repo
</strong>
</p>,
]
`);
});
it('should handle errors', async () => {
(mockApiClient.getRepositories as jest.Mock).mockImplementationOnce(
async () => {
throw new Error('Kaboom');
},
);
const { getByTestId } = render(<Repo />);
expect(getByTestId(TEST_IDS.form.repo.loading)).toBeInTheDocument();
await waitFor(() => screen.getByTestId(TEST_IDS.form.repo.error));
expect(getByTestId(TEST_IDS.form.repo.error)).toMatchInlineSnapshot(`
<p
class="MuiFormHelperText-root Mui-error Mui-required"
data-testid="grm--form--repo--error"
>
Encountered an error (
Kaboom
")
</p>
`);
});
});
@@ -30,6 +30,7 @@ import { useFormClasses } from './styles';
import { CenteredCircularProgress } from '../../components/CenteredCircularProgress';
import { useProjectContext } from '../../contexts/ProjectContext';
import { useQueryHandler } from '../../hooks/useQueryHandler';
import { TEST_IDS } from '../../test-helpers/test-ids';
export function Repo() {
const pluginApiClient = usePluginApiClientContext();
@@ -53,11 +54,12 @@ export function Repo() {
return (
<FormControl className={formClasses.formControl} required error={!!error}>
{loading ? (
<CenteredCircularProgress />
<CenteredCircularProgress data-testid={TEST_IDS.form.repo.loading} />
) : (
<>
<InputLabel id="repo-select-label">Repositories</InputLabel>
<Select
data-testid={TEST_IDS.form.repo.select}
labelId="repo-select-label"
id="repo-select"
value={project.repo}
@@ -89,15 +91,17 @@ export function Repo() {
</Select>
{error && (
<FormHelperText>
<FormHelperText data-testid={TEST_IDS.form.repo.error}>
Encountered an error ({error.message}")
</FormHelperText>
)}
{!error && project.repo.length === 0 && (
<>
<FormHelperText>Select a repository</FormHelperText>
<FormHelperText>
<FormHelperText data-testid={TEST_IDS.form.repo.empty}>
Select a repository
</FormHelperText>
<FormHelperText data-testid={TEST_IDS.form.repo.empty}>
Custom queries can be made via the query param{' '}
<strong>repo</strong>
</FormHelperText>
@@ -17,11 +17,11 @@
import React from 'react';
import { render } from '@testing-library/react';
import { mockSemverProject } from '../test-helpers/test-helpers';
import { mockSearchSemver } from '../test-helpers/test-helpers';
jest.mock('react-router', () => ({
useLocation: jest.fn(() => ({
search: `?versioningStrategy=${mockSemverProject.versioningStrategy}&owner=${mockSemverProject.owner}&repo=${mockSemverProject.repo}`,
search: mockSearchSemver,
})),
}));
@@ -76,7 +76,7 @@ describe('testHelpers', () => {
},
"name": "rc/1.2.3",
},
"mockReleaseCandidate": Object {
"mockReleaseCandidateCalver": Object {
"htmlUrl": "mock_release_html_url",
"id": 1,
"prerelease": true,
@@ -97,6 +97,8 @@ describe('testHelpers', () => {
"tagName": "version-1.2.3",
"targetCommitish": "rc/1.2.3",
},
"mockSearchCalver": "?versioningStrategy=calver&owner=mock_owner&repo=mock_repo",
"mockSearchSemver": "?versioningStrategy=semver&owner=mock_owner&repo=mock_repo",
"mockSelectedPatchCommit": Object {
"author": Object {
"htmlUrl": "author_html_url",
@@ -39,6 +39,10 @@ export const mockCalverProject: Project = {
versioningStrategy: 'calver',
};
export const mockSearchCalver = `?versioningStrategy=${mockCalverProject.versioningStrategy}&owner=${mockCalverProject.owner}&repo=${mockCalverProject.repo}`;
export const mockSearchSemver = `?versioningStrategy=${mockSemverProject.versioningStrategy}&owner=${mockSemverProject.owner}&repo=${mockSemverProject.repo}`;
export const mockDefaultBranch = 'mock_defaultBranch';
export const mockNextGitHubInfo: ReturnType<typeof getRcGitHubInfo> = {
@@ -42,6 +42,12 @@ export const TEST_IDS = {
error: 'grm--form--owner--error',
empty: 'grm--form--owner--empty',
},
repo: {
loading: 'grm--form--repo--loading',
select: 'grm--form--repo--select',
error: 'grm--form--repo--error',
empty: 'grm--form--repo--empty',
},
},
components: {
divider: 'grm--divider',