|
| 1 | +import { render, unmountComponentAtNode } from 'react-dom'; |
| 2 | +import { act } from 'react-dom/test-utils'; |
| 3 | +import TitleBar from './TitleBar'; |
| 4 | + |
| 5 | +describe('TitleBar', () => { |
| 6 | + let container: HTMLDivElement | null = null; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + container = document.createElement('div'); |
| 10 | + document.body.appendChild(container); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + if (container) { |
| 15 | + unmountComponentAtNode(container); |
| 16 | + container.remove(); |
| 17 | + container = null; |
| 18 | + } |
| 19 | + }); |
| 20 | + |
| 21 | + it('renders title text', () => { |
| 22 | + act(() => { |
| 23 | + render(<TitleBar titleText="My Title" />, container!); |
| 24 | + }); |
| 25 | + const wrapper = container!.firstElementChild as HTMLElement; |
| 26 | + const title = wrapper.firstElementChild as HTMLElement; |
| 27 | + expect(title.textContent).toBe('My Title'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('triggers callbacks when buttons clicked', () => { |
| 31 | + const onMinimize = jest.fn(); |
| 32 | + const onMaximize = jest.fn(); |
| 33 | + const onUnMaximize = jest.fn(); |
| 34 | + const onClose = jest.fn(); |
| 35 | + |
| 36 | + act(() => { |
| 37 | + render( |
| 38 | + <TitleBar |
| 39 | + titleText="" |
| 40 | + onMinimize={onMinimize} |
| 41 | + onMaximize={onMaximize} |
| 42 | + onUnMaximize={onUnMaximize} |
| 43 | + onClose={onClose} |
| 44 | + />, container!); |
| 45 | + }); |
| 46 | + |
| 47 | + const wrapper = container!.firstElementChild as HTMLElement; |
| 48 | + const buttons = wrapper.lastElementChild as HTMLElement; |
| 49 | + const buttonItems = buttons.children; |
| 50 | + |
| 51 | + act(() => { |
| 52 | + (buttonItems[0] as HTMLElement).dispatchEvent(new MouseEvent('click', { bubbles: true })); |
| 53 | + }); |
| 54 | + expect(onMinimize).toHaveBeenCalledTimes(1); |
| 55 | + |
| 56 | + act(() => { |
| 57 | + (buttonItems[1] as HTMLElement).dispatchEvent(new MouseEvent('click', { bubbles: true })); |
| 58 | + }); |
| 59 | + expect(onMaximize).toHaveBeenCalledTimes(1); |
| 60 | + |
| 61 | + act(() => { |
| 62 | + (buttonItems[1] as HTMLElement).dispatchEvent(new MouseEvent('click', { bubbles: true })); |
| 63 | + }); |
| 64 | + expect(onUnMaximize).toHaveBeenCalledTimes(1); |
| 65 | + |
| 66 | + act(() => { |
| 67 | + (buttonItems[2] as HTMLElement).dispatchEvent(new MouseEvent('click', { bubbles: true })); |
| 68 | + }); |
| 69 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 70 | + }); |
| 71 | +}); |
0 commit comments