From 2f9943a7b878251d85562ae5beda945346c6faf4 Mon Sep 17 00:00:00 2001 From: sawhil Date: Mon, 19 May 2025 06:44:45 +0530 Subject: [PATCH] chore: added tests for traces loader and updater --- .../__tests__/tracesLoaderConfig.test.ts | 131 ++++++++++++++++ .../__tests__/tracesUpdaterConfig.test.ts | 141 ++++++++++++++++++ 2 files changed, 272 insertions(+) create mode 100644 frontend/src/providers/preferences/__tests__/tracesLoaderConfig.test.ts create mode 100644 frontend/src/providers/preferences/__tests__/tracesUpdaterConfig.test.ts diff --git a/frontend/src/providers/preferences/__tests__/tracesLoaderConfig.test.ts b/frontend/src/providers/preferences/__tests__/tracesLoaderConfig.test.ts new file mode 100644 index 0000000000..230c297e09 --- /dev/null +++ b/frontend/src/providers/preferences/__tests__/tracesLoaderConfig.test.ts @@ -0,0 +1,131 @@ +import { LOCALSTORAGE } from 'constants/localStorage'; +import { defaultTraceSelectedColumns } from 'container/OptionsMenu/constants'; +import { + BaseAutocompleteData, + DataTypes, +} from 'types/api/queryBuilder/queryAutocompleteResponse'; + +import tracesLoaderConfig from '../configs/tracesLoaderConfig'; + +// Mock localStorage +const mockLocalStorage: Record = {}; + +jest.mock('api/browser/localstorage/get', () => ({ + __esModule: true, + default: jest.fn((key: string) => mockLocalStorage[key] || null), +})); + +describe('tracesLoaderConfig', () => { + // Save original location object + const originalWindowLocation = window.location; + let mockedLocation: Partial; + + beforeEach(() => { + // Setup a mocked location object + mockedLocation = { + ...originalWindowLocation, + search: '', + }; + + // Mock the window.location property + Object.defineProperty(window, 'location', { + configurable: true, + value: mockedLocation, + writable: true, + }); + + // Clear mocked localStorage + Object.keys(mockLocalStorage).forEach((key) => { + delete mockLocalStorage[key]; + }); + }); + + afterEach(() => { + // Restore original location + Object.defineProperty(window, 'location', { + configurable: true, + value: originalWindowLocation, + writable: true, + }); + }); + + it('should have priority order: local, url, default', () => { + expect(tracesLoaderConfig.priority).toEqual(['local', 'url', 'default']); + }); + + it('should load from localStorage when available', async () => { + const mockColumns: BaseAutocompleteData[] = [ + { + key: 'test-trace-column', + type: 'tag', + dataType: DataTypes.String, + isColumn: true, + }, + ]; + + // Set up localStorage mock data with the correct key from LOCALSTORAGE enum + mockLocalStorage[LOCALSTORAGE.TRACES_LIST_OPTIONS] = JSON.stringify({ + selectColumns: mockColumns, + }); + + const result = await tracesLoaderConfig.local(); + + expect(result).toEqual({ + columns: mockColumns, + }); + }); + + it('should handle invalid localStorage data gracefully', async () => { + // Set up invalid localStorage mock data + mockLocalStorage[LOCALSTORAGE.TRACES_LIST_OPTIONS] = 'invalid-json'; + + const result = await tracesLoaderConfig.local(); + + expect(result).toEqual({ + columns: [] as BaseAutocompleteData[], + }); + }); + + it('should load from URL when available', async () => { + const mockColumns: BaseAutocompleteData[] = [ + { + key: 'url-trace-column', + type: 'tag', + dataType: DataTypes.String, + isColumn: true, + }, + ]; + + // Set up URL search params + mockedLocation.search = `?options=${encodeURIComponent( + JSON.stringify({ + selectColumns: mockColumns, + }), + )}`; + + const result = await tracesLoaderConfig.url(); + + expect(result).toEqual({ + columns: mockColumns, + }); + }); + + it('should handle invalid URL data gracefully', async () => { + // Set up invalid URL search params + mockedLocation.search = '?options=invalid-json'; + + const result = await tracesLoaderConfig.url(); + + expect(result).toEqual({ + columns: [] as BaseAutocompleteData[], + }); + }); + + it('should provide default values when no other source is available', async () => { + const result = await tracesLoaderConfig.default(); + + expect(result).toEqual({ + columns: defaultTraceSelectedColumns as BaseAutocompleteData[], + }); + }); +}); diff --git a/frontend/src/providers/preferences/__tests__/tracesUpdaterConfig.test.ts b/frontend/src/providers/preferences/__tests__/tracesUpdaterConfig.test.ts new file mode 100644 index 0000000000..32f615c73b --- /dev/null +++ b/frontend/src/providers/preferences/__tests__/tracesUpdaterConfig.test.ts @@ -0,0 +1,141 @@ +import { LOCALSTORAGE } from 'constants/localStorage'; +import { defaultOptionsQuery } from 'container/OptionsMenu/constants'; +import { + BaseAutocompleteData, + DataTypes, +} from 'types/api/queryBuilder/queryAutocompleteResponse'; + +import getTracesUpdaterConfig from '../configs/tracesUpdaterConfig'; + +// Mock setLocalStorageKey +const mockSetLocalStorageKey = jest.fn(); +jest.mock('api/browser/localstorage/set', () => ({ + __esModule: true, + default: (key: string, value: string): void => + mockSetLocalStorageKey(key, value), +})); + +// Mock localStorage +let mockLocalStorage: Record = {}; +Object.defineProperty(global, 'localStorage', { + value: { + getItem: jest.fn((key: string) => mockLocalStorage[key] || null), + setItem: jest.fn((key: string, value: string) => { + mockLocalStorage[key] = value; + }), + }, + writable: true, +}); + +describe('tracesUpdaterConfig', () => { + // Mock functions + const mockRedirectWithOptionsData = jest.fn(); + const mockSetSavedViewPreferences = jest.fn(); + + // Test data + const mockColumns: BaseAutocompleteData[] = [ + { + key: 'test-trace-column', + type: 'tag', + dataType: DataTypes.String, + isColumn: true, + }, + ]; + + beforeEach(() => { + jest.clearAllMocks(); + // Reset mockLocalStorage + mockLocalStorage = {}; + }); + + it('should update columns in localStorage and redirect with options in direct mode', () => { + const tracesUpdaterConfig = getTracesUpdaterConfig( + mockRedirectWithOptionsData, + mockSetSavedViewPreferences, + ); + + tracesUpdaterConfig.updateColumns(mockColumns, 'direct'); + + // Should redirect with the updated columns + expect(mockRedirectWithOptionsData).toHaveBeenCalledWith({ + ...defaultOptionsQuery, + selectColumns: mockColumns, + }); + + // Should set localStorage with the updated columns + expect(mockSetLocalStorageKey).toHaveBeenCalledWith( + LOCALSTORAGE.TRACES_LIST_OPTIONS, + JSON.stringify({ selectColumns: mockColumns }), + ); + }); + + it('should merge with existing localStorage data in direct mode', () => { + // Setup existing localStorage data + mockLocalStorage[LOCALSTORAGE.TRACES_LIST_OPTIONS] = JSON.stringify({ + selectColumns: [ + { + key: 'existing-column', + type: 'tag', + dataType: DataTypes.String, + isColumn: true, + }, + ], + otherProp: 'value', + }); + + const tracesUpdaterConfig = getTracesUpdaterConfig( + mockRedirectWithOptionsData, + mockSetSavedViewPreferences, + ); + + tracesUpdaterConfig.updateColumns(mockColumns, 'direct'); + + // Should set localStorage with the updated columns while preserving other props + expect(mockSetLocalStorageKey).toHaveBeenCalledWith( + LOCALSTORAGE.TRACES_LIST_OPTIONS, + JSON.stringify({ + selectColumns: mockColumns, + otherProp: 'value', + }), + ); + }); + + it('should update savedViewPreferences in savedView mode', () => { + const tracesUpdaterConfig = getTracesUpdaterConfig( + mockRedirectWithOptionsData, + mockSetSavedViewPreferences, + ); + + tracesUpdaterConfig.updateColumns(mockColumns, 'savedView'); + + // Should not redirect or modify localStorage in savedView mode + expect(mockRedirectWithOptionsData).not.toHaveBeenCalled(); + expect(mockSetLocalStorageKey).not.toHaveBeenCalled(); + + // Should update savedViewPreferences + expect(mockSetSavedViewPreferences).toHaveBeenCalledWith({ + columns: mockColumns, + formatting: { + maxLines: 2, + format: 'table', + fontSize: 'small', + version: 1, + }, + }); + }); + + it('should have a no-op updateFormatting method', () => { + const tracesUpdaterConfig = getTracesUpdaterConfig( + mockRedirectWithOptionsData, + mockSetSavedViewPreferences, + ); + + // Call updateFormatting and verify it does nothing + tracesUpdaterConfig.updateFormatting(); + + // No API calls should be made + expect(mockRedirectWithOptionsData).not.toHaveBeenCalled(); + expect(mockSetLocalStorageKey).not.toHaveBeenCalled(); + expect(mockSetSavedViewPreferences).not.toHaveBeenCalled(); + }); +});