|
| 1 | +/* |
| 2 | + * Copyright 2021 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import {act, render} from '@testing-library/react'; |
| 14 | +import {Content, Footer, Header} from '@react-spectrum/view'; |
| 15 | +import {ContextualHelp} from '../'; |
| 16 | +import {Link} from '@react-spectrum/link'; |
| 17 | +import {Provider} from '@react-spectrum/provider'; |
| 18 | +import React from 'react'; |
| 19 | +import {theme} from '@react-spectrum/theme-default'; |
| 20 | +import {triggerPress} from '@react-spectrum/test-utils'; |
| 21 | + |
| 22 | +describe('ContextualHelp', function () { |
| 23 | + beforeAll(() => { |
| 24 | + jest.useFakeTimers(); |
| 25 | + }); |
| 26 | + afterAll(() => { |
| 27 | + jest.clearAllMocks(); |
| 28 | + jest.useRealTimers(); |
| 29 | + }); |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + // this needs to be a setTimeout so that the dialog can be removed from the dom before the callback is invoked |
| 33 | + jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => setTimeout(() => cb(), 0)); |
| 34 | + }); |
| 35 | + |
| 36 | + it('renders quiet action button', function () { |
| 37 | + let {getByRole, queryByRole} = render( |
| 38 | + <Provider theme={theme}> |
| 39 | + <ContextualHelp> |
| 40 | + <Header>Test title</Header> |
| 41 | + </ContextualHelp> |
| 42 | + </Provider> |
| 43 | + ); |
| 44 | + |
| 45 | + expect(queryByRole('dialog')).toBeNull(); |
| 46 | + |
| 47 | + let button = getByRole('button'); |
| 48 | + expect(button).toBeVisible(); |
| 49 | + expect(button).toHaveClass('spectrum-ActionButton--quiet'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('opens a popover', function () { |
| 53 | + let {getByRole, queryByRole, getByTestId, getByText} = render( |
| 54 | + <Provider theme={theme}> |
| 55 | + <ContextualHelp> |
| 56 | + <Header>Test title</Header> |
| 57 | + </ContextualHelp> |
| 58 | + </Provider> |
| 59 | + ); |
| 60 | + |
| 61 | + expect(queryByRole('dialog')).toBeNull(); |
| 62 | + |
| 63 | + let button = getByRole('button'); |
| 64 | + triggerPress(button); |
| 65 | + |
| 66 | + act(() => { |
| 67 | + jest.runAllTimers(); |
| 68 | + }); |
| 69 | + |
| 70 | + let dialog = getByRole('dialog'); |
| 71 | + expect(dialog).toBeVisible(); |
| 72 | + |
| 73 | + let modal = getByTestId('popover'); |
| 74 | + expect(modal).toBeVisible(); |
| 75 | + |
| 76 | + expect(getByText('Test title')).toBeVisible(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('renders content', function () { |
| 80 | + let {getByRole, getByText} = render( |
| 81 | + <Provider theme={theme}> |
| 82 | + <ContextualHelp> |
| 83 | + <Header>Test title</Header> |
| 84 | + <Content>Help content</Content> |
| 85 | + </ContextualHelp> |
| 86 | + </Provider> |
| 87 | + ); |
| 88 | + |
| 89 | + let button = getByRole('button'); |
| 90 | + triggerPress(button); |
| 91 | + |
| 92 | + act(() => { |
| 93 | + jest.runAllTimers(); |
| 94 | + }); |
| 95 | + |
| 96 | + let dialog = getByRole('dialog'); |
| 97 | + expect(dialog).toBeVisible(); |
| 98 | + |
| 99 | + const content = getByText('Help content'); |
| 100 | + expect(content).toBeVisible(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('renders a link', function () { |
| 104 | + let {getByRole, getByText} = render( |
| 105 | + <Provider theme={theme}> |
| 106 | + <ContextualHelp> |
| 107 | + <Header>Test title</Header> |
| 108 | + <Content>Help content</Content> |
| 109 | + <Footer> |
| 110 | + <Link>Test link</Link> |
| 111 | + </Footer> |
| 112 | + </ContextualHelp> |
| 113 | + </Provider> |
| 114 | + ); |
| 115 | + |
| 116 | + let button = getByRole('button'); |
| 117 | + triggerPress(button); |
| 118 | + |
| 119 | + act(() => { |
| 120 | + jest.runAllTimers(); |
| 121 | + }); |
| 122 | + |
| 123 | + let dialog = getByRole('dialog'); |
| 124 | + expect(dialog).toBeVisible(); |
| 125 | + |
| 126 | + const content = getByText('Test link'); |
| 127 | + expect(content).toBeVisible(); |
| 128 | + expect(content.parentElement).toHaveClass('react-spectrum-ContextualHelp-footer'); |
| 129 | + }); |
| 130 | +}); |
0 commit comments