|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent } from '@testing-library/react'; |
| 3 | +import { DataViewContext, DataViewProvider, EventTypes } from './DataViewContext'; |
| 4 | + |
| 5 | +let id = 0; |
| 6 | + |
| 7 | +beforeAll(() => { |
| 8 | + Object.defineProperty(global, 'crypto', { |
| 9 | + value: { |
| 10 | + randomUUID: jest.fn(() => `mocked-uuid-${id++}`), |
| 11 | + }, |
| 12 | + }); |
| 13 | +}); |
| 14 | + |
| 15 | +describe('DataViewContext', () => { |
| 16 | + test('should provide context value and allow subscriptions', () => { |
| 17 | + const callback = jest.fn(); |
| 18 | + |
| 19 | + const TestComponent = () => { |
| 20 | + const { subscribe, trigger } = React.useContext(DataViewContext); |
| 21 | + |
| 22 | + React.useEffect(() => { |
| 23 | + const unsubscribe = subscribe(EventTypes.rowClick, callback); |
| 24 | + return () => unsubscribe(); |
| 25 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 26 | + }, []); |
| 27 | + |
| 28 | + return ( |
| 29 | + <button onClick={() => trigger(EventTypes.rowClick, 'some payload')}>Trigger</button> |
| 30 | + ); |
| 31 | + }; |
| 32 | + |
| 33 | + const { getByText } = render( |
| 34 | + <DataViewProvider> |
| 35 | + <TestComponent /> |
| 36 | + </DataViewProvider> |
| 37 | + ); |
| 38 | + |
| 39 | + fireEvent.click(getByText('Trigger')); |
| 40 | + expect(callback).toHaveBeenCalledWith('some payload'); |
| 41 | + }); |
| 42 | + |
| 43 | + test('should handle unsubscribing correctly', () => { |
| 44 | + const callback = jest.fn(); |
| 45 | + |
| 46 | + const TestComponent = () => { |
| 47 | + const { subscribe, trigger } = React.useContext(DataViewContext); |
| 48 | + |
| 49 | + React.useEffect(() => { |
| 50 | + const unsubscribe = subscribe(EventTypes.rowClick, callback); |
| 51 | + unsubscribe(); |
| 52 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 53 | + }, []); |
| 54 | + |
| 55 | + return ( |
| 56 | + <button onClick={() => trigger(EventTypes.rowClick, 'some payload')}>Trigger</button> |
| 57 | + ); |
| 58 | + }; |
| 59 | + |
| 60 | + const { getByText } = render( |
| 61 | + <DataViewProvider> |
| 62 | + <TestComponent /> |
| 63 | + </DataViewProvider> |
| 64 | + ); |
| 65 | + |
| 66 | + fireEvent.click(getByText('Trigger')); |
| 67 | + |
| 68 | + expect(callback).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + test('should handle multiple subscriptions and trigger events correctly', () => { |
| 72 | + const callback1 = jest.fn(); |
| 73 | + const callback2 = jest.fn(); |
| 74 | + |
| 75 | + const TestComponent = () => { |
| 76 | + const { subscribe, trigger } = React.useContext(DataViewContext); |
| 77 | + |
| 78 | + React.useEffect(() => { |
| 79 | + const unsubscribe1 = subscribe(EventTypes.rowClick, callback1); |
| 80 | + const unsubscribe2 = subscribe(EventTypes.rowClick, callback2); |
| 81 | + |
| 82 | + return () => { |
| 83 | + unsubscribe1(); |
| 84 | + unsubscribe2(); |
| 85 | + }; |
| 86 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 87 | + }, []); |
| 88 | + |
| 89 | + return ( |
| 90 | + <button onClick={() => trigger(EventTypes.rowClick, 'some payload')}>Trigger</button> |
| 91 | + ); |
| 92 | + }; |
| 93 | + |
| 94 | + const { getByText } = render( |
| 95 | + <DataViewProvider> |
| 96 | + <TestComponent /> |
| 97 | + </DataViewProvider> |
| 98 | + ); |
| 99 | + |
| 100 | + fireEvent.click(getByText('Trigger')); |
| 101 | + |
| 102 | + expect(callback1).toHaveBeenCalledWith('some payload'); |
| 103 | + expect(callback2).toHaveBeenCalledWith('some payload'); |
| 104 | + }); |
| 105 | +}); |
0 commit comments