|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import * as React from 'react'; |
| 4 | +import { useRef } from 'react'; |
| 5 | +import { fireEvent, render } from '@testing-library/react'; |
| 6 | + |
| 7 | +import FeaturePrompt, { FeaturePromptProps } from '../../../../../lib/components/internal/do-not-use/feature-prompt'; |
| 8 | +import FeaturePromptWrapper from '../../../../../lib/components/test-utils/dom/internal/feature-prompt'; |
| 9 | + |
| 10 | +function renderComponent(jsx: React.ReactElement) { |
| 11 | + const { container, ...rest } = render(jsx); |
| 12 | + const wrapper = new FeaturePromptWrapper(container); |
| 13 | + |
| 14 | + return { wrapper, ...rest }; |
| 15 | +} |
| 16 | + |
| 17 | +const TestComponent = ({ onDismiss }: { onDismiss?: FeaturePromptProps['onDismiss'] }) => { |
| 18 | + const featurePromptRef = useRef<FeaturePromptProps.Ref>(null); |
| 19 | + const trackRef = useRef<HTMLDivElement>(null); |
| 20 | + |
| 21 | + return ( |
| 22 | + <div> |
| 23 | + <div ref={trackRef}>tracked element</div> |
| 24 | + <button |
| 25 | + data-testid="trigger-button" |
| 26 | + onClick={() => { |
| 27 | + featurePromptRef.current?.show(); |
| 28 | + }} |
| 29 | + > |
| 30 | + trigger the feature prompt |
| 31 | + </button> |
| 32 | + <button |
| 33 | + data-testid="dismiss-button" |
| 34 | + onClick={() => { |
| 35 | + featurePromptRef.current?.dismiss(); |
| 36 | + }} |
| 37 | + > |
| 38 | + dismiss the feature prompt |
| 39 | + </button> |
| 40 | + <FeaturePrompt |
| 41 | + ref={featurePromptRef} |
| 42 | + position="left" |
| 43 | + header={<div>header</div>} |
| 44 | + content={<div>content</div>} |
| 45 | + onDismiss={onDismiss} |
| 46 | + getTrack={() => trackRef.current} |
| 47 | + trackKey="track-element" |
| 48 | + /> |
| 49 | + </div> |
| 50 | + ); |
| 51 | +}; |
| 52 | + |
| 53 | +describe('FeaturePrompt', () => { |
| 54 | + test('should render feature prompt only after calling show method', () => { |
| 55 | + const { getByTestId, wrapper } = renderComponent(<TestComponent />); |
| 56 | + |
| 57 | + expect(wrapper.findContent()).toBeFalsy(); |
| 58 | + |
| 59 | + getByTestId('trigger-button').click(); |
| 60 | + |
| 61 | + expect(wrapper.findHeader()!.getElement()).toHaveTextContent('header'); |
| 62 | + expect(wrapper.findContent()!.getElement()).toHaveTextContent('content'); |
| 63 | + }); |
| 64 | + |
| 65 | + test('should dismiss feature prompt on shifting focus away', () => { |
| 66 | + const { getByTestId, wrapper } = renderComponent(<TestComponent />); |
| 67 | + |
| 68 | + expect(wrapper.findContent()).toBeFalsy(); |
| 69 | + |
| 70 | + getByTestId('trigger-button').click(); |
| 71 | + |
| 72 | + expect(wrapper.findHeader()!.getElement()).toHaveTextContent('header'); |
| 73 | + expect(wrapper.findContent()!.getElement()).toHaveTextContent('content'); |
| 74 | + |
| 75 | + fireEvent.blur(wrapper.findContent()!.getElement()); |
| 76 | + expect(wrapper.findContent()).toBeFalsy(); |
| 77 | + }); |
| 78 | + |
| 79 | + test('should call component onDismiss when dismissed via close button', () => { |
| 80 | + const onDismissMock = jest.fn(); |
| 81 | + const { getByTestId, wrapper } = renderComponent(<TestComponent onDismiss={onDismissMock} />); |
| 82 | + |
| 83 | + getByTestId('trigger-button').click(); |
| 84 | + expect(wrapper.findContent()).toBeTruthy(); |
| 85 | + |
| 86 | + wrapper.findDismissButton()!.click(); |
| 87 | + |
| 88 | + expect(onDismissMock).toHaveBeenCalledTimes(1); |
| 89 | + expect(wrapper.findContent()).toBeFalsy(); |
| 90 | + }); |
| 91 | + |
| 92 | + test('should call component onDismiss when dismissed programmatically', () => { |
| 93 | + const onDismissMock = jest.fn(); |
| 94 | + const { getByTestId, wrapper } = renderComponent(<TestComponent onDismiss={onDismissMock} />); |
| 95 | + |
| 96 | + getByTestId('trigger-button').click(); |
| 97 | + expect(wrapper.findContent()).toBeTruthy(); |
| 98 | + |
| 99 | + getByTestId('dismiss-button').click(); |
| 100 | + |
| 101 | + expect(onDismissMock).toHaveBeenCalledTimes(1); |
| 102 | + expect(wrapper.findContent()).toBeFalsy(); |
| 103 | + }); |
| 104 | + |
| 105 | + test('should call component onDismiss when dismissed via blur', () => { |
| 106 | + const onDismissMock = jest.fn(); |
| 107 | + const { getByTestId, wrapper } = renderComponent(<TestComponent onDismiss={onDismissMock} />); |
| 108 | + |
| 109 | + getByTestId('trigger-button').click(); |
| 110 | + expect(wrapper.findContent()).toBeTruthy(); |
| 111 | + |
| 112 | + fireEvent.blur(wrapper.findContent()!.getElement()); |
| 113 | + |
| 114 | + expect(onDismissMock).toHaveBeenCalledTimes(1); |
| 115 | + expect(wrapper.findContent()).toBeFalsy(); |
| 116 | + }); |
| 117 | + |
| 118 | + test('should not dismiss when blur relatedTarget is within popover body', () => { |
| 119 | + const onDismissMock = jest.fn(); |
| 120 | + const { getByTestId, wrapper } = renderComponent(<TestComponent onDismiss={onDismissMock} />); |
| 121 | + |
| 122 | + getByTestId('trigger-button').click(); |
| 123 | + expect(wrapper.findContent()).toBeTruthy(); |
| 124 | + |
| 125 | + const popoverBody = wrapper.findContent()!.getElement(); |
| 126 | + const dismissButton = wrapper.findDismissButton()!.getElement(); |
| 127 | + |
| 128 | + fireEvent.blur(popoverBody, { |
| 129 | + relatedTarget: dismissButton, |
| 130 | + }); |
| 131 | + |
| 132 | + expect(onDismissMock).not.toHaveBeenCalled(); |
| 133 | + expect(wrapper.findContent()).toBeTruthy(); |
| 134 | + }); |
| 135 | + |
| 136 | + test('should dismiss when blur relatedTarget is outside popover body', () => { |
| 137 | + const onDismissMock = jest.fn(); |
| 138 | + const { getByTestId, wrapper } = renderComponent(<TestComponent onDismiss={onDismissMock} />); |
| 139 | + |
| 140 | + getByTestId('trigger-button').click(); |
| 141 | + expect(wrapper.findContent()).toBeTruthy(); |
| 142 | + |
| 143 | + const popoverBody = wrapper.findContent()!.getElement(); |
| 144 | + const triggerButton = getByTestId('trigger-button'); |
| 145 | + |
| 146 | + fireEvent.blur(popoverBody, { |
| 147 | + relatedTarget: triggerButton, |
| 148 | + }); |
| 149 | + |
| 150 | + expect(onDismissMock).toHaveBeenCalledTimes(1); |
| 151 | + expect(wrapper.findContent()).toBeFalsy(); |
| 152 | + }); |
| 153 | +}); |
0 commit comments