|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | + |
| 5 | +import { ChatLayout, useChatLayoutContext } from '.'; |
| 6 | + |
| 7 | +describe('packages/chat-layout', () => { |
| 8 | + describe('ChatLayout', () => { |
| 9 | + test('renders children', () => { |
| 10 | + render( |
| 11 | + <ChatLayout> |
| 12 | + <div>Test Content</div> |
| 13 | + </ChatLayout>, |
| 14 | + ); |
| 15 | + expect(screen.getByText('Test Content')).toBeInTheDocument(); |
| 16 | + }); |
| 17 | + |
| 18 | + test('provides isPinned context with default value', () => { |
| 19 | + const TestConsumer = () => { |
| 20 | + const { isPinned } = useChatLayoutContext(); |
| 21 | + return <div>isPinned: {isPinned.toString()}</div>; |
| 22 | + }; |
| 23 | + |
| 24 | + render( |
| 25 | + <ChatLayout> |
| 26 | + <TestConsumer /> |
| 27 | + </ChatLayout>, |
| 28 | + ); |
| 29 | + expect(screen.getByText('isPinned: true')).toBeInTheDocument(); |
| 30 | + }); |
| 31 | + |
| 32 | + test('accepts initialIsPinned prop', () => { |
| 33 | + const TestConsumer = () => { |
| 34 | + const { isPinned } = useChatLayoutContext(); |
| 35 | + return <div>isPinned: {isPinned.toString()}</div>; |
| 36 | + }; |
| 37 | + |
| 38 | + render( |
| 39 | + <ChatLayout initialIsPinned={false}> |
| 40 | + <TestConsumer /> |
| 41 | + </ChatLayout>, |
| 42 | + ); |
| 43 | + expect(screen.getByText('isPinned: false')).toBeInTheDocument(); |
| 44 | + }); |
| 45 | + |
| 46 | + test('togglePin function updates isPinned state', async () => { |
| 47 | + const TestConsumer = () => { |
| 48 | + const { isPinned, togglePin } = useChatLayoutContext(); |
| 49 | + return ( |
| 50 | + <> |
| 51 | + <div>isPinned: {isPinned.toString()}</div> |
| 52 | + <button onClick={togglePin}>Toggle</button> |
| 53 | + </> |
| 54 | + ); |
| 55 | + }; |
| 56 | + |
| 57 | + render( |
| 58 | + <ChatLayout> |
| 59 | + <TestConsumer /> |
| 60 | + </ChatLayout>, |
| 61 | + ); |
| 62 | + |
| 63 | + expect(screen.getByText('isPinned: true')).toBeInTheDocument(); |
| 64 | + |
| 65 | + await userEvent.click(screen.getByRole('button', { name: 'Toggle' })); |
| 66 | + |
| 67 | + expect(screen.getByText('isPinned: false')).toBeInTheDocument(); |
| 68 | + |
| 69 | + await userEvent.click(screen.getByRole('button', { name: 'Toggle' })); |
| 70 | + |
| 71 | + expect(screen.getByText('isPinned: true')).toBeInTheDocument(); |
| 72 | + }); |
| 73 | + |
| 74 | + test('forwards HTML attributes to the div wrapper', () => { |
| 75 | + render( |
| 76 | + <ChatLayout data-testid="chat-layout"> |
| 77 | + <div>Content</div> |
| 78 | + </ChatLayout>, |
| 79 | + ); |
| 80 | + expect(screen.getByTestId('chat-layout')).toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + test('calls onTogglePinned callback when togglePin is called', async () => { |
| 84 | + const onTogglePinned = jest.fn(); |
| 85 | + |
| 86 | + const TestConsumer = () => { |
| 87 | + const { togglePin } = useChatLayoutContext(); |
| 88 | + return <button onClick={togglePin}>Toggle</button>; |
| 89 | + }; |
| 90 | + |
| 91 | + render( |
| 92 | + <ChatLayout onTogglePinned={onTogglePinned}> |
| 93 | + <TestConsumer /> |
| 94 | + </ChatLayout>, |
| 95 | + ); |
| 96 | + |
| 97 | + const toggleButton = screen.getByRole('button', { name: 'Toggle' }); |
| 98 | + |
| 99 | + await userEvent.click(toggleButton); |
| 100 | + |
| 101 | + expect(onTogglePinned).toHaveBeenCalledTimes(1); |
| 102 | + expect(onTogglePinned).toHaveBeenCalledWith(false); |
| 103 | + |
| 104 | + await userEvent.click(toggleButton); |
| 105 | + |
| 106 | + expect(onTogglePinned).toHaveBeenCalledTimes(2); |
| 107 | + expect(onTogglePinned).toHaveBeenCalledWith(true); |
| 108 | + }); |
| 109 | + |
| 110 | + test('onTogglePinned receives correct isPinned value based on initialIsPinned', async () => { |
| 111 | + const onTogglePinned = jest.fn(); |
| 112 | + |
| 113 | + const TestConsumer = () => { |
| 114 | + const { togglePin } = useChatLayoutContext(); |
| 115 | + return <button onClick={togglePin}>Toggle</button>; |
| 116 | + }; |
| 117 | + |
| 118 | + render( |
| 119 | + <ChatLayout initialIsPinned={false} onTogglePinned={onTogglePinned}> |
| 120 | + <TestConsumer /> |
| 121 | + </ChatLayout>, |
| 122 | + ); |
| 123 | + |
| 124 | + await userEvent.click(screen.getByRole('button', { name: 'Toggle' })); |
| 125 | + |
| 126 | + expect(onTogglePinned).toHaveBeenCalledWith(true); |
| 127 | + }); |
| 128 | + |
| 129 | + test('applies custom className', () => { |
| 130 | + render( |
| 131 | + <ChatLayout data-testid="chat-layout" className="custom-class"> |
| 132 | + <div>Content</div> |
| 133 | + </ChatLayout>, |
| 134 | + ); |
| 135 | + const element = screen.getByTestId('chat-layout'); |
| 136 | + expect(element).toHaveClass('custom-class'); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments