|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2015 - present Instructure, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +import { useState } from 'react' |
| 25 | +import { Modal, View } from '@instructure/ui' |
| 26 | +import 'cypress-real-events' |
| 27 | + |
| 28 | +import '../support/component' |
| 29 | + |
| 30 | +describe('<Modal/>', () => { |
| 31 | + it('should call onDismiss prop when Esc key pressed by default', () => { |
| 32 | + const onDismissSpy = cy.spy() |
| 33 | + cy.mount( |
| 34 | + <Modal |
| 35 | + open={true} |
| 36 | + onDismiss={onDismissSpy} |
| 37 | + label="Modal Dialog" |
| 38 | + shouldReturnFocus={false} |
| 39 | + > |
| 40 | + <p>Modal body text</p> |
| 41 | + </Modal> |
| 42 | + ) |
| 43 | + cy.wait(500) |
| 44 | + cy.root().should('contain', 'Modal body text') |
| 45 | + |
| 46 | + cy.get('body').trigger('keydown', { keyCode: 27 }).wait(100) |
| 47 | + cy.get('body').trigger('keyup', { keyCode: 27 }).wait(100) |
| 48 | + cy.wait(500) |
| 49 | + cy.wrap(onDismissSpy).should('have.been.calledOnce') |
| 50 | + }) |
| 51 | + |
| 52 | + it('should not call stale callbacks', () => { |
| 53 | + const handleDismissSpy = cy.spy() |
| 54 | + interface ExampleProps { |
| 55 | + handleDismiss: (value: number) => void |
| 56 | + } |
| 57 | + |
| 58 | + function Example({ handleDismiss }: ExampleProps) { |
| 59 | + const [value, setValue] = useState(0) |
| 60 | + |
| 61 | + function onButtonClick() { |
| 62 | + setValue(value + 1) |
| 63 | + } |
| 64 | + |
| 65 | + return ( |
| 66 | + <View> |
| 67 | + <Modal |
| 68 | + label="Modal" |
| 69 | + open |
| 70 | + onDismiss={() => { |
| 71 | + handleDismiss(value) |
| 72 | + }} |
| 73 | + > |
| 74 | + <Modal.Body> |
| 75 | + <p>Modal body text</p> |
| 76 | + <div id="value-indicator">{value}</div> |
| 77 | + <button id="increment-btn" onClick={onButtonClick}> |
| 78 | + Increment Button |
| 79 | + </button> |
| 80 | + </Modal.Body> |
| 81 | + </Modal> |
| 82 | + </View> |
| 83 | + ) |
| 84 | + } |
| 85 | + cy.mount(<Example handleDismiss={handleDismissSpy} />) |
| 86 | + cy.wait(500) |
| 87 | + cy.root().should('contain', 'Modal body text') |
| 88 | + |
| 89 | + cy.get('#increment-btn').click().wait(200) |
| 90 | + cy.root().should('contain', 'Modal body text').wait(200) |
| 91 | + cy.get('#value-indicator').should('contain', '1').wait(200) |
| 92 | + cy.wrap(handleDismissSpy).should('not.have.been.called') |
| 93 | + |
| 94 | + cy.get('body').click(0, 0).wait(200) |
| 95 | + cy.wrap(handleDismissSpy).should('have.been.calledOnceWith', 1) |
| 96 | + }) |
| 97 | +}) |
0 commit comments