|
| 1 | +import React from 'react'; |
| 2 | +import { Provider } from 'react-redux'; |
| 3 | +import { renderHook } from '@testing-library/react'; |
| 4 | +import { mockStore } from '../../__test-helpers/MockStore'; |
| 5 | +import useWebVitals from './index'; |
| 6 | +import { initializeWebVitals, createUserAnalyticsData } from '../../utils/webVitals'; |
| 7 | + |
| 8 | +jest.mock('../../utils/webVitals', () => ({ |
| 9 | + initializeWebVitals: jest.fn(), |
| 10 | + createUserAnalyticsData: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +describe('useWebVitals hook', () => { |
| 14 | + beforeEach(() => { |
| 15 | + jest.clearAllMocks(); |
| 16 | + |
| 17 | + jest.spyOn(console, 'log').mockImplementation(() => { }); // Mock console.log to avoid noise in tests |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + jest.restoreAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + const createWrapper = (storeState = {}) => { |
| 25 | + const testStore = mockStore({ |
| 26 | + data: { |
| 27 | + user: null, |
| 28 | + selectedUserProfile: {}, |
| 29 | + ...storeState.data, |
| 30 | + }, |
| 31 | + ...storeState, |
| 32 | + }); |
| 33 | + |
| 34 | + const Wrapper = ({ children }) => ( |
| 35 | + <Provider store={testStore}> |
| 36 | + {children} |
| 37 | + </Provider> |
| 38 | + ); |
| 39 | + |
| 40 | + return Wrapper; |
| 41 | + }; |
| 42 | + |
| 43 | + it('should not initialize web vitals when user is not available', () => { |
| 44 | + const wrapper = createWrapper({ |
| 45 | + data: { |
| 46 | + user: null, |
| 47 | + selectedUserProfile: {}, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + renderHook(() => useWebVitals(), { wrapper }); |
| 52 | + |
| 53 | + expect(initializeWebVitals).not.toHaveBeenCalled(); |
| 54 | + expect(createUserAnalyticsData).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should initialize web vitals when user with ID is available', () => { |
| 58 | + const mockUser = { |
| 59 | + id: 'user123', |
| 60 | + username: 'testuser', |
| 61 | + role: 'ranger', |
| 62 | + is_staff: false, |
| 63 | + is_superuser: false, |
| 64 | + }; |
| 65 | + |
| 66 | + const mockSelectedProfile = { |
| 67 | + id: 'profile456', |
| 68 | + role: 'admin', |
| 69 | + }; |
| 70 | + |
| 71 | + const mockServerVersion = '1.2.3'; |
| 72 | + |
| 73 | + const mockUserData = { |
| 74 | + user_role: 'admin', |
| 75 | + organization: 'test.earthranger.com', |
| 76 | + user_id_hash: 'abc123', |
| 77 | + is_staff: false, |
| 78 | + is_superuser: false, |
| 79 | + }; |
| 80 | + |
| 81 | + createUserAnalyticsData.mockReturnValue(mockUserData); |
| 82 | + |
| 83 | + const wrapper = createWrapper({ |
| 84 | + data: { |
| 85 | + user: mockUser, |
| 86 | + selectedUserProfile: mockSelectedProfile, |
| 87 | + systemStatus: { |
| 88 | + server: { |
| 89 | + version: mockServerVersion, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + renderHook(() => useWebVitals(), { wrapper }); |
| 96 | + |
| 97 | + expect(createUserAnalyticsData).toHaveBeenCalledWith(mockUser, mockSelectedProfile, mockServerVersion); |
| 98 | + expect(initializeWebVitals).toHaveBeenCalledWith(mockUserData); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should only initialize once even with multiple renders', () => { |
| 102 | + const mockUser = { |
| 103 | + id: 'user123', |
| 104 | + username: 'testuser', |
| 105 | + role: 'ranger', |
| 106 | + }; |
| 107 | + |
| 108 | + const mockUserData = { |
| 109 | + user_role: 'ranger', |
| 110 | + organization: 'test.earthranger.com', |
| 111 | + user_id_hash: 'abc123', |
| 112 | + }; |
| 113 | + |
| 114 | + createUserAnalyticsData.mockReturnValue(mockUserData); |
| 115 | + |
| 116 | + const wrapper = createWrapper({ |
| 117 | + data: { |
| 118 | + user: mockUser, |
| 119 | + selectedUserProfile: {}, |
| 120 | + systemStatus: { |
| 121 | + server: { |
| 122 | + version: '1.0.0', |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }); |
| 127 | + |
| 128 | + const { rerender } = renderHook(() => useWebVitals(), { wrapper }); |
| 129 | + |
| 130 | + expect(initializeWebVitals).toHaveBeenCalledTimes(1); |
| 131 | + |
| 132 | + // Rerender the hook |
| 133 | + rerender(); |
| 134 | + |
| 135 | + expect(initializeWebVitals).toHaveBeenCalledTimes(1); // Should still be 1 |
| 136 | + }); |
| 137 | +}); |
0 commit comments