From 3c32dccfdeb3919e1583415bc5dc9be9a1ec0bd3 Mon Sep 17 00:00:00 2001 From: Erik Engervall Date: Sun, 18 Apr 2021 12:44:34 +0200 Subject: [PATCH] Add tests for Repo Signed-off-by: Erik Engervall --- .../src/api/PluginApiClient.ts | 2 +- .../src/cards/projectForm/Owner.test.tsx | 3 +- .../src/cards/projectForm/Repo.test.tsx | 110 ++++++++++++++++++ .../src/cards/projectForm/Repo.tsx | 12 +- .../src/hooks/useQueryHandler.test.tsx | 4 +- .../src/test-helpers/test-helpers.test.ts | 4 +- .../src/test-helpers/test-helpers.ts | 4 + .../src/test-helpers/test-ids.ts | 6 + 8 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 plugins/github-release-manager/src/cards/projectForm/Repo.test.tsx diff --git a/plugins/github-release-manager/src/api/PluginApiClient.ts b/plugins/github-release-manager/src/api/PluginApiClient.ts index 4acad86375..04f39539eb 100644 --- a/plugins/github-release-manager/src/api/PluginApiClient.ts +++ b/plugins/github-release-manager/src/api/PluginApiClient.ts @@ -587,7 +587,7 @@ type GetOwners = () => Promise<{ export type GetOwnersResult = UnboxReturnedPromise; type GetRepositories = (args: { - owner: string; + owner: OwnerRepo['owner']; }) => Promise<{ repositories: string[]; }>; diff --git a/plugins/github-release-manager/src/cards/projectForm/Owner.test.tsx b/plugins/github-release-manager/src/cards/projectForm/Owner.test.tsx index 71abec2e2d..e673c39c59 100644 --- a/plugins/github-release-manager/src/cards/projectForm/Owner.test.tsx +++ b/plugins/github-release-manager/src/cards/projectForm/Owner.test.tsx @@ -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', () => ({ diff --git a/plugins/github-release-manager/src/cards/projectForm/Repo.test.tsx b/plugins/github-release-manager/src/cards/projectForm/Repo.test.tsx new file mode 100644 index 0000000000..a5b7fca25a --- /dev/null +++ b/plugins/github-release-manager/src/cards/projectForm/Repo.test.tsx @@ -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(); + + 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(); + + 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 [ +

+ Select a repository +

, +

+ Custom queries can be made via the query param + + + repo + +

, + ] + `); + }); + + it('should handle errors', async () => { + (mockApiClient.getRepositories as jest.Mock).mockImplementationOnce( + async () => { + throw new Error('Kaboom'); + }, + ); + + const { getByTestId } = render(); + + 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(` +

+ Encountered an error ( + Kaboom + ") +

+ `); + }); +}); diff --git a/plugins/github-release-manager/src/cards/projectForm/Repo.tsx b/plugins/github-release-manager/src/cards/projectForm/Repo.tsx index e6e94c64ab..fb05783e3e 100644 --- a/plugins/github-release-manager/src/cards/projectForm/Repo.tsx +++ b/plugins/github-release-manager/src/cards/projectForm/Repo.tsx @@ -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 ( {loading ? ( - + ) : ( <> Repositories