Skip to content

Commit

Permalink
test(sanity): add test to WorkspaceRouterProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
RitaDias committed Oct 1, 2024
1 parent 2ecff5c commit 9fd439c
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {describe, expect, it, jest} from '@jest/globals'
import {ErrorBoundary, studioTheme, ThemeProvider} from '@sanity/ui'
import {render, screen} from '@testing-library/react'

import {LocaleProviderBase, usEnglishLocale} from '../../i18n'
import {prepareI18n} from '../../i18n/i18nConfig'
import {useSource} from '../source'
import {WorkspaceRouterProvider} from './WorkspaceRouterProvider'

jest.mock('../router/RouterHistoryContext', () => ({
useRouterHistory: () => ({
location: {pathname: '/'},
listen: jest.fn(),
}),
}))

jest.mock('../source', () => ({
useSource: jest.fn(),
}))

jest.mock('../router', () => ({
createRouter: () => ({
getBasePath: jest.fn(),
decode: jest.fn(),
isNotFound: jest.fn(),
}),
}))

jest.mock('sanity/router', () => ({
RouterProvider: ({children}: {children: React.ReactNode}) => <div>{children}</div>,
IntentLink: () => <div>IntentLink</div>,
}))

jest.mock('./WorkspaceRouterProvider', () => ({
...(jest.requireActual('./WorkspaceRouterProvider') as object),
useRouterFromWorkspaceHistory: jest.fn(),
}))

const useSourceMock = useSource as jest.Mock

describe('WorkspaceRouterProvider', () => {
const LoadingComponent = () => <div>Loading...</div>
const children = <div>Children</div>
const workspace = {
basePath: '',
tools: [],
icon: null,
unstable_sources: [],
scheduledPublishing: false,
document: {},
form: {},
search: {},
title: 'Default Workspace',
name: 'default',
projectId: 'test',
dataset: 'test',
schema: {},
templates: {},
currentUser: {},
authenticated: true,
auth: {},
getClient: jest.fn(),
i18n: {},
__internal: {},
type: 'workspace',
// Add other required properties with appropriate default values
} as unknown as Workspace

it('renders children when state is not null', () => {
render(
<WorkspaceRouterProvider LoadingComponent={LoadingComponent} workspace={workspace}>
{children}
</WorkspaceRouterProvider>,
)

expect(screen.getByText('Children')).toBeInTheDocument()
})

it('calls onStudioError when an error is caught', () => {
const onStudioError = jest.fn()
useSourceMock.mockReturnValue({onStudioError})

const ThrowErrorComponent = () => {
throw new Error('An EXPECTED, testing error occurred!')
}

const locales = [usEnglishLocale]
const {i18next} = prepareI18n({
projectId: 'test',
dataset: 'test',
name: 'test',
})

render(
<ThemeProvider theme={studioTheme}>
<LocaleProviderBase
projectId={'test'}
sourceId={'test'}
locales={locales}
i18next={i18next}
>
{/* prevents thrown error from breaking the test */}
<ErrorBoundary onCatch={({error, info}) => <></>}>
<WorkspaceRouterProvider LoadingComponent={LoadingComponent} workspace={workspace}>
<ThrowErrorComponent />
</WorkspaceRouterProvider>
</ErrorBoundary>
</LocaleProviderBase>
</ThemeProvider>,
)

expect(onStudioError).toHaveBeenCalledTimes(1)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ export function WorkspaceRouterProvider({
const history = useRouterHistory()
const router = useMemo(() => createRouter({basePath, tools}), [basePath, tools])
const [state, onNavigate] = useRouterFromWorkspaceHistory(history, router, tools)
const {onStudioError} = useSource()
const source = useSource()

const handleCatchError = useCallback(
({error, info}: {error: Error; info: React.ErrorInfo}) => {
/** catches errors in studio to be able to be caught in the config */
if (onStudioError) {
if (source?.onStudioError) {
const {onStudioError} = source
onStudioError(error, info)
}

throw new Error(error.message)
},
[onStudioError],
[source],
)

// `state` is only null if the Studio is somehow rendering in SSR or using hydrateRoot in combination with `unstable_noAuthBoundary`.
Expand All @@ -65,7 +66,7 @@ type HandleNavigate = (opts: {path: string; replace?: boolean}) => void
/**
* @internal
*/
function useRouterFromWorkspaceHistory(
export function useRouterFromWorkspaceHistory(
history: RouterHistory,
router: Router,
tools: Tool[],
Expand Down

0 comments on commit 9fd439c

Please sign in to comment.