|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { MessageSearchProvider, useMessageSearchContext } from '../MessageSearchProvider'; |
| 4 | +import { match } from 'ts-pattern'; |
| 5 | + |
| 6 | +jest.mock('../../../../hooks/useSendbirdStateContext', () => ({ |
| 7 | + __esModule: true, |
| 8 | + default: () => ({}), |
| 9 | +})); |
| 10 | + |
| 11 | +const mockProps = { |
| 12 | + channelUrl: 'channel-1', |
| 13 | + searchString: 'test', |
| 14 | + messageSearchQuery: {}, |
| 15 | + onResultLoaded: jest.fn(), |
| 16 | + onResultClick: jest.fn(), |
| 17 | + children: <div>Child Component</div>, |
| 18 | +}; |
| 19 | + |
| 20 | +describe('MessageSearch Migration Compatibility Tests', () => { |
| 21 | + // 1. Provider Props Interface test |
| 22 | + describe('MessageSearchProvider Props Compatibility', () => { |
| 23 | + it('should accept all legacy props without type errors', () => { |
| 24 | + const { rerender } = render( |
| 25 | + <MessageSearchProvider {...mockProps}> |
| 26 | + {mockProps.children} |
| 27 | + </MessageSearchProvider>, |
| 28 | + ); |
| 29 | + |
| 30 | + // Props change scenario test |
| 31 | + rerender( |
| 32 | + <MessageSearchProvider |
| 33 | + {...mockProps} |
| 34 | + searchString="updated" |
| 35 | + onResultLoaded={() => {}} |
| 36 | + > |
| 37 | + {mockProps.children} |
| 38 | + </MessageSearchProvider>, |
| 39 | + ); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + // 2. Context Hook return value test |
| 44 | + describe('useMessageSearchContext Hook Return Values', () => { |
| 45 | + type ContextType = ReturnType<typeof useMessageSearchContext>; |
| 46 | + const expectedProps: Array<keyof ContextType> = [ |
| 47 | + 'channelUrl', |
| 48 | + 'searchString', |
| 49 | + 'messageSearchQuery', |
| 50 | + 'onResultLoaded', |
| 51 | + 'onResultClick', |
| 52 | + 'children', |
| 53 | + 'requestString', |
| 54 | + 'retryCount', |
| 55 | + 'setRetryCount', |
| 56 | + 'selectedMessageId', |
| 57 | + 'setSelectedMessageId', |
| 58 | + 'messageSearchDispatcher', |
| 59 | + 'scrollRef', |
| 60 | + 'allMessages', |
| 61 | + 'loading', |
| 62 | + 'isInvalid', |
| 63 | + 'currentChannel', |
| 64 | + 'currentMessageSearchQuery', |
| 65 | + 'hasMoreResult', |
| 66 | + 'onScroll', |
| 67 | + 'handleRetryToConnect', |
| 68 | + 'handleOnScroll', |
| 69 | + ]; |
| 70 | + |
| 71 | + const TestComponent = () => { |
| 72 | + const context = useMessageSearchContext(); |
| 73 | + return ( |
| 74 | + <div> |
| 75 | + {expectedProps.map(prop => ( |
| 76 | + <div key={prop} data-testid={`prop-${prop}`}> |
| 77 | + {/* text can be function, object, string, or unknown */} |
| 78 | + {match(context[prop]) |
| 79 | + .with('function', () => 'function') |
| 80 | + .with('object', () => JSON.stringify(context[prop])) |
| 81 | + .with('string', () => String(context[prop])) |
| 82 | + .otherwise(() => 'unknown')} |
| 83 | + </div> |
| 84 | + ))} |
| 85 | + </div> |
| 86 | + ); |
| 87 | + }; |
| 88 | + |
| 89 | + it('should provide all legacy context values', () => { |
| 90 | + render( |
| 91 | + <MessageSearchProvider {...mockProps}> |
| 92 | + <TestComponent /> |
| 93 | + </MessageSearchProvider>, |
| 94 | + ); |
| 95 | + |
| 96 | + expectedProps.forEach(prop => { |
| 97 | + const element = screen.getByTestId(`prop-${prop}`); |
| 98 | + expect(element).toBeInTheDocument(); |
| 99 | + }); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments