-
Notifications
You must be signed in to change notification settings - Fork 2.9k
test(react-toast): add hook regression tests for useToast, useToastBody, useToastTitle #36044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dmytrokirpa
wants to merge
5
commits into
microsoft:master
Choose a base branch
from
dmytrokirpa:test/react-toast-hook-regression-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+372
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c075ce0
test(react-toast): add hook-level regression tests for useToast, useT…
dmytrokirpa 2fca2e4
test(react-toast): suppress no-deprecated lint on components shape as…
dmytrokirpa a2bfaf2
Merge branch 'master' into test/react-toast-hook-regression-tests
dmytrokirpa 37652bd
Merge branch 'master' into test/react-toast-hook-regression-tests
dmytrokirpa d8321a9
Merge branch 'master' into test/react-toast-hook-regression-tests
dmytrokirpa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
packages/react-components/react-toast/library/src/components/Toast/useToast.test.ts
This file contains hidden or 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,80 @@ | ||
| import { renderHook } from '@testing-library/react-hooks'; | ||
| import * as React from 'react'; | ||
| import { useToast_unstable } from './useToast'; | ||
| import { ToastContainerContextProvider } from '../../contexts/toastContainerContext'; | ||
| import type { ToastContainerContextValue } from '../../contexts/toastContainerContext'; | ||
|
|
||
| const defaultContextValue: ToastContainerContextValue = { | ||
| close: () => null, | ||
| intent: undefined, | ||
| bodyId: 'body-id', | ||
| titleId: 'title-id', | ||
| }; | ||
|
|
||
| function makeWrapper(contextValue: Partial<ToastContainerContextValue> = {}) { | ||
| const value = { ...defaultContextValue, ...contextValue }; | ||
| return ({ children }: { children: React.ReactNode }) => | ||
| React.createElement(ToastContainerContextProvider, { value }, children); | ||
| } | ||
|
|
||
| describe('useToast_unstable', () => { | ||
| it('returns components shape { root: div }', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToast_unstable({}, ref), { wrapper: makeWrapper() }); | ||
| // eslint-disable-next-line @typescript-eslint/no-deprecated | ||
| expect(result.current.components).toEqual({ root: 'div' }); | ||
| }); | ||
|
|
||
| it('always returns a root slot', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToast_unstable({}, ref), { wrapper: makeWrapper() }); | ||
| expect(result.current.root).toBeDefined(); | ||
| }); | ||
|
|
||
| it('sets backgroundAppearance to undefined when appearance prop is omitted', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToast_unstable({}, ref), { wrapper: makeWrapper() }); | ||
| expect(result.current.backgroundAppearance).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('sets backgroundAppearance to "inverted" when appearance="inverted"', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToast_unstable({ appearance: 'inverted' }, ref), { | ||
| wrapper: makeWrapper(), | ||
| }); | ||
| expect(result.current.backgroundAppearance).toBe('inverted'); | ||
| }); | ||
|
|
||
| it('sets backgroundAppearance to "brand" when appearance="brand"', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToast_unstable({ appearance: 'brand' }, ref), { | ||
| wrapper: makeWrapper(), | ||
| }); | ||
| expect(result.current.backgroundAppearance).toBe('brand'); | ||
| }); | ||
|
|
||
| it('reads intent from ToastContainerContext', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToast_unstable({}, ref), { | ||
| wrapper: makeWrapper({ intent: 'success' }), | ||
| }); | ||
| expect(result.current.intent).toBe('success'); | ||
| }); | ||
|
|
||
| it('intent is undefined when context does not provide one', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToast_unstable({}, ref), { | ||
| wrapper: makeWrapper({ intent: undefined }), | ||
| }); | ||
| expect(result.current.intent).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('spreads extra div props onto the root slot', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToast_unstable({ className: 'custom-class', 'aria-label': 'toast' }, ref), { | ||
| wrapper: makeWrapper(), | ||
| }); | ||
| expect(result.current.root.className).toBe('custom-class'); | ||
| expect(result.current.root['aria-label']).toBe('toast'); | ||
| }); | ||
| }); | ||
106 changes: 106 additions & 0 deletions
106
packages/react-components/react-toast/library/src/components/ToastBody/useToastBody.test.ts
This file contains hidden or 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,106 @@ | ||
| import { renderHook } from '@testing-library/react-hooks'; | ||
| import * as React from 'react'; | ||
| import { useToastBody_unstable } from './useToastBody'; | ||
| import { ToastContainerContextProvider } from '../../contexts/toastContainerContext'; | ||
| import { BackgroundAppearanceProvider } from '@fluentui/react-shared-contexts'; | ||
| import type { ToastContainerContextValue } from '../../contexts/toastContainerContext'; | ||
| import type { BackgroundAppearanceContextValue } from '@fluentui/react-shared-contexts'; | ||
|
|
||
| const defaultContextValue: ToastContainerContextValue = { | ||
| close: () => null, | ||
| intent: undefined, | ||
| bodyId: 'test-body-id', | ||
| titleId: 'test-title-id', | ||
| }; | ||
|
|
||
| function makeWrapper( | ||
| options: { | ||
| context?: Partial<ToastContainerContextValue>; | ||
| backgroundAppearance?: BackgroundAppearanceContextValue; | ||
| } = {}, | ||
| ) { | ||
| const contextValue = { ...defaultContextValue, ...options.context }; | ||
| return ({ children }: { children: React.ReactNode }) => | ||
| React.createElement( | ||
| BackgroundAppearanceProvider, | ||
| { value: options.backgroundAppearance }, | ||
| React.createElement(ToastContainerContextProvider, { value: contextValue }, children), | ||
| ); | ||
| } | ||
|
|
||
| describe('useToastBody_unstable', () => { | ||
| it('returns components shape { root: div, subtitle: div }', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastBody_unstable({}, ref), { wrapper: makeWrapper() }); | ||
| // eslint-disable-next-line @typescript-eslint/no-deprecated | ||
| expect(result.current.components).toEqual({ root: 'div', subtitle: 'div' }); | ||
| }); | ||
|
|
||
| it('always returns a root slot', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastBody_unstable({}, ref), { wrapper: makeWrapper() }); | ||
| expect(result.current.root).toBeDefined(); | ||
| }); | ||
|
|
||
| it('applies bodyId from context to root.id', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastBody_unstable({}, ref), { | ||
| wrapper: makeWrapper({ context: { bodyId: 'my-body-id' } }), | ||
| }); | ||
| expect(result.current.root.id).toBe('my-body-id'); | ||
| }); | ||
|
|
||
| it('returns undefined subtitle when subtitle prop is not provided', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastBody_unstable({}, ref), { wrapper: makeWrapper() }); | ||
| expect(result.current.subtitle).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('returns a subtitle slot when subtitle prop is provided', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastBody_unstable({ subtitle: 'sub text' }, ref), { | ||
| wrapper: makeWrapper(), | ||
| }); | ||
| expect(result.current.subtitle).toBeDefined(); | ||
| }); | ||
|
|
||
| it('reads backgroundAppearance from BackgroundAppearanceContext — undefined by default', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastBody_unstable({}, ref), { wrapper: makeWrapper() }); | ||
| expect(result.current.backgroundAppearance).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('reads backgroundAppearance="inverted" from BackgroundAppearanceContext', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastBody_unstable({}, ref), { | ||
| wrapper: makeWrapper({ backgroundAppearance: 'inverted' }), | ||
| }); | ||
| expect(result.current.backgroundAppearance).toBe('inverted'); | ||
| }); | ||
|
|
||
| it('reads backgroundAppearance="brand" from BackgroundAppearanceContext', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastBody_unstable({}, ref), { | ||
| wrapper: makeWrapper({ backgroundAppearance: 'brand' }), | ||
| }); | ||
| expect(result.current.backgroundAppearance).toBe('brand'); | ||
| }); | ||
|
|
||
| it('does not derive backgroundAppearance from props (it has no appearance prop)', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| // ToastBody has no appearance prop; backgroundAppearance must always come from context | ||
| const { result } = renderHook(() => useToastBody_unstable({}, ref), { | ||
| wrapper: makeWrapper({ backgroundAppearance: 'brand' }), | ||
| }); | ||
| expect(result.current.backgroundAppearance).toBe('brand'); | ||
| }); | ||
|
|
||
| it('spreads extra div props onto the root slot', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastBody_unstable({ className: 'body-class', 'aria-label': 'body' }, ref), { | ||
| wrapper: makeWrapper(), | ||
| }); | ||
| expect(result.current.root.className).toBe('body-class'); | ||
| expect(result.current.root['aria-label']).toBe('body'); | ||
| }); | ||
| }); |
186 changes: 186 additions & 0 deletions
186
...ges/react-components/react-toast/library/src/components/ToastTitle/useToastTitle.test.tsx
This file contains hidden or 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,186 @@ | ||
| import * as React from 'react'; | ||
| import { renderHook } from '@testing-library/react-hooks'; | ||
| import { CheckmarkCircleFilled, DiamondDismissFilled, InfoFilled, WarningFilled } from '@fluentui/react-icons'; | ||
| import { useToastTitle_unstable } from './useToastTitle'; | ||
| import { ToastContainerContextProvider } from '../../contexts/toastContainerContext'; | ||
| import { BackgroundAppearanceProvider } from '@fluentui/react-shared-contexts'; | ||
| import type { ToastContainerContextValue } from '../../contexts/toastContainerContext'; | ||
| import type { BackgroundAppearanceContextValue } from '@fluentui/react-shared-contexts'; | ||
| import type { ToastIntent } from '../../state/types'; | ||
|
|
||
| const defaultContextValue: ToastContainerContextValue = { | ||
| close: () => null, | ||
| intent: undefined, | ||
| bodyId: 'test-body-id', | ||
| titleId: 'test-title-id', | ||
| }; | ||
|
|
||
| function makeWrapper( | ||
| options: { | ||
| context?: Partial<ToastContainerContextValue>; | ||
| backgroundAppearance?: BackgroundAppearanceContextValue; | ||
| } = {}, | ||
| ) { | ||
| const contextValue = { ...defaultContextValue, ...options.context }; | ||
| return ({ children }: { children: React.ReactNode }) => | ||
| React.createElement( | ||
| BackgroundAppearanceProvider, | ||
| { value: options.backgroundAppearance }, | ||
| React.createElement(ToastContainerContextProvider, { value: contextValue }, children), | ||
| ); | ||
| } | ||
|
|
||
| describe('useToastTitle_unstable', () => { | ||
| describe('components and slots', () => { | ||
| it('returns components shape { root: div, media: div, action: div }', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { wrapper: makeWrapper() }); | ||
| // eslint-disable-next-line @typescript-eslint/no-deprecated | ||
| expect(result.current.components).toEqual({ root: 'div', media: 'div', action: 'div' }); | ||
| }); | ||
|
|
||
| it('always returns a root slot', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { wrapper: makeWrapper() }); | ||
| expect(result.current.root).toBeDefined(); | ||
| }); | ||
|
|
||
| it('returns undefined action when action prop is not provided', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { wrapper: makeWrapper() }); | ||
| expect(result.current.action).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('returns an action slot when action prop is provided', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({ action: 'Dismiss' }, ref), { | ||
| wrapper: makeWrapper(), | ||
| }); | ||
| expect(result.current.action).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('root slot', () => { | ||
| it('applies titleId from context to root.id', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { | ||
| wrapper: makeWrapper({ context: { titleId: 'my-title-id' } }), | ||
| }); | ||
| expect(result.current.root.id).toBe('my-title-id'); | ||
| }); | ||
|
|
||
| it('spreads extra div props onto the root slot', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook( | ||
| () => useToastTitle_unstable({ className: 'title-class', 'aria-label': 'title' }, ref), | ||
| { wrapper: makeWrapper() }, | ||
| ); | ||
| expect(result.current.root.className).toBe('title-class'); | ||
| expect(result.current.root['aria-label']).toBe('title'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('backgroundAppearance', () => { | ||
| it('is undefined by default', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { wrapper: makeWrapper() }); | ||
| expect(result.current.backgroundAppearance).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('reads "inverted" from BackgroundAppearanceContext', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { | ||
| wrapper: makeWrapper({ backgroundAppearance: 'inverted' }), | ||
| }); | ||
| expect(result.current.backgroundAppearance).toBe('inverted'); | ||
| }); | ||
|
|
||
| it('reads "brand" from BackgroundAppearanceContext', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { | ||
| wrapper: makeWrapper({ backgroundAppearance: 'brand' }), | ||
| }); | ||
| expect(result.current.backgroundAppearance).toBe('brand'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('intent', () => { | ||
| it('reads intent from ToastContainerContext', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { | ||
| wrapper: makeWrapper({ context: { intent: 'success' } }), | ||
| }); | ||
| expect(result.current.intent).toBe('success'); | ||
| }); | ||
|
|
||
| it('intent is undefined when context does not provide one', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { | ||
| wrapper: makeWrapper({ context: { intent: undefined } }), | ||
| }); | ||
| expect(result.current.intent).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('media slot — default icon injection by intent', () => { | ||
| it('media is undefined when no intent and no media prop', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { | ||
| wrapper: makeWrapper({ context: { intent: undefined } }), | ||
| }); | ||
| expect(result.current.media).toBeUndefined(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['success', CheckmarkCircleFilled], | ||
| ['error', DiamondDismissFilled], | ||
| ['warning', WarningFilled], | ||
| ['info', InfoFilled], | ||
| ] as [ToastIntent, React.ElementType][])('injects default icon for intent="%s"', (intent, ExpectedIcon) => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { | ||
| wrapper: makeWrapper({ context: { intent } }), | ||
| }); | ||
|
|
||
| expect(result.current.media).toBeDefined(); | ||
| const children = result.current.media?.children as React.ReactElement | undefined; | ||
| expect(children).toBeDefined(); | ||
| expect((children as React.ReactElement).type).toBe(ExpectedIcon); | ||
| }); | ||
|
|
||
| it('renders media slot (without default icon) when intent is set but media has explicit children', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const customIcon = React.createElement('span', { 'data-testid': 'custom-icon' }); | ||
| const { result } = renderHook(() => useToastTitle_unstable({ media: { children: customIcon } }, ref), { | ||
| wrapper: makeWrapper({ context: { intent: 'success' } }), | ||
| }); | ||
|
|
||
| expect(result.current.media).toBeDefined(); | ||
| const children = result.current.media?.children as React.ReactElement; | ||
| // User's children must take precedence over the default icon | ||
| expect(children).toBe(customIcon); | ||
| expect(children.type).not.toBe(CheckmarkCircleFilled); | ||
| }); | ||
|
|
||
| it('media is defined (renderByDefault) when intent is set even without media prop', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({}, ref), { | ||
| wrapper: makeWrapper({ context: { intent: 'info' } }), | ||
| }); | ||
| // renderByDefault: !!intent → media must exist | ||
| expect(result.current.media).toBeDefined(); | ||
| }); | ||
|
|
||
| it('media children are still the default icon when media prop is provided without children and intent is set', () => { | ||
| const ref = React.createRef<HTMLElement>(); | ||
| const { result } = renderHook(() => useToastTitle_unstable({ media: {} }, ref), { | ||
| wrapper: makeWrapper({ context: { intent: 'warning' } }), | ||
| }); | ||
|
|
||
| expect(result.current.media).toBeDefined(); | ||
| const children = result.current.media?.children as React.ReactElement | undefined; | ||
| expect(children).toBeDefined(); | ||
| expect((children as React.ReactElement).type).toBe(WarningFilled); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.