Skip to content

Commit

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

import {LocaleProviderBase} from '../../i18n/components/LocaleProvider'
import {prepareI18n} from '../../i18n/i18nConfig'
import {usEnglishLocale} from '../../i18n/locales'
import {useSource} from '../../studio/source'
import {FormBuilderInputErrorBoundary} from './FormBuilderInputErrorBoundary'

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

jest.mock('use-hot-module-reload', () => ({
useHotModuleReload: jest.fn(),
}))

const useSourceMock = useSource as jest.Mock

describe('FormBuilderInputErrorBoundary', () => {
beforeAll(() => {
jest.clearAllMocks()
})

it('renders children when there is no error', async () => {
render(
<FormBuilderInputErrorBoundary>
<div data-testid="child">Child Component</div>
</FormBuilderInputErrorBoundary>,
)

expect(screen.getByTestId('child')).toBeInTheDocument()
})

it('calls onStudioError when an error is caught', async () => {
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}
>
<FormBuilderInputErrorBoundary>
<ThrowErrorComponent />
</FormBuilderInputErrorBoundary>
</LocaleProviderBase>
</ThemeProvider>,
)

expect(onStudioError).toHaveBeenCalledTimes(1)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ export function FormBuilderInputErrorBoundary(
error: null,
info: {},
})
const {onStudioError} = useSource()
const source = useSource()
const handleRetry = useCallback(() => setError({error: null, info: {}}), [])
const handleCatch = useCallback(
({error: caughtError, info: caughtInfo}: {error: Error; info: React.ErrorInfo}) => {
setError({error: caughtError, info: caughtInfo})

if (onStudioError) {
if (source?.onStudioError) {
const {onStudioError} = source
onStudioError(caughtError, caughtInfo)
}
},
[onStudioError],
[source],
)

if (!error) {
Expand Down

0 comments on commit 26236f1

Please sign in to comment.