Return objects from contexts to simplify naming

Signed-off-by: Erik Engervall <erik.engervall@gmail.com>
This commit is contained in:
Erik Engervall
2021-04-22 13:33:43 +02:00
parent a32d221ab8
commit 491af9430a
22 changed files with 90 additions and 54 deletions
@@ -81,8 +81,8 @@ export function GitHubReleaseManager(props: GitHubReleaseManagerProps) {
}
return (
<PluginApiClientContext.Provider value={pluginApiClient}>
<ProjectContext.Provider value={project}>
<PluginApiClientContext.Provider value={{ pluginApiClient }}>
<ProjectContext.Provider value={{ project }}>
<div className={classes.root}>
<ContentHeader title="GitHub Release Manager" />
@@ -21,10 +21,14 @@ import { mockApiClient, mockCalverProject } from '../test-helpers/test-helpers';
import { TEST_IDS } from '../test-helpers/test-ids';
jest.mock('../contexts/PluginApiClientContext', () => ({
usePluginApiClientContext: () => mockApiClient,
usePluginApiClientContext: () => ({
pluginApiClient: mockApiClient,
}),
}));
jest.mock('../contexts/ProjectContext', () => ({
useProjectContext: jest.fn(() => mockCalverProject),
useProjectContext: () => ({
project: mockCalverProject,
}),
}));
import { Cards } from './Cards';
@@ -35,8 +35,8 @@ export function Cards({
}: {
components: GitHubReleaseManagerProps['components'];
}) {
const pluginApiClient = usePluginApiClientContext();
const project = useProjectContext();
const { pluginApiClient } = usePluginApiClientContext();
const { project } = useProjectContext();
const [refetchTrigger, setRefetchTrigger] = useState(0);
const { gitHubBatchInfo } = useGetGitHubBatchInfo({
pluginApiClient,
@@ -30,10 +30,14 @@ import { TEST_IDS } from '../../test-helpers/test-ids';
import { useCreateRc } from './hooks/useCreateRc';
jest.mock('../../contexts/PluginApiClientContext', () => ({
usePluginApiClientContext: () => mockApiClient,
usePluginApiClientContext: () => ({
pluginApiClient: mockApiClient,
}),
}));
jest.mock('../../contexts/ProjectContext', () => ({
useProjectContext: jest.fn(() => mockCalverProject),
useProjectContext: jest.fn(() => ({
project: mockCalverProject,
})),
}));
jest.mock('../../helpers/getRcGitHubInfo', () => ({
getRcGitHubInfo: () => mockNextGitHubInfoSemver,
@@ -65,7 +69,9 @@ describe('CreateRc', () => {
});
it('should display select element for semver', () => {
(useProjectContext as jest.Mock).mockReturnValue(mockSemverProject);
(useProjectContext as jest.Mock).mockReturnValue({
project: mockSemverProject,
});
const { getByTestId } = render(
<CreateRc
@@ -55,8 +55,8 @@ export const CreateRc = ({
releaseBranch,
successCb,
}: CreateRcProps) => {
const pluginApiClient = usePluginApiClientContext();
const project = useProjectContext();
const { pluginApiClient } = usePluginApiClientContext();
const { project } = useProjectContext();
const classes = useStyles();
const [semverBumpLevel, setSemverBumpLevel] = useState<'major' | 'minor'>(
@@ -24,7 +24,9 @@ import {
} from '../../test-helpers/test-helpers';
jest.mock('../../contexts/ProjectContext', () => ({
useProjectContext: jest.fn(() => mockCalverProject),
useProjectContext: () => ({
project: mockCalverProject,
}),
}));
import { Info } from './Info';
@@ -34,8 +34,7 @@ interface InfoCardProps {
}
export const Info = ({ releaseBranch, latestRelease }: InfoCardProps) => {
const project = useProjectContext();
const { project } = useProjectContext();
const classes = useStyles();
return (
@@ -24,7 +24,9 @@ import {
import { TEST_IDS } from '../../test-helpers/test-ids';
jest.mock('../../contexts/ProjectContext', () => ({
useProjectContext: jest.fn(() => mockCalverProject),
useProjectContext: () => ({
project: mockCalverProject,
}),
}));
import { Patch } from './Patch';
@@ -40,7 +40,7 @@ export const Patch = ({
releaseBranch,
successCb,
}: PatchProps) => {
const project = useProjectContext();
const { project } = useProjectContext();
const classes = useStyles();
function Body() {
@@ -28,10 +28,14 @@ import {
} from '../../test-helpers/test-helpers';
jest.mock('../../contexts/PluginApiClientContext', () => ({
usePluginApiClientContext: jest.fn(() => mockApiClient),
usePluginApiClientContext: () => ({
pluginApiClient: mockApiClient,
}),
}));
jest.mock('../../contexts/ProjectContext', () => ({
useProjectContext: jest.fn(() => mockCalverProject),
useProjectContext: () => ({
project: mockCalverProject,
}),
}));
jest.mock('./hooks/usePatch', () => ({
usePatch: () => ({
@@ -65,8 +65,8 @@ export const PatchBody = ({
successCb,
tagParts,
}: PatchBodyProps) => {
const pluginApiClient = usePluginApiClientContext();
const project = useProjectContext();
const { pluginApiClient } = usePluginApiClientContext();
const { project } = useProjectContext();
const [checkedCommitIndex, setCheckedCommitIndex] = useState(-1);
const githubDataResponse = useAsync(async () => {
@@ -25,10 +25,14 @@ import {
import { TEST_IDS } from '../../test-helpers/test-ids';
jest.mock('../../contexts/PluginApiClientContext', () => ({
usePluginApiClientContext: jest.fn(() => mockApiClient),
usePluginApiClientContext: () => ({
pluginApiClient: mockApiClient,
}),
}));
jest.mock('../../contexts/ProjectContext', () => ({
useProjectContext: jest.fn(() => mockCalverProject),
useProjectContext: () => ({
project: mockCalverProject,
}),
}));
jest.mock('./hooks/usePromoteRc', () => ({
usePromoteRc: () => ({
@@ -33,8 +33,8 @@ interface PromoteRcBodyProps {
}
export const PromoteRcBody = ({ rcRelease, successCb }: PromoteRcBodyProps) => {
const pluginApiClient = usePluginApiClientContext();
const project = useProjectContext();
const { pluginApiClient } = usePluginApiClientContext();
const { project } = useProjectContext();
const classes = useStyles();
const releaseVersion = rcRelease.tagName.replace('rc-', 'version-');
@@ -31,10 +31,14 @@ jest.mock('react-router', () => ({
})),
}));
jest.mock('../../contexts/PluginApiClientContext', () => ({
usePluginApiClientContext: jest.fn(() => mockApiClient),
usePluginApiClientContext: () => ({
pluginApiClient: mockApiClient,
}),
}));
jest.mock('../../contexts/ProjectContext', () => ({
useProjectContext: jest.fn(() => mockCalverProject),
useProjectContext: jest.fn(() => ({
project: mockCalverProject,
})),
}));
import { useProjectContext } from '../../contexts/ProjectContext';
@@ -55,10 +59,9 @@ describe('Owner', () => {
});
it('should render select for empty owners', async () => {
(useProjectContext as jest.Mock).mockImplementation(() => ({
...mockCalverProject,
owner: '',
}));
(useProjectContext as jest.Mock).mockReturnValue({
project: { ...mockCalverProject, owner: '' },
});
const { getAllByTestId, getByTestId } = render(
<Owner username={mockCalverProject.owner} />,
@@ -33,10 +33,10 @@ import { useProjectContext } from '../../contexts/ProjectContext';
import { useQueryHandler } from '../../hooks/useQueryHandler';
export function Owner({ username }: { username: string }) {
const project = useProjectContext();
const { pluginApiClient } = usePluginApiClientContext();
const { project } = useProjectContext();
const formClasses = useFormClasses();
const navigate = useNavigate();
const pluginApiClient = usePluginApiClientContext();
const { getQueryParamsWithUpdates } = useQueryHandler();
const { loading, error, value } = useAsync(() => pluginApiClient.getOwners());
@@ -31,10 +31,14 @@ jest.mock('react-router', () => ({
})),
}));
jest.mock('../../contexts/PluginApiClientContext', () => ({
usePluginApiClientContext: jest.fn(() => mockApiClient),
usePluginApiClientContext: () => ({
pluginApiClient: mockApiClient,
}),
}));
jest.mock('../../contexts/ProjectContext', () => ({
useProjectContext: jest.fn(() => mockCalverProject),
useProjectContext: jest.fn(() => ({
project: mockCalverProject,
})),
}));
import { useProjectContext } from '../../contexts/ProjectContext';
@@ -53,10 +57,9 @@ describe('Repo', () => {
});
it('should render select for empty repo', async () => {
(useProjectContext as jest.Mock).mockImplementation(() => ({
...mockCalverProject,
repo: '',
}));
(useProjectContext as jest.Mock).mockReturnValue({
project: { ...mockCalverProject, repo: '' },
});
const { getAllByTestId, getByTestId } = render(<Repo />);
@@ -33,8 +33,8 @@ import { useProjectContext } from '../../contexts/ProjectContext';
import { useQueryHandler } from '../../hooks/useQueryHandler';
export function Repo() {
const pluginApiClient = usePluginApiClientContext();
const project = useProjectContext();
const { pluginApiClient } = usePluginApiClientContext();
const { project } = useProjectContext();
const navigate = useNavigate();
const formClasses = useFormClasses();
const { getQueryParamsWithUpdates } = useQueryHandler();
@@ -31,7 +31,9 @@ jest.mock('react-router', () => ({
})),
}));
jest.mock('../../contexts/ProjectContext', () => ({
useProjectContext: jest.fn(() => mockSemverProject),
useProjectContext: () => ({
project: mockSemverProject,
}),
}));
import { VersioningStrategy } from './VersioningStrategy';
@@ -31,7 +31,7 @@ import { VERSIONING_STRATEGIES } from '../../constants/constants';
export function VersioningStrategy() {
const navigate = useNavigate();
const project = useProjectContext();
const { project } = useProjectContext();
const { getParsedQuery, getQueryParamsWithUpdates } = useQueryHandler();
useEffect(() => {
@@ -20,15 +20,17 @@ import { IPluginApiClient } from '../api/PluginApiClient';
import { GitHubReleaseManagerError } from '../errors/GitHubReleaseManagerError';
export const PluginApiClientContext = createContext<
IPluginApiClient | undefined
{ pluginApiClient: IPluginApiClient } | undefined
>(undefined);
export const usePluginApiClientContext = () => {
const pluginApiClient = useContext(PluginApiClientContext);
const { pluginApiClient } = useContext(PluginApiClientContext) ?? {};
if (!pluginApiClient) {
throw new GitHubReleaseManagerError('pluginApiClient not found');
}
return pluginApiClient;
return {
pluginApiClient,
};
};
@@ -49,14 +49,18 @@ export interface Project {
isProvidedViaProps: boolean;
}
export const ProjectContext = createContext<Project | undefined>(undefined);
export const ProjectContext = createContext<{ project: Project } | undefined>(
undefined,
);
export const useProjectContext = () => {
const project = useContext(ProjectContext);
const { project } = useContext(ProjectContext) ?? {};
if (!project) {
throw new GitHubReleaseManagerError('project not found');
}
return project;
return {
project,
};
};
@@ -18,12 +18,13 @@ import { createContext, useContext } from 'react';
import { GitHubReleaseManagerError } from '../errors/GitHubReleaseManagerError';
export interface Refetch {
refetchTrigger: number;
setRefetchTrigger: React.Dispatch<React.SetStateAction<number>>;
}
export const RefetchContext = createContext<Refetch | undefined>(undefined);
export const RefetchContext = createContext<
| {
refetchTrigger: number;
setRefetchTrigger: React.Dispatch<React.SetStateAction<number>>;
}
| undefined
>(undefined);
export const useRefetchContext = () => {
const refetch = useContext(RefetchContext);