-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(RSC): Better error handling for missing context (#508)
- Loading branch information
Showing
6 changed files
with
143 additions
and
1 deletion.
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
24 changes: 24 additions & 0 deletions
24
packages/next-intl/test/react-client/useFormatter.test.tsx
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,24 @@ | ||
import {render, screen} from '@testing-library/react'; | ||
import React from 'react'; | ||
import {it, expect} from 'vitest'; | ||
import {useFormatter, NextIntlClientProvider} from '../../src'; | ||
|
||
function Component() { | ||
const format = useFormatter(); | ||
return <>{format.number(1)}</>; | ||
} | ||
|
||
it('provides a helpful error message when no provider is found', () => { | ||
expect(() => render(<Component />)).toThrow( | ||
/Failed to call `useFormatter`, because the context from `NextIntlClientProvider` was not found\./ | ||
); | ||
}); | ||
|
||
it('works with a provider', () => { | ||
render( | ||
<NextIntlClientProvider locale="en"> | ||
<Component /> | ||
</NextIntlClientProvider> | ||
); | ||
screen.getByText('1'); | ||
}); |
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,29 @@ | ||
import {render, screen} from '@testing-library/react'; | ||
import React from 'react'; | ||
import {it} from 'vitest'; | ||
import {useNow, NextIntlClientProvider} from '../../src'; | ||
|
||
function Component() { | ||
const now = useNow(); | ||
return <>{now.toISOString()}</>; | ||
} | ||
|
||
it('works without a provider', () => { | ||
render( | ||
<NextIntlClientProvider locale="en"> | ||
<Component /> | ||
</NextIntlClientProvider> | ||
); | ||
}); | ||
|
||
it('works with a provider', () => { | ||
render( | ||
<NextIntlClientProvider | ||
locale="en" | ||
now={new Date('2021-01-01T00:00:00.000Z')} | ||
> | ||
<Component /> | ||
</NextIntlClientProvider> | ||
); | ||
screen.getByText('2021-01-01T00:00:00.000Z'); | ||
}); |
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 {render, screen} from '@testing-library/react'; | ||
import React from 'react'; | ||
import {it} from 'vitest'; | ||
import {useTimeZone, NextIntlClientProvider} from '../../src'; | ||
|
||
function Component() { | ||
const timeZone = useTimeZone(); | ||
return <>{timeZone}</>; | ||
} | ||
|
||
it('works without a provider', () => { | ||
render( | ||
<NextIntlClientProvider locale="en"> | ||
<Component /> | ||
</NextIntlClientProvider> | ||
); | ||
}); | ||
|
||
it('works with a provider', () => { | ||
render( | ||
<NextIntlClientProvider locale="en" timeZone="America/New_York"> | ||
<Component /> | ||
</NextIntlClientProvider> | ||
); | ||
screen.getByText('America/New_York'); | ||
}); |
35 changes: 35 additions & 0 deletions
35
packages/next-intl/test/react-client/useTranslations.test.tsx
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,35 @@ | ||
import {render, screen} from '@testing-library/react'; | ||
import React from 'react'; | ||
import {it, expect, vi} from 'vitest'; | ||
import {useTranslations, NextIntlClientProvider} from '../../src'; | ||
|
||
function Component() { | ||
const t = useTranslations('Component'); | ||
return <>{t('test')}</>; | ||
} | ||
|
||
it('provides a helpful error message when no provider is found', () => { | ||
expect(() => render(<Component />)).toThrow( | ||
/Failed to call `useTranslations`, because the context from `NextIntlClientProvider` was not found\./ | ||
); | ||
}); | ||
|
||
it('works with a provider', () => { | ||
render( | ||
<NextIntlClientProvider locale="en" messages={{Component: {test: 'Hello'}}}> | ||
<Component /> | ||
</NextIntlClientProvider> | ||
); | ||
screen.getByText('Hello'); | ||
}); | ||
|
||
it('uses error handling for missing messages', () => { | ||
const onError = vi.fn(); | ||
render( | ||
<NextIntlClientProvider locale="en" onError={onError}> | ||
<Component /> | ||
</NextIntlClientProvider> | ||
); | ||
screen.getByText('Component.test'); | ||
expect(onError).toHaveBeenCalled(); | ||
}); |