mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-17 09:26:03 +08:00
chore: added tests for traces loader and updater
This commit is contained in:
parent
34cd2c1b40
commit
2f9943a7b8
@ -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<string, string> = {};
|
||||
|
||||
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<Location>;
|
||||
|
||||
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[],
|
||||
});
|
||||
});
|
||||
});
|
@ -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<string, string> = {};
|
||||
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();
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user