|
| 1 | +/* |
| 2 | +
|
| 3 | + MIT License |
| 4 | +
|
| 5 | + Copyright (c) 2020 Looker Data Sciences, Inc. |
| 6 | +
|
| 7 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + of this software and associated documentation files (the "Software"), to deal |
| 9 | + in the Software without restriction, including without limitation the rights |
| 10 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + copies of the Software, and to permit persons to whom the Software is |
| 12 | + furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | + The above copyright notice and this permission notice shall be included in all |
| 15 | + copies or substantial portions of the Software. |
| 16 | +
|
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + SOFTWARE. |
| 24 | +
|
| 25 | + */ |
| 26 | + |
| 27 | +import 'jest-styled-components' |
| 28 | +import React, { useContext } from 'react' |
| 29 | +import { renderWithTheme } from '@looker/components-test-utils' |
| 30 | +import { |
| 31 | + screen, |
| 32 | + fireEvent, |
| 33 | + waitForElementToBeRemoved, |
| 34 | +} from '@testing-library/react' |
| 35 | +import { DialogContext } from '../Dialog/DialogContext' |
| 36 | +import { Drawer } from './Drawer' |
| 37 | +import { UseDrawerHook, RenderProps } from './stories/Drawer.story' |
| 38 | + |
| 39 | +const SimpleContent = () => { |
| 40 | + const { closeModal } = useContext(DialogContext) |
| 41 | + |
| 42 | + return ( |
| 43 | + <> |
| 44 | + Drawer content |
| 45 | + <button onClick={closeModal}>Done</button> |
| 46 | + </> |
| 47 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +describe('Drawer', () => { |
| 51 | + test('Verify initial state', () => { |
| 52 | + renderWithTheme(<Drawer content={<SimpleContent />} />) |
| 53 | + expect(screen.queryByText('Drawer content')).not.toBeInTheDocument() |
| 54 | + }) |
| 55 | + |
| 56 | + test('defaultOpen prop', async () => { |
| 57 | + renderWithTheme(<Drawer defaultOpen content={<SimpleContent />} />) |
| 58 | + expect(screen.queryByText('Drawer content')).toBeInTheDocument() |
| 59 | + const doneButton = screen.getByText('Done') |
| 60 | + fireEvent.click(doneButton) |
| 61 | + await waitForElementToBeRemoved(() => screen.getByText('Drawer content')) |
| 62 | + }) |
| 63 | + |
| 64 | + test('Drawer can be opened & closed', async () => { |
| 65 | + renderWithTheme( |
| 66 | + <Drawer content={<SimpleContent />}> |
| 67 | + <a>Open Drawer</a> |
| 68 | + </Drawer> |
| 69 | + ) |
| 70 | + |
| 71 | + // Dialog closed |
| 72 | + expect(screen.queryByText('Drawer content')).not.toBeInTheDocument() |
| 73 | + |
| 74 | + // Open Drawer |
| 75 | + const link = screen.getByText('Open Drawer') |
| 76 | + expect(link).toBeInTheDocument() |
| 77 | + fireEvent.click(link) |
| 78 | + expect(screen.queryByText('Drawer content')).toBeInTheDocument() |
| 79 | + |
| 80 | + // Close the Drawer |
| 81 | + const doneButton = screen.getByText('Done') |
| 82 | + fireEvent.click(doneButton) |
| 83 | + await waitForElementToBeRemoved(() => screen.getByText('Drawer content')) |
| 84 | + }) |
| 85 | + |
| 86 | + test('useDrawer hook', async () => { |
| 87 | + renderWithTheme(<UseDrawerHook />) |
| 88 | + |
| 89 | + // Dialog closed |
| 90 | + expect( |
| 91 | + screen.queryByText('The Constitution of the United States') |
| 92 | + ).not.toBeInTheDocument() |
| 93 | + |
| 94 | + // Open Drawer |
| 95 | + const link = screen.getByText('Open Drawer') |
| 96 | + fireEvent.click(link) |
| 97 | + expect( |
| 98 | + screen.queryByText('The Constitution of the United States') |
| 99 | + ).toBeInTheDocument() |
| 100 | + |
| 101 | + // Close the Drawer |
| 102 | + const doneButton = screen.getByText('Done Reading') |
| 103 | + fireEvent.click(doneButton) |
| 104 | + await waitForElementToBeRemoved(() => |
| 105 | + screen.getByText('The Constitution of the United States') |
| 106 | + ) |
| 107 | + }) |
| 108 | + |
| 109 | + test('renderProps form', async () => { |
| 110 | + renderWithTheme(<RenderProps />) |
| 111 | + |
| 112 | + // Dialog closed |
| 113 | + expect( |
| 114 | + screen.queryByText('The Constitution of the United States') |
| 115 | + ).not.toBeInTheDocument() |
| 116 | + |
| 117 | + // Open Drawer |
| 118 | + const link = screen.getByText('Open Drawer') |
| 119 | + fireEvent.click(link) |
| 120 | + expect( |
| 121 | + screen.queryByText('The Constitution of the United States') |
| 122 | + ).toBeInTheDocument() |
| 123 | + |
| 124 | + // Close the Drawer |
| 125 | + const doneButton = screen.getByText('Done Reading') |
| 126 | + fireEvent.click(doneButton) |
| 127 | + await waitForElementToBeRemoved(() => |
| 128 | + screen.getByText('The Constitution of the United States') |
| 129 | + ) |
| 130 | + }) |
| 131 | + |
| 132 | + xtest('Backdrop can be clicked to close', () => true) |
| 133 | + xtest('Composition form: controlled', () => true) |
| 134 | + xtest('canClose callback', () => true) |
| 135 | + xtest('onClose callback', () => true) |
| 136 | +}) |
0 commit comments