|
| 1 | +/** |
| 2 | + * @jest-environment @happy-dom/jest-environment |
| 3 | + */ |
| 4 | +/* eslint-disable prefer-destructuring */ |
| 5 | + |
| 6 | +import React from 'react'; |
| 7 | +import { render } from '@testing-library/react'; |
| 8 | + |
| 9 | +import PostMessageListener from './PostMessageListener'; |
| 10 | + |
| 11 | +jest.mock('./hooks/useFocus', () => ({ |
| 12 | + __esModule: true, |
| 13 | + default: jest.fn() |
| 14 | +})); |
| 15 | + |
| 16 | +const mockFocus = jest.fn(); |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + jest.clearAllMocks(); |
| 20 | + mockFocus.mockClear(); |
| 21 | + const useFocusMock = require('./hooks/useFocus').default; |
| 22 | + useFocusMock.mockReturnValue(mockFocus); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('PostMessageListener', () => { |
| 26 | + let mockAddEventListener: jest.SpyInstance; |
| 27 | + let mockRemoveEventListener: jest.SpyInstance; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + mockAddEventListener = jest.spyOn(window, 'addEventListener'); |
| 31 | + mockRemoveEventListener = jest.spyOn(window, 'removeEventListener'); |
| 32 | + mockAddEventListener.mockClear(); |
| 33 | + mockRemoveEventListener.mockClear(); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + mockAddEventListener.mockRestore(); |
| 38 | + mockRemoveEventListener.mockRestore(); |
| 39 | + }); |
| 40 | + |
| 41 | + test('should render without crashing', () => { |
| 42 | + const { container } = render(<PostMessageListener />); |
| 43 | + expect(container).toBeTruthy(); |
| 44 | + }); |
| 45 | + |
| 46 | + test('should return null', () => { |
| 47 | + const { container } = render(<PostMessageListener />); |
| 48 | + expect(container.firstChild).toBeNull(); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should add message event listener on mount', () => { |
| 52 | + render(<PostMessageListener />); |
| 53 | + |
| 54 | + expect(mockAddEventListener).toHaveBeenCalledWith('message', expect.any(Function)); |
| 55 | + }); |
| 56 | + |
| 57 | + test('should remove message event listener on unmount', () => { |
| 58 | + const { unmount } = render(<PostMessageListener />); |
| 59 | + |
| 60 | + const messageListenerCalls = mockAddEventListener.mock.calls.filter(call => call[0] === 'message'); |
| 61 | + expect(messageListenerCalls).toHaveLength(1); |
| 62 | + |
| 63 | + const [, handleMessage] = messageListenerCalls[0]; |
| 64 | + |
| 65 | + unmount(); |
| 66 | + |
| 67 | + expect(mockRemoveEventListener).toHaveBeenCalledWith('message', handleMessage); |
| 68 | + }); |
| 69 | + |
| 70 | + test('should call focus when receiving WEBCHAT_FOCUS message from parent window', async () => { |
| 71 | + render(<PostMessageListener />); |
| 72 | + |
| 73 | + const [, handleMessage] = mockAddEventListener.mock.calls.filter(call => call[0] === 'message')[0]; |
| 74 | + |
| 75 | + const mockEvent = { |
| 76 | + source: window.parent, |
| 77 | + data: { |
| 78 | + type: 'WEBCHAT_FOCUS', |
| 79 | + target: 'sendBox' |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + await handleMessage(mockEvent); |
| 84 | + |
| 85 | + expect(mockFocus).toHaveBeenCalledWith('sendBox'); |
| 86 | + }); |
| 87 | + |
| 88 | + test('should not call focus when message is not from parent window', async () => { |
| 89 | + render(<PostMessageListener />); |
| 90 | + |
| 91 | + const [, handleMessage] = mockAddEventListener.mock.calls.filter(call => call[0] === 'message')[0]; |
| 92 | + |
| 93 | + const mockEvent = { |
| 94 | + source: {}, // Different source, not window.parent |
| 95 | + data: { |
| 96 | + type: 'WEBCHAT_FOCUS', |
| 97 | + target: 'sendBox' |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + const focusCallCountBefore = mockFocus.mock.calls.length; |
| 102 | + await handleMessage(mockEvent); |
| 103 | + const focusCallCountAfter = mockFocus.mock.calls.length; |
| 104 | + |
| 105 | + expect(focusCallCountAfter).toBe(focusCallCountBefore); |
| 106 | + }); |
| 107 | + |
| 108 | + test('should not call focus when message type is not WEBCHAT_FOCUS', async () => { |
| 109 | + render(<PostMessageListener />); |
| 110 | + |
| 111 | + const [, handleMessage] = mockAddEventListener.mock.calls.filter(call => call[0] === 'message')[0]; |
| 112 | + |
| 113 | + const mockEvent = { |
| 114 | + source: window.parent, |
| 115 | + data: { |
| 116 | + type: 'OTHER_MESSAGE', |
| 117 | + target: 'sendBox' |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + const focusCallCountBefore = mockFocus.mock.calls.length; |
| 122 | + await handleMessage(mockEvent); |
| 123 | + const focusCallCountAfter = mockFocus.mock.calls.length; |
| 124 | + |
| 125 | + expect(focusCallCountAfter).toBe(focusCallCountBefore); |
| 126 | + }); |
| 127 | + |
| 128 | + test('should handle message with no data', async () => { |
| 129 | + render(<PostMessageListener />); |
| 130 | + |
| 131 | + const [, handleMessage] = mockAddEventListener.mock.calls.filter(call => call[0] === 'message')[0]; |
| 132 | + |
| 133 | + const mockEvent = { |
| 134 | + source: window.parent, |
| 135 | + data: null |
| 136 | + }; |
| 137 | + |
| 138 | + const focusCallCountBefore = mockFocus.mock.calls.length; |
| 139 | + await handleMessage(mockEvent); |
| 140 | + const focusCallCountAfter = mockFocus.mock.calls.length; |
| 141 | + |
| 142 | + expect(focusCallCountAfter).toBe(focusCallCountBefore); |
| 143 | + }); |
| 144 | + |
| 145 | + test('should handle message with undefined data', async () => { |
| 146 | + render(<PostMessageListener />); |
| 147 | + |
| 148 | + const [, handleMessage] = mockAddEventListener.mock.calls.filter(call => call[0] === 'message')[0]; |
| 149 | + |
| 150 | + const mockEvent = { |
| 151 | + source: window.parent, |
| 152 | + data: undefined |
| 153 | + }; |
| 154 | + |
| 155 | + const focusCallCountBefore = mockFocus.mock.calls.length; |
| 156 | + await handleMessage(mockEvent); |
| 157 | + const focusCallCountAfter = mockFocus.mock.calls.length; |
| 158 | + |
| 159 | + expect(focusCallCountAfter).toBe(focusCallCountBefore); |
| 160 | + }); |
| 161 | + |
| 162 | + test('should log error when focus throws an error', async () => { |
| 163 | + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { |
| 164 | + // Mock implementation |
| 165 | + }); |
| 166 | + const focusError = new Error('Focus failed'); |
| 167 | + mockFocus.mockRejectedValue(focusError); |
| 168 | + |
| 169 | + render(<PostMessageListener />); |
| 170 | + |
| 171 | + const [, handleMessage] = mockAddEventListener.mock.calls.filter(call => call[0] === 'message')[0]; |
| 172 | + |
| 173 | + const mockEvent = { |
| 174 | + source: window.parent, |
| 175 | + data: { |
| 176 | + type: 'WEBCHAT_FOCUS', |
| 177 | + target: 'sendBox' |
| 178 | + } |
| 179 | + }; |
| 180 | + |
| 181 | + await handleMessage(mockEvent); |
| 182 | + |
| 183 | + expect(consoleErrorSpy).toHaveBeenCalledWith('WebChat focus error:', focusError); |
| 184 | + |
| 185 | + consoleErrorSpy.mockRestore(); |
| 186 | + }); |
| 187 | + |
| 188 | + test('should handle focus without target', async () => { |
| 189 | + render(<PostMessageListener />); |
| 190 | + |
| 191 | + const [, handleMessage] = mockAddEventListener.mock.calls.filter(call => call[0] === 'message')[0]; |
| 192 | + |
| 193 | + const mockEvent = { |
| 194 | + source: window.parent, |
| 195 | + data: { |
| 196 | + type: 'WEBCHAT_FOCUS' |
| 197 | + } |
| 198 | + }; |
| 199 | + |
| 200 | + await handleMessage(mockEvent); |
| 201 | + |
| 202 | + expect(mockFocus).toHaveBeenCalledWith(undefined); |
| 203 | + }); |
| 204 | +}); |
0 commit comments