|
| 1 | +import * as React from 'react'; |
| 2 | +import { mount as mountBase } from '@fluentui/scripts-cypress'; |
| 3 | + |
| 4 | +import { FluentProvider } from '@fluentui/react-provider'; |
| 5 | +import { teamsLightTheme } from '@fluentui/react-theme'; |
| 6 | + |
| 7 | +import { |
| 8 | + TeachingPopover, |
| 9 | + TeachingPopoverTrigger, |
| 10 | + TeachingPopoverSurface, |
| 11 | + TeachingPopoverBody, |
| 12 | + TeachingPopoverTitle, |
| 13 | +} from '@fluentui/react-teaching-popover'; |
| 14 | +import type { TeachingPopoverProps } from '@fluentui/react-teaching-popover'; |
| 15 | +import type { JSXElement } from '@fluentui/react-utilities'; |
| 16 | + |
| 17 | +const mount = (element: JSXElement) => { |
| 18 | + mountBase(<FluentProvider theme={teamsLightTheme}>{element}</FluentProvider>); |
| 19 | +}; |
| 20 | + |
| 21 | +const triggerSelector = '[aria-expanded]'; |
| 22 | +// TeachingPopover defaults to `trapFocus: true`, so the surface is rendered with role="dialog". |
| 23 | +const surfaceSelector = '[role="dialog"]'; |
| 24 | + |
| 25 | +describe('TeachingPopover', () => { |
| 26 | + (['uncontrolled', 'controlled'] as const).forEach(scenario => { |
| 27 | + const UncontrolledExample = () => ( |
| 28 | + <TeachingPopover> |
| 29 | + <TeachingPopoverTrigger disableButtonEnhancement> |
| 30 | + <button>Trigger</button> |
| 31 | + </TeachingPopoverTrigger> |
| 32 | + <TeachingPopoverSurface> |
| 33 | + <TeachingPopoverBody> |
| 34 | + <TeachingPopoverTitle>Title</TeachingPopoverTitle> |
| 35 | + <div>This is a teaching popover</div> |
| 36 | + </TeachingPopoverBody> |
| 37 | + </TeachingPopoverSurface> |
| 38 | + </TeachingPopover> |
| 39 | + ); |
| 40 | + |
| 41 | + const ControlledExample = () => { |
| 42 | + const [open, setOpen] = React.useState(false); |
| 43 | + |
| 44 | + return ( |
| 45 | + <TeachingPopover open={open} onOpenChange={(e, data) => setOpen(data.open)}> |
| 46 | + <TeachingPopoverTrigger disableButtonEnhancement> |
| 47 | + <button>Trigger</button> |
| 48 | + </TeachingPopoverTrigger> |
| 49 | + <TeachingPopoverSurface> |
| 50 | + <TeachingPopoverBody> |
| 51 | + <TeachingPopoverTitle>Title</TeachingPopoverTitle> |
| 52 | + <div>This is a teaching popover</div> |
| 53 | + </TeachingPopoverBody> |
| 54 | + </TeachingPopoverSurface> |
| 55 | + </TeachingPopover> |
| 56 | + ); |
| 57 | + }; |
| 58 | + |
| 59 | + describe(scenario, () => { |
| 60 | + const Example = scenario === 'controlled' ? ControlledExample : UncontrolledExample; |
| 61 | + |
| 62 | + beforeEach(() => { |
| 63 | + mount(<Example />); |
| 64 | + cy.get('body').click('bottomRight'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should open when clicked', () => { |
| 68 | + cy.get(triggerSelector).click().get(surfaceSelector).should('be.visible'); |
| 69 | + }); |
| 70 | + |
| 71 | + (['{enter}', 'Space'] as const).forEach(key => { |
| 72 | + it(`should open with ${key}`, () => { |
| 73 | + cy.get(triggerSelector).focus().realPress(key); |
| 74 | + cy.get(surfaceSelector).should('be.visible'); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should dismiss on click outside', () => { |
| 79 | + cy.get(triggerSelector).click().get('body').click('bottomRight').get(surfaceSelector).should('not.exist'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should dismiss on Escape keydown', () => { |
| 83 | + cy.get(triggerSelector).click().realPress('Escape'); |
| 84 | + cy.get(surfaceSelector).should('not.exist'); |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('focus trap default', () => { |
| 90 | + it('should trap focus by default', () => { |
| 91 | + mount( |
| 92 | + <TeachingPopover> |
| 93 | + <TeachingPopoverTrigger disableButtonEnhancement> |
| 94 | + <button>Trigger</button> |
| 95 | + </TeachingPopoverTrigger> |
| 96 | + <TeachingPopoverSurface> |
| 97 | + <button>One</button> |
| 98 | + <button>Two</button> |
| 99 | + </TeachingPopoverSurface> |
| 100 | + </TeachingPopover>, |
| 101 | + ); |
| 102 | + |
| 103 | + cy.get(triggerSelector).focus().realPress('Enter'); |
| 104 | + |
| 105 | + cy.contains('One').should('have.focus').realPress('Tab'); |
| 106 | + cy.contains('Two').should('have.focus').realPress('Tab'); |
| 107 | + cy.contains('One').should('have.focus'); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('updating content', () => { |
| 112 | + const Example = () => { |
| 113 | + const [visible, setVisible] = React.useState(false); |
| 114 | + const changeContent = () => setVisible(true); |
| 115 | + const onOpenChange: TeachingPopoverProps['onOpenChange'] = (e, data) => { |
| 116 | + if (data.open === false) { |
| 117 | + setVisible(false); |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + return ( |
| 122 | + <TeachingPopover onOpenChange={onOpenChange}> |
| 123 | + <TeachingPopoverTrigger disableButtonEnhancement> |
| 124 | + <button>Trigger</button> |
| 125 | + </TeachingPopoverTrigger> |
| 126 | + <TeachingPopoverSurface> |
| 127 | + {visible ? ( |
| 128 | + <div>The second panel</div> |
| 129 | + ) : ( |
| 130 | + <div> |
| 131 | + <button onClick={changeContent}>Action</button> |
| 132 | + </div> |
| 133 | + )} |
| 134 | + </TeachingPopoverSurface> |
| 135 | + </TeachingPopover> |
| 136 | + ); |
| 137 | + }; |
| 138 | + |
| 139 | + it('should not close when inner content changes', () => { |
| 140 | + mount(<Example />); |
| 141 | + cy.get(triggerSelector) |
| 142 | + .click() |
| 143 | + .get(surfaceSelector) |
| 144 | + .within(() => { |
| 145 | + cy.contains('Action').click(); |
| 146 | + }) |
| 147 | + .get(surfaceSelector) |
| 148 | + .should('exist') |
| 149 | + .contains('The second panel'); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments