diff --git a/src/organisms/Status/collections/ApiResponseError.stories.tsx b/src/organisms/Status/collections/ApiResponseError.stories.tsx new file mode 100644 index 00000000..46cfa836 --- /dev/null +++ b/src/organisms/Status/collections/ApiResponseError.stories.tsx @@ -0,0 +1,46 @@ +import { MemoryRouter } from 'react-router-dom'; + +import { Meta, StoryObj } from '@storybook/react'; + +import { ApiResponseError } from './ApiResponseError'; + +import styled from 'styled-components'; + +const Wrapper = styled.div` + height: 50vh; +`; + +const meta: Meta = { + title: 'Organisms/Status/Collections/ApiResponseError', + component: ApiResponseError, + parameters: { + layout: 'centered', + design: { + type: 'figma', + url: '', + }, + }, + argTypes: { + statusCode: { + control: 'number', + description: 'Eg. 400, 401, 403, 404, 500', + }, + }, + args: { + statusCode: 400, + }, + decorators: (Story) => ( + + + + + + ), +}; + +export default meta; +type Story = StoryObj; + +export const Default: Story = { + args: {}, +}; diff --git a/src/organisms/Status/collections/ApiResponseError.test.tsx b/src/organisms/Status/collections/ApiResponseError.test.tsx new file mode 100644 index 00000000..3f6803ef --- /dev/null +++ b/src/organisms/Status/collections/ApiResponseError.test.tsx @@ -0,0 +1,80 @@ +import { ReactNode } from 'react'; +import { BrowserRouter as Router } from 'react-router-dom'; + +import { screen } from '@testing-library/dom'; +import { render } from '@testing-library/react'; + +import { ApiResponseError } from './ApiResponseError'; + +describe('ApiResponseError', () => { + const renderWithRouter = (children: ReactNode) => + render({children}); + + it('renders BadRequest for statusCode 400', () => { + renderWithRouter(); + + expect( + screen.getByRole('heading', { + name: /bad request/i, + }) + ).toBeInTheDocument(); + }); + + it('renders MissingAccessToApp for statusCode 401', () => { + renderWithRouter(); + expect( + screen.getByRole('heading', { + name: /you don't have access to/i, + }) + ).toBeInTheDocument(); + }); + + it('renders MissingPermissions for statusCode 403', () => { + renderWithRouter(); + expect( + screen.getByRole('heading', { + name: /it looks like you don't have permission to access this page\./i, + }) + ).toBeInTheDocument(); + }); + + it('renders PageNotFound for statusCode 404', () => { + renderWithRouter(); + expect( + screen.getByRole('heading', { + name: /page not found/i, + }) + ).toBeInTheDocument(); + }); + + it('renders ServerError for statusCode 500', () => { + renderWithRouter(); + expect( + screen.getByRole('heading', { + name: /something is wrong on our servers/i, + }) + ).toBeInTheDocument(); + }); + + it('renders GenericError for unknown statusCode', () => { + renderWithRouter(); + expect( + screen.getByRole('heading', { + name: /something went wrong/i, + }) + ).toBeInTheDocument(); + }); + + it('renders GenericError when statusCode is undefined', () => { + renderWithRouter(); + expect( + screen.getByRole('heading', { + name: /something went wrong/i, + }) + ).toBeInTheDocument(); + }); + it('renders nothing when statusCode is less than 400', () => { + renderWithRouter(); + expect(screen.queryByText(/something went wrong/i)).not.toBeInTheDocument(); + }); +}); diff --git a/src/organisms/Status/collections/ApiResponseError.tsx b/src/organisms/Status/collections/ApiResponseError.tsx new file mode 100644 index 00000000..b748d26e --- /dev/null +++ b/src/organisms/Status/collections/ApiResponseError.tsx @@ -0,0 +1,48 @@ +import { FC } from 'react'; + +import { + BadRequest, + GenericError, + MissingAccessToApp, + MissingPermissions, + PageNotFound, + ServerError, +} from './'; + +interface ServerErrorProps { + statusCode?: number; +} + +/** + * Will determine what kind of error message to show, for non-error status code will return nothing + * @param statusCode - optional, will return generic error by default + * @returns error component corresponding to http status code + */ +const ApiResponseError: FC = ({ statusCode }) => { + if (statusCode! < 400) { + return null; + } + if (statusCode === 400) { + return ; + } + + if (statusCode === 401) { + return ; + } + + if (statusCode === 403) { + return ; + } + + if (statusCode === 404) { + return ; + } + + if (statusCode === 500) { + return ; + } + + return ; +}; + +export { ApiResponseError }; diff --git a/src/organisms/Status/collections/index.ts b/src/organisms/Status/collections/index.ts index 5b1017da..a1856814 100644 --- a/src/organisms/Status/collections/index.ts +++ b/src/organisms/Status/collections/index.ts @@ -4,3 +4,4 @@ export { MissingAccessToApp } from './MissingAccessToApp'; export { MissingPermissions } from './MissingPermissions'; export { PageNotFound } from './PageNotFound'; export { ServerError } from './ServerError'; +export { ApiResponseError } from './ApiResponseError';