-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
FocusTrapZone: port most tests to cypress #21741
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5045e52
FocusTrapZone: port most tests to cypress
ecraig12345 ea0baa3
fix e2e scopes
ecraig12345 b23e6d4
make tests more realistic and improve comments
ecraig12345 092b312
make focus stack test more realistic
ecraig12345 d2a17f0
make tests more reliable in headless mode
ecraig12345 3b164cb
cypress: save screenshots on local headless runs
ecraig12345 e2f5677
update examples
ecraig12345 463f69f
move e2e utils to common location
ecraig12345 51b4397
story style updates
ecraig12345 7d4930c
fix checks for nothing focused
ecraig12345 9ff5d82
PR feedback
ecraig12345 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
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,33 @@ | ||
import * as React from 'react'; | ||
|
||
/** | ||
* Define a global function on `window` and clean it up after the test finishes. | ||
* NOTE: This only runs once (updates to the function are not respected). | ||
*/ | ||
export function useGlobal<Globals>(name: keyof Globals, func: Required<Globals>[typeof name]) { | ||
React.useEffect(() => { | ||
((window as unknown) as Globals)[name] = func; | ||
return () => { | ||
// Clean up the global to avoid timing issues where a test tries to call the version of a | ||
// global defined by a previous test (this can happen in headless mode) | ||
delete ((window as unknown) as Globals)[name]; | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps -- should only run on mount | ||
}, []); | ||
} | ||
|
||
export type UsePropsGlobals<TProps> = { | ||
/** Callback allowing a test to update the value returned by `useProps` inside a story. */ | ||
setProps?: (props: TProps) => void; | ||
}; | ||
|
||
/** | ||
* Define a global `window.setProps` to set props for the story, and clean it up | ||
* after the test finishes. | ||
* @returns the latest props | ||
*/ | ||
export function useProps<TProps>() { | ||
const [props, setProps] = React.useState<TProps | undefined>(); | ||
useGlobal<UsePropsGlobals<TProps>>('setProps' as const, setProps); | ||
return props; | ||
} |
59 changes: 59 additions & 0 deletions
59
packages/react-examples/src/react/FocusTrapZone/e2e/FocusTrapZone.FocusStack.stories.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,59 @@ | ||
import * as React from 'react'; | ||
import { FocusTrapZone } from '@fluentui/react'; | ||
import { useGlobal } from '../../../e2e/utils'; | ||
import { FTZTestGlobals } from './types'; | ||
import { rootClass } from './shared'; | ||
|
||
/** | ||
* It maintains a proper stack of FocusTrapZones as more are mounted/unmounted | ||
*/ | ||
export const FocusStack = () => { | ||
// Whether to render each FocusTrapZone | ||
const [shouldRender, setShouldRender] = React.useState([true, false, false, false, false]); | ||
|
||
const updateFTZ = (num: 1 | 2 | 3 | 4, newValue: boolean) => { | ||
setShouldRender(prevValues => { | ||
const newValues = [...prevValues]; | ||
newValues[num] = newValue; | ||
return newValues; | ||
}); | ||
}; | ||
|
||
useGlobal<FTZTestGlobals>('getFocusStack', () => FocusTrapZone.focusStack); | ||
|
||
return ( | ||
<div className={rootClass}> | ||
<FocusTrapZone id="ftz0"> | ||
ftz0 | ||
<button onClick={() => updateFTZ(1, true)}>add ftz1</button> | ||
<button onClick={() => updateFTZ(3, true)}>add ftz3</button> | ||
<button onClick={() => updateFTZ(4, true)}>add ftz4</button> | ||
</FocusTrapZone> | ||
|
||
{shouldRender[1] && ( | ||
<FocusTrapZone id="ftz1"> | ||
ftz1 | ||
<button onClick={() => updateFTZ(2, true)}>add ftz2</button> | ||
</FocusTrapZone> | ||
)} | ||
{shouldRender[2] && ( | ||
<FocusTrapZone id="ftz2"> | ||
ftz2 | ||
<button onClick={() => updateFTZ(1, false)}>remove ftz1</button> | ||
<button onClick={() => updateFTZ(2, false)}>remove ftz2</button> | ||
</FocusTrapZone> | ||
)} | ||
{shouldRender[3] && ( | ||
<FocusTrapZone id="ftz3" forceFocusInsideTrap={false}> | ||
ftz3 | ||
<button onClick={() => updateFTZ(3, false)}>remove ftz3</button> | ||
</FocusTrapZone> | ||
)} | ||
{shouldRender[4] && ( | ||
<FocusTrapZone id="ftz4" disabled> | ||
ftz4 | ||
</FocusTrapZone> | ||
)} | ||
</div> | ||
); | ||
}; |
30 changes: 30 additions & 0 deletions
30
...ages/react-examples/src/react/FocusTrapZone/e2e/FocusTrapZone.ImperativeFocus.stories.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,30 @@ | ||
import * as React from 'react'; | ||
import { FocusZone, FocusTrapZone } from '@fluentui/react'; | ||
import type { IFocusTrapZone, IFocusTrapZoneProps } from '@fluentui/react'; | ||
import { useGlobal, useProps } from '../../../e2e/utils'; | ||
import type { FTZTestGlobals } from './types'; | ||
import { rootClass } from './shared'; | ||
|
||
/** Imperatively focusing the FTZ */ | ||
export const ImperativeFocus = () => { | ||
const props = useProps<IFocusTrapZoneProps>(); | ||
const focusTrapZoneRef = React.useRef<IFocusTrapZone>(null); | ||
|
||
useGlobal<FTZTestGlobals>('imperativeFocus', () => focusTrapZoneRef.current?.focus()); | ||
|
||
return ( | ||
// don't render until props have been set | ||
props && ( | ||
<div className={rootClass}> | ||
<FocusTrapZone disableFirstFocus componentRef={focusTrapZoneRef} {...props}> | ||
<button>first</button> | ||
<FocusZone> | ||
<button>mid</button> | ||
<button>last</button> | ||
</FocusZone> | ||
</FocusTrapZone> | ||
<button>after</button> | ||
</div> | ||
) | ||
); | ||
}; |
27 changes: 27 additions & 0 deletions
27
...ages/react-examples/src/react/FocusTrapZone/e2e/FocusTrapZone.NoTabbableItems.stories.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,27 @@ | ||
import * as React from 'react'; | ||
import { FocusTrapZone } from '@fluentui/react'; | ||
import type { IFocusTrapZoneProps } from '@fluentui/react'; | ||
import { useProps } from '../../../e2e/utils'; | ||
import { rootClass } from './shared'; | ||
|
||
/** | ||
* Tab and shift-tab when the FTZ contains 0 tabbable items | ||
*/ | ||
export const NoTabbableItems = () => { | ||
const props = useProps<IFocusTrapZoneProps>(); | ||
|
||
return ( | ||
// don't render until props have been set | ||
props && ( | ||
<div className={rootClass}> | ||
<button>before</button> | ||
<FocusTrapZone forceFocusInsideTrap {...props}> | ||
<button tabIndex={-1}>first</button> | ||
<button tabIndex={-1}>mid</button> | ||
<button tabIndex={-1}>last</button> | ||
</FocusTrapZone> | ||
<button>after</button> | ||
</div> | ||
) | ||
); | ||
}; |
31 changes: 31 additions & 0 deletions
31
packages/react-examples/src/react/FocusTrapZone/e2e/FocusTrapZone.PropValues.stories.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,31 @@ | ||
import * as React from 'react'; | ||
import { FocusTrapZone } from '@fluentui/react'; | ||
import type { IFocusTrapZoneProps } from '@fluentui/react'; | ||
import { useProps } from '../../../e2e/utils'; | ||
import { rootClass } from './shared'; | ||
|
||
/** Respects default and explicit prop values */ | ||
export const PropValues = () => { | ||
const [buttonClicked, setButtonClicked] = React.useState(''); | ||
const props = useProps<IFocusTrapZoneProps>(); | ||
|
||
return ( | ||
// don't render until props have been set | ||
props && ( | ||
<div className={rootClass} onClick={ev => setButtonClicked((ev.target as HTMLButtonElement).textContent || '')}> | ||
<span id="buttonClicked" style={{ display: 'block' /* avoid inherited div styling */ }}> | ||
clicked {buttonClicked} | ||
</span> | ||
<button>before</button> | ||
<FocusTrapZone {...props}> | ||
<button>first</button> | ||
<button>mid</button> | ||
<button className="last-class" id="last"> | ||
last | ||
</button> | ||
</FocusTrapZone> | ||
<button>after</button> | ||
</div> | ||
) | ||
); | ||
}; |
32 changes: 32 additions & 0 deletions
32
...examples/src/react/FocusTrapZone/e2e/FocusTrapZone.TabWrappingButtonFocusZone.stories.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,32 @@ | ||
import * as React from 'react'; | ||
import { FocusZone, FocusTrapZone, FocusZoneDirection } from '@fluentui/react'; | ||
import { rootClass } from './shared'; | ||
|
||
/** | ||
* Tab and shift-tab wrap at extreme ends of the FTZ: | ||
* | ||
* can tab between a button and a FocusZone | ||
*/ | ||
export const TabWrappingButtonFocusZone = () => { | ||
return ( | ||
<div className={rootClass}> | ||
<FocusTrapZone forceFocusInsideTrap={false}> | ||
<div> | ||
<button>first</button> | ||
</div> | ||
<FocusZone direction={FocusZoneDirection.horizontal}> | ||
<div> | ||
<button>fzFirst</button> | ||
</div> | ||
<div> | ||
<div> | ||
<button>fzMid1</button> | ||
<button>fzMid2</button> | ||
<button>fzLast</button> | ||
</div> | ||
</div> | ||
</FocusZone> | ||
</FocusTrapZone> | ||
</div> | ||
); | ||
}; |
31 changes: 31 additions & 0 deletions
31
...xamples/src/react/FocusTrapZone/e2e/FocusTrapZone.TabWrappingFocusZoneBumpers.stories.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,31 @@ | ||
import * as React from 'react'; | ||
import { FocusZone, FocusTrapZone, FocusZoneDirection } from '@fluentui/react'; | ||
import { rootClass } from './shared'; | ||
|
||
/** | ||
* Tab and shift-tab wrap at extreme ends of the FTZ: | ||
* | ||
* can trap focus when FTZ bookmark elements are FocusZones, | ||
* and those elements have inner elements focused that are not the first inner element | ||
*/ | ||
export const TabWrappingFocusZoneBumpers = () => { | ||
return ( | ||
<div className={rootClass}> | ||
<button>before</button> | ||
<FocusTrapZone forceFocusInsideTrap={false}> | ||
<FocusZone direction={FocusZoneDirection.horizontal}> | ||
<button>fz1First</button> | ||
<button>fz1Mid</button> | ||
<button>fz1Last</button> | ||
</FocusZone> | ||
<button>mid</button> | ||
<FocusZone direction={FocusZoneDirection.horizontal}> | ||
<button>fz2First</button> | ||
<button>fz2Mid</button> | ||
<button>fz2Last</button> | ||
</FocusZone> | ||
</FocusTrapZone> | ||
<button>after</button> | ||
</div> | ||
); | ||
}; |
28 changes: 9 additions & 19 deletions
28
...-examples/src/react/FocusTrapZone/e2e/FocusTrapZone.TabWrappingMultiFocusZone.stories.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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having two packages as scopes caused this PR's first build to try and run tests for deps of react-components even though react-components hadn't been built or deployed. Hopefully splitting into two steps will fix it. (Problem is it won't be possible to tell in this PR, because changing a pipeline file makes the PR build all packages.)