|
1 |
| -import { render, screen } from '@testing-library/react'; |
| 1 | +import { render, screen, waitFor } from '@testing-library/react'; |
2 | 2 | import { act, useEffect, type PropsWithChildren } from 'react';
|
3 |
| -import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
4 | 4 | import { OverlayProvider } from './context/provider';
|
5 | 5 | import { overlay } from './event';
|
6 | 6 |
|
@@ -74,4 +74,72 @@ describe('overlay object', () => {
|
74 | 74 | expect(screen.queryByText(testContent3)).toBeInTheDocument();
|
75 | 75 | expect(screen.queryByText(testContent4)).toBeInTheDocument();
|
76 | 76 | });
|
| 77 | + |
| 78 | + it('The value passed as an argument to close is passed to resolve. overlay.openAsync', async () => { |
| 79 | + const wrapper = ({ children }: PropsWithChildren) => <OverlayProvider>{children}</OverlayProvider>; |
| 80 | + const testContent = 'context-modal-test-content'; |
| 81 | + const dialogContent = 'context-modal-dialog-content'; |
| 82 | + const mockFn = vi.fn(); |
| 83 | + const Component = () => { |
| 84 | + const handleClick = async () => { |
| 85 | + const result = await overlay.openAsync<boolean>(({ close }) => ( |
| 86 | + <button onClick={() => close(true)}>{dialogContent}</button> |
| 87 | + )); |
| 88 | + |
| 89 | + if (result) { |
| 90 | + mockFn(result); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + return <button onClick={handleClick}>{testContent}</button>; |
| 95 | + }; |
| 96 | + |
| 97 | + const renderComponent = render(<Component />, { wrapper }); |
| 98 | + const testContentElement = await renderComponent.findByText(testContent); |
| 99 | + |
| 100 | + act(() => { |
| 101 | + testContentElement.click(); |
| 102 | + }); |
| 103 | + |
| 104 | + const dialogContentElement = await renderComponent.findByText(dialogContent); |
| 105 | + |
| 106 | + act(() => { |
| 107 | + dialogContentElement.click(); |
| 108 | + }); |
| 109 | + await waitFor(() => { |
| 110 | + expect(mockFn).toHaveBeenCalledWith(true); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should be able to turn off overlay through close overlay.openAsync', async () => { |
| 115 | + const wrapper = ({ children }: PropsWithChildren) => <OverlayProvider>{children}</OverlayProvider>; |
| 116 | + const testContent = 'context-modal-test-content'; |
| 117 | + const dialogContent = 'context-modal-dialog-content'; |
| 118 | + |
| 119 | + const Component = () => { |
| 120 | + const handleClick = async () => { |
| 121 | + overlay.openAsync<boolean>(({ isOpen, close }) => |
| 122 | + isOpen ? <button onClick={() => close(true)}>{dialogContent}</button> : null |
| 123 | + ); |
| 124 | + }; |
| 125 | + return <button onClick={handleClick}>{testContent}</button>; |
| 126 | + }; |
| 127 | + |
| 128 | + const renderComponent = render(<Component />, { wrapper }); |
| 129 | + const testContentElement = await renderComponent.findByText(testContent); |
| 130 | + |
| 131 | + act(() => { |
| 132 | + testContentElement.click(); |
| 133 | + }); |
| 134 | + |
| 135 | + const dialogContentElement = await renderComponent.findByText(dialogContent); |
| 136 | + |
| 137 | + act(() => { |
| 138 | + dialogContentElement.click(); |
| 139 | + }); |
| 140 | + |
| 141 | + await waitFor(() => { |
| 142 | + expect(dialogContentElement).not.toBeInTheDocument(); |
| 143 | + }); |
| 144 | + }); |
77 | 145 | });
|
0 commit comments