diff --git a/plugins/github-release-manager/src/GitHubReleaseManager.tsx b/plugins/github-release-manager/src/GitHubReleaseManager.tsx index 3d0ea73a37..573e788177 100644 --- a/plugins/github-release-manager/src/GitHubReleaseManager.tsx +++ b/plugins/github-release-manager/src/GitHubReleaseManager.tsx @@ -37,12 +37,16 @@ import { Info } from './cards/info/Info'; import { InfoCardPlus } from './components/InfoCardPlus'; import { isProjectValid } from './helpers/isProjectValid'; import { Patch } from './cards/patchRc/Patch'; -import { ProjectContext, Project } from './contexts/ProjectContext'; +import { + ProjectContext, + Project, + useProjectContext, +} from './contexts/ProjectContext'; import { PromoteRc } from './cards/promoteRc/PromoteRc'; import { RefetchContext } from './contexts/RefetchContext'; import { RepoDetailsForm } from './cards/projectForm/RepoDetailsForm'; -import { useVersioningStrategyMatchesRepoTags } from './helpers/useVersioningStrategyMatchesRepoTags'; -import { useQueryHandler } from './helpers/useQueryHandler'; +import { useVersioningStrategyMatchesRepoTags } from './hooks/useVersioningStrategyMatchesRepoTags'; +import { useQueryHandler } from './hooks/useQueryHandler'; interface GitHubReleaseManagerProps { components?: { @@ -92,32 +96,28 @@ export function GitHubReleaseManager({ return ( -
- + +
+ - - - + + + - {isProjectValid(project) && ( - - )} -
+ {isProjectValid(project) && } +
+
); } function Cards({ components, - project, }: { components: GitHubReleaseManagerProps['components']; - project: Project; }) { const pluginApiClient = usePluginApiClientContext(); + const project = useProjectContext(); const [refetchTrigger, setRefetchTrigger] = useState(0); const gitHubBatchInfo = useAsync( getGitHubBatchInfo({ project, pluginApiClient }), @@ -159,52 +159,50 @@ function Cards({ } return ( - - - - {gitHubBatchInfo.value.latestRelease && !versioningStrategyMatches && ( - - Versioning mismatch, expected {project.versioningStrategy}{' '} - version, got "{gitHubBatchInfo.value.latestRelease.tagName}" - - )} + + + {gitHubBatchInfo.value.latestRelease && !versioningStrategyMatches && ( + + Versioning mismatch, expected {project.versioningStrategy} version, + got "{gitHubBatchInfo.value.latestRelease.tagName}" + + )} - {!gitHubBatchInfo.value.latestRelease && ( - - This repository has not releases yet - - )} + {!gitHubBatchInfo.value.latestRelease && ( + + This repository has not releases yet + + )} - + + {components?.default?.createRc?.omit !== true && ( + + )} - {components?.default?.createRc?.omit !== true && ( - - )} + {components?.default?.promoteRc?.omit !== true && ( + + )} - {components?.default?.promoteRc?.omit !== true && ( - - )} - - {components?.default?.patch?.omit !== true && ( - - )} - - - + {components?.default?.patch?.omit !== true && ( + + )} + + ); } diff --git a/plugins/github-release-manager/src/cards/projectForm/Owner.test.tsx b/plugins/github-release-manager/src/cards/projectForm/Owner.test.tsx new file mode 100644 index 0000000000..71abec2e2d --- /dev/null +++ b/plugins/github-release-manager/src/cards/projectForm/Owner.test.tsx @@ -0,0 +1,113 @@ +/* + * 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, +} 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}`, + })), +})); +jest.mock('../../contexts/PluginApiClientContext', () => ({ + usePluginApiClientContext: jest.fn(() => mockApiClient), +})); +jest.mock('../../contexts/ProjectContext', () => ({ + useProjectContext: jest.fn(() => mockCalverProject), +})); + +import { useProjectContext } from '../../contexts/ProjectContext'; +import { Owner } from './Owner'; + +describe('Owner', () => { + beforeEach(jest.clearAllMocks); + + it('should render select', async () => { + const { getByTestId } = render( + , + ); + + expect(getByTestId(TEST_IDS.form.owner.loading)).toBeInTheDocument(); + + await waitFor(() => screen.getByTestId(TEST_IDS.form.owner.select)); + expect(getByTestId(TEST_IDS.form.owner.select)).toBeInTheDocument(); + }); + + it('should render select for empty owners', async () => { + (useProjectContext as jest.Mock).mockImplementation(() => ({ + ...mockCalverProject, + owner: '', + })); + + const { getAllByTestId, getByTestId } = render( + , + ); + + expect(getByTestId(TEST_IDS.form.owner.loading)).toBeInTheDocument(); + + await waitFor(() => screen.getAllByTestId(TEST_IDS.form.owner.empty)); + expect(getAllByTestId(TEST_IDS.form.owner.empty)).toMatchInlineSnapshot(` + Array [ +

+ Select an owner (org or user) +

, +

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

, + ] + `); + }); + + it('should handle errors', async () => { + (mockApiClient.getOwners as jest.Mock).mockImplementationOnce(async () => { + throw new Error('Kaboom'); + }); + + const { getByTestId } = render( + , + ); + + expect(getByTestId(TEST_IDS.form.owner.loading)).toBeInTheDocument(); + await waitFor(() => screen.getByTestId(TEST_IDS.form.owner.error)); + expect(getByTestId(TEST_IDS.form.owner.error)).toMatchInlineSnapshot(` +

+ Encountered an error ( + Kaboom + ) +

+ `); + }); +}); diff --git a/plugins/github-release-manager/src/cards/projectForm/Owner.tsx b/plugins/github-release-manager/src/cards/projectForm/Owner.tsx index 0ce411cb59..5e7d5b2f30 100644 --- a/plugins/github-release-manager/src/cards/projectForm/Owner.tsx +++ b/plugins/github-release-manager/src/cards/projectForm/Owner.tsx @@ -26,18 +26,14 @@ import { } from '@material-ui/core'; import { CenteredCircularProgress } from '../../components/CenteredCircularProgress'; -import { Project } from '../../contexts/ProjectContext'; import { useFormClasses } from './styles'; import { usePluginApiClientContext } from '../../contexts/PluginApiClientContext'; -import { useQueryHandler } from '../../helpers/useQueryHandler'; +import { useProjectContext } from '../../contexts/ProjectContext'; +import { useQueryHandler } from '../../hooks/useQueryHandler'; +import { TEST_IDS } from '../../test-helpers/test-ids'; -export function Owner({ - username, - project, -}: { - username: string; - project: Project; -}) { +export function Owner({ username }: { username: string }) { + const project = useProjectContext(); const formClasses = useFormClasses(); const navigate = useNavigate(); const pluginApiClient = usePluginApiClientContext(); @@ -52,11 +48,12 @@ export function Owner({ return ( {loading ? ( - + ) : ( <> Owners