|
| 1 | +import { fireEvent, render } from '@testing-library/react'; |
| 2 | +import '@testing-library/jest-dom'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | +import { Toast } from './Toast'; |
| 5 | + |
| 6 | +describe('Toast component', () => { |
| 7 | + it('should render bottom-right correctly', () => { |
| 8 | + const handleClose = vi.fn(); |
| 9 | + const { getByTestId } = render( |
| 10 | + <Toast isVisible={true} position="bottom-right" onClose={handleClose}> |
| 11 | + <div>Test</div> |
| 12 | + </Toast>, |
| 13 | + ); |
| 14 | + |
| 15 | + const toastContainer = getByTestId('ockToast'); |
| 16 | + expect(toastContainer).toBeInTheDocument(); |
| 17 | + expect(toastContainer).toHaveClass('bottom-5 left-3/4'); |
| 18 | + |
| 19 | + const closeButton = getByTestId('ockCloseButton'); |
| 20 | + expect(closeButton).toBeInTheDocument(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should render top-right correctly', () => { |
| 24 | + const handleClose = vi.fn(); |
| 25 | + const { getByTestId } = render( |
| 26 | + <Toast isVisible={true} position="top-right" onClose={handleClose}> |
| 27 | + <div>Test</div> |
| 28 | + </Toast>, |
| 29 | + ); |
| 30 | + |
| 31 | + const toastContainer = getByTestId('ockToast'); |
| 32 | + expect(toastContainer).toBeInTheDocument(); |
| 33 | + expect(toastContainer).toHaveClass('top-[100px] left-3/4'); |
| 34 | + |
| 35 | + const closeButton = getByTestId('ockCloseButton'); |
| 36 | + expect(closeButton).toBeInTheDocument(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should render top-center correctly', () => { |
| 40 | + const handleClose = vi.fn(); |
| 41 | + const { getByTestId } = render( |
| 42 | + <Toast isVisible={true} position="top-center" onClose={handleClose}> |
| 43 | + <div>Test</div> |
| 44 | + </Toast>, |
| 45 | + ); |
| 46 | + |
| 47 | + const toastContainer = getByTestId('ockToast'); |
| 48 | + expect(toastContainer).toBeInTheDocument(); |
| 49 | + expect(toastContainer).toHaveClass('top-[100px] left-2/4'); |
| 50 | + |
| 51 | + const closeButton = getByTestId('ockCloseButton'); |
| 52 | + expect(closeButton).toBeInTheDocument(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should render bottom-center correctly', () => { |
| 56 | + const handleClose = vi.fn(); |
| 57 | + const { getByTestId } = render( |
| 58 | + <Toast isVisible={true} position="bottom-center" onClose={handleClose}> |
| 59 | + <div>Test</div> |
| 60 | + </Toast>, |
| 61 | + ); |
| 62 | + |
| 63 | + const toastContainer = getByTestId('ockToast'); |
| 64 | + expect(toastContainer).toBeInTheDocument(); |
| 65 | + expect(toastContainer).toHaveClass('bottom-5 left-2/4'); |
| 66 | + |
| 67 | + const closeButton = getByTestId('ockCloseButton'); |
| 68 | + expect(closeButton).toBeInTheDocument(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should apply custom className correctly', () => { |
| 72 | + const handleClose = vi.fn(); |
| 73 | + const { getByTestId } = render( |
| 74 | + <Toast |
| 75 | + isVisible={true} |
| 76 | + position="bottom-right" |
| 77 | + onClose={handleClose} |
| 78 | + className="custom-class" |
| 79 | + > |
| 80 | + <div>Test</div> |
| 81 | + </Toast>, |
| 82 | + ); |
| 83 | + |
| 84 | + const toastContainer = getByTestId('ockToast'); |
| 85 | + expect(toastContainer).toHaveClass('custom-class'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should not be visible when isVisible is false', () => { |
| 89 | + const handleClose = vi.fn(); |
| 90 | + const { queryByTestId } = render( |
| 91 | + <Toast isVisible={false} position="bottom-right" onClose={handleClose}> |
| 92 | + <div>Test</div> |
| 93 | + </Toast>, |
| 94 | + ); |
| 95 | + const toastContainer = queryByTestId('ockToast'); |
| 96 | + expect(toastContainer).not.toBeInTheDocument(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should close when close button is clicked', () => { |
| 100 | + const handleClose = vi.fn(); |
| 101 | + const { getByTestId } = render( |
| 102 | + <Toast isVisible={true} position="bottom-right" onClose={handleClose}> |
| 103 | + <div>Test</div> |
| 104 | + </Toast>, |
| 105 | + ); |
| 106 | + |
| 107 | + const closeButton = getByTestId('ockCloseButton'); |
| 108 | + fireEvent.click(closeButton); |
| 109 | + expect(handleClose).toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should render children correctly', () => { |
| 113 | + const handleClose = vi.fn(); |
| 114 | + const { getByText } = render( |
| 115 | + <Toast isVisible={true} position="bottom-right" onClose={handleClose}> |
| 116 | + <div>Test</div> |
| 117 | + </Toast>, |
| 118 | + ); |
| 119 | + |
| 120 | + const text = getByText('Test'); |
| 121 | + expect(text).toBeInTheDocument(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should disappear after durationMs', async () => { |
| 125 | + vi.useFakeTimers(); |
| 126 | + const handleClose = vi.fn(); |
| 127 | + const durationMs = 2000; |
| 128 | + |
| 129 | + render( |
| 130 | + <Toast |
| 131 | + isVisible={true} |
| 132 | + position="bottom-right" |
| 133 | + onClose={handleClose} |
| 134 | + durationMs={durationMs} |
| 135 | + > |
| 136 | + <div>Test</div> |
| 137 | + </Toast>, |
| 138 | + ); |
| 139 | + |
| 140 | + expect(handleClose).not.toHaveBeenCalled(); |
| 141 | + |
| 142 | + vi.advanceTimersByTime(durationMs); |
| 143 | + |
| 144 | + expect(handleClose).toHaveBeenCalled(); |
| 145 | + |
| 146 | + vi.useRealTimers(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should not fire timer after manual close', () => { |
| 150 | + vi.useFakeTimers(); |
| 151 | + const handleClose = vi.fn(); |
| 152 | + const durationMs = 2000; |
| 153 | + |
| 154 | + const { getByTestId, rerender } = render( |
| 155 | + <Toast |
| 156 | + isVisible={true} |
| 157 | + position="bottom-right" |
| 158 | + onClose={handleClose} |
| 159 | + durationMs={durationMs} |
| 160 | + > |
| 161 | + <div>Test</div> |
| 162 | + </Toast>, |
| 163 | + ); |
| 164 | + |
| 165 | + vi.advanceTimersByTime(1000); |
| 166 | + |
| 167 | + const closeButton = getByTestId('ockCloseButton'); |
| 168 | + fireEvent.click(closeButton); |
| 169 | + |
| 170 | + expect(handleClose).toHaveBeenCalledTimes(1); |
| 171 | + |
| 172 | + rerender( |
| 173 | + <Toast |
| 174 | + isVisible={false} |
| 175 | + position="bottom-right" |
| 176 | + onClose={handleClose} |
| 177 | + durationMs={durationMs} |
| 178 | + > |
| 179 | + <div>Test</div> |
| 180 | + </Toast>, |
| 181 | + ); |
| 182 | + |
| 183 | + vi.advanceTimersByTime(1500); |
| 184 | + expect(handleClose).toHaveBeenCalledTimes(1); |
| 185 | + |
| 186 | + vi.useRealTimers(); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should cleanup correctly on unmount', () => { |
| 190 | + vi.useFakeTimers(); |
| 191 | + const handleClose = vi.fn(); |
| 192 | + |
| 193 | + const { unmount } = render( |
| 194 | + <Toast |
| 195 | + isVisible={true} |
| 196 | + position="bottom-right" |
| 197 | + onClose={handleClose} |
| 198 | + durationMs={2000} |
| 199 | + > |
| 200 | + <div>Test</div> |
| 201 | + </Toast>, |
| 202 | + ); |
| 203 | + |
| 204 | + unmount(); |
| 205 | + vi.advanceTimersByTime(2000); |
| 206 | + |
| 207 | + expect(handleClose).not.toHaveBeenCalled(); |
| 208 | + vi.useRealTimers(); |
| 209 | + }); |
| 210 | +}); |
0 commit comments