-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
251 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from 'react' | ||
import { renderHook, waitFor } from '@testing-library/react' | ||
import { UserShow, useUser } from '../useUser' | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { AuthProvider } from 'src/utils/AuthContext' | ||
import { asMock, buildUserShow, userIsSignedIn } from 'src/testHelpers' | ||
import { AuthedFetch, useAuthedFetch } from '../useAuthedFetch' | ||
|
||
jest.mock('../useAuthedFetch') | ||
|
||
describe('useUser', () => { | ||
let mockAuthedFetch: AuthedFetch | ||
|
||
beforeEach(() => { | ||
userIsSignedIn() | ||
mockAuthedFetch = jest.fn() | ||
asMock(useAuthedFetch).mockReturnValue(mockAuthedFetch) | ||
}) | ||
|
||
it('queries for the email template with the given id', async () => { | ||
const client = new QueryClient() | ||
const user: UserShow = buildUserShow() | ||
asMock(mockAuthedFetch).mockResolvedValue({ statusCode: 200, json: { user } }) | ||
|
||
const { result } = renderHook(() => useUser(user.id), { | ||
wrapper: ({ children }) => { | ||
return ( | ||
<QueryClientProvider client={client}> | ||
<AuthProvider>{children}</AuthProvider> | ||
</QueryClientProvider> | ||
) | ||
}, | ||
}) | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toEqual(true)) | ||
expect(mockAuthedFetch).toHaveBeenCalledWith({ | ||
path: `/users/${user.id}`, | ||
method: 'GET', | ||
}) | ||
expect(result.current.data).toEqual(user) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { useAuthedFetch } from './useAuthedFetch' | ||
|
||
export interface UserShow { | ||
id: string | ||
email: string | ||
role: 'admin' | 'member' | ||
} | ||
|
||
const QUERY_KEY = 'useUser' | ||
|
||
export const useUser = (id: string) => { | ||
const authedFetch = useAuthedFetch() | ||
|
||
return useQuery({ | ||
queryKey: [QUERY_KEY, id], | ||
queryFn: async () => { | ||
const result = await authedFetch<{ user: UserShow }>({ | ||
path: `/users/${id}`, | ||
method: 'GET', | ||
}) | ||
const user = result.json!.user | ||
return { ...user, role: user.role ?? 'member' } | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { HeadFC, PageProps } from 'gatsby' | ||
import capitalize from 'lodash.capitalize' | ||
import React, { FC } from 'react' | ||
import { useUser } from 'src/network/useUser' | ||
import { | ||
Heading, | ||
Layout, | ||
PageContent, | ||
Paragraph, | ||
Sidebar, | ||
SkipNavContent, | ||
SpacedContainer, | ||
SidebarNavigation, | ||
LoadingOverlay, | ||
} from 'src/ui' | ||
import { formatPageTitle } from 'src/utils/formatPageTitle' | ||
|
||
export type Props = PageProps<null, null, null> | ||
|
||
const UserShowPage: FC<Props> = ({ params }) => { | ||
const query = useUser(params.id) | ||
const { data: user, isLoading, error } = query | ||
|
||
return ( | ||
<Layout element="div"> | ||
<Sidebar> | ||
<SidebarNavigation /> | ||
</Sidebar> | ||
<PageContent element="main"> | ||
<SkipNavContent /> | ||
<SpacedContainer> | ||
<Heading element="h1">User</Heading> | ||
{error && <Paragraph>{error.message}</Paragraph>} | ||
|
||
{user && ( | ||
<div key={user.id} className="user-item"> | ||
<span className="user-email">{user.email}</span> | ||
<span className="user-role">{capitalize(user.role)}</span> | ||
</div> | ||
)} | ||
{isLoading && <LoadingOverlay description="Loading user" />} | ||
</SpacedContainer> | ||
</PageContent> | ||
</Layout> | ||
) | ||
} | ||
|
||
export default UserShowPage | ||
|
||
export const Head: HeadFC = () => <title>{formatPageTitle('User Details')}</title> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import UserShowPage, { Props } from '../[id]' | ||
import { asMock, buildUseQueryResult, buildUserShow } from 'src/testHelpers' | ||
import { useUser } from 'src/network/useUser' | ||
import { UserShow } from 'src/network/useUser' | ||
import { faker } from '@faker-js/faker' | ||
import { randomUUID } from 'crypto' | ||
import { SIDEBAR_NAVIGATION_TEST_ID as sidebarNavigationTestId } from 'src/ui/SidebarNavigation' | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import capitalize from 'lodash.capitalize' | ||
|
||
jest.mock('src/network/useUser', () => { | ||
return { | ||
useUser: jest.fn(), | ||
} | ||
}) | ||
|
||
describe('User Show Page', () => { | ||
const renderPage = (props?: Partial<Props>) => { | ||
return render( | ||
<QueryClientProvider client={new QueryClient()}> | ||
<UserShowPage | ||
pageContext={null} | ||
uri="" | ||
path="" | ||
location={{} as any} | ||
pageResources={{} as any} | ||
params={{ id: faker.lorem.word() }} | ||
children={undefined} | ||
data={null} | ||
serverData={{}} | ||
{...props} | ||
/> | ||
</QueryClientProvider>, | ||
) | ||
} | ||
|
||
it('is displayed in a layout', () => { | ||
const query = buildUseQueryResult<UserShow>({ isLoading: true, data: undefined }) | ||
asMock(useUser).mockReturnValue(query) | ||
const { baseElement } = renderPage() | ||
expect(baseElement.querySelector('.layout')).not.toBeNull() | ||
}) | ||
|
||
it('displays the sidebar navigation', () => { | ||
const query = buildUseQueryResult<UserShow>({ isLoading: true, data: undefined }) | ||
asMock(useUser).mockReturnValue(query) | ||
const { queryByTestId } = renderPage() | ||
expect(queryByTestId(sidebarNavigationTestId)).not.toBeNull() | ||
}) | ||
|
||
it('loads the correct user', () => { | ||
const userId = randomUUID() | ||
const query = buildUseQueryResult<UserShow>({ isLoading: true, data: undefined }) | ||
asMock(useUser).mockReturnValue(query) | ||
renderPage({ params: { id: userId } }) | ||
expect(useUser).toHaveBeenCalledWith(userId) | ||
}) | ||
|
||
describe('when loading', () => { | ||
it('displays an loading spinner', () => { | ||
const query = buildUseQueryResult<UserShow>({ isLoading: true, data: undefined }) | ||
asMock(useUser).mockReturnValue(query) | ||
const { queryByText } = renderPage() | ||
expect(queryByText('Loading user')).not.toBeNull() | ||
}) | ||
}) | ||
|
||
describe('when successful', () => { | ||
let user: UserShow | ||
|
||
beforeEach(() => { | ||
user = buildUserShow() | ||
const query = buildUseQueryResult({ data: user }) | ||
asMock(useUser).mockReturnValue(query) | ||
}) | ||
|
||
it('displays the user', () => { | ||
const { queryByText } = renderPage() | ||
expect(queryByText(user.email)).not.toBeNull() | ||
expect(queryByText(capitalize(user.role))).not.toBeNull() | ||
}) | ||
}) | ||
|
||
describe('when there is an error', () => { | ||
it('displays an error', () => { | ||
const error = new Error(faker.lorem.sentence()) | ||
const query = buildUseQueryResult<UserShow>({ error, isError: true }) | ||
asMock(useUser).mockReturnValue(query) | ||
const { queryByText } = renderPage() | ||
expect(queryByText(error.message)).not.toBeNull() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters