mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-13 05:19:04 +08:00
fix: cover the title as reactNode case for useGetResolvedText (#7965)
* fix: cover the title as reactNode case for useResolvedText * fix: added more test cases
This commit is contained in:
parent
e34e61a20d
commit
33506cafce
@ -1,4 +1,5 @@
|
|||||||
import { renderHook } from '@testing-library/react';
|
import { renderHook } from '@testing-library/react';
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
import useGetResolvedText from '../useGetResolvedText';
|
import useGetResolvedText from '../useGetResolvedText';
|
||||||
|
|
||||||
@ -20,7 +21,7 @@ describe('useGetResolvedText', () => {
|
|||||||
const TEXT_TEMPLATE = 'Logs count in $service.name in $severity';
|
const TEXT_TEMPLATE = 'Logs count in $service.name in $severity';
|
||||||
|
|
||||||
const renderHookWithProps = (props: {
|
const renderHookWithProps = (props: {
|
||||||
text: string;
|
text: string | React.ReactNode;
|
||||||
variables?: Record<string, string | number | boolean>;
|
variables?: Record<string, string | number | boolean>;
|
||||||
dashboardVariables?: Record<string, any>;
|
dashboardVariables?: Record<string, any>;
|
||||||
maxLength?: number;
|
maxLength?: number;
|
||||||
@ -130,4 +131,52 @@ describe('useGetResolvedText', () => {
|
|||||||
'Logs count in test, app, frontend, env in $unknown',
|
'Logs count in test, app, frontend, env in $unknown',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should handle non-string text input (ReactNode)', () => {
|
||||||
|
const reactNodeText = <div>Test ReactNode</div>;
|
||||||
|
const variables = {
|
||||||
|
'service.name': SERVICE_VAR,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { result } = renderHookWithProps({
|
||||||
|
text: reactNodeText,
|
||||||
|
variables,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Should return the ReactNode unchanged
|
||||||
|
expect(result.current.fullText).toBe(reactNodeText);
|
||||||
|
expect(result.current.truncatedText).toBe(reactNodeText);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle number input', () => {
|
||||||
|
const text = 123;
|
||||||
|
const variables = {
|
||||||
|
'service.name': SERVICE_VAR,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { result } = renderHookWithProps({
|
||||||
|
text,
|
||||||
|
variables,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Should return the number unchanged
|
||||||
|
expect(result.current.fullText).toBe(text);
|
||||||
|
expect(result.current.truncatedText).toBe(text);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle boolean input', () => {
|
||||||
|
const text = true;
|
||||||
|
const variables = {
|
||||||
|
'service.name': SERVICE_VAR,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { result } = renderHookWithProps({
|
||||||
|
text,
|
||||||
|
variables,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Should return the boolean unchanged
|
||||||
|
expect(result.current.fullText).toBe(text);
|
||||||
|
expect(result.current.truncatedText).toBe(text);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -5,10 +5,10 @@
|
|||||||
// return value should be a full text string, and a truncated text string (if max length is provided)
|
// return value should be a full text string, and a truncated text string (if max length is provided)
|
||||||
|
|
||||||
import { useDashboard } from 'providers/Dashboard/Dashboard';
|
import { useDashboard } from 'providers/Dashboard/Dashboard';
|
||||||
import { useCallback, useMemo } from 'react';
|
import { ReactNode, useCallback, useMemo } from 'react';
|
||||||
|
|
||||||
interface UseGetResolvedTextProps {
|
interface UseGetResolvedTextProps {
|
||||||
text: string;
|
text: string | ReactNode;
|
||||||
variables?: Record<string, string | number | boolean>;
|
variables?: Record<string, string | number | boolean>;
|
||||||
maxLength?: number;
|
maxLength?: number;
|
||||||
matcher?: string;
|
matcher?: string;
|
||||||
@ -16,10 +16,11 @@ interface UseGetResolvedTextProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface ResolvedTextResult {
|
interface ResolvedTextResult {
|
||||||
fullText: string;
|
fullText: string | ReactNode;
|
||||||
truncatedText: string;
|
truncatedText: string | ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
||||||
function useGetResolvedText({
|
function useGetResolvedText({
|
||||||
text,
|
text,
|
||||||
variables,
|
variables,
|
||||||
@ -28,6 +29,7 @@ function useGetResolvedText({
|
|||||||
maxValues = 2, // Default to showing 2 values before +n more
|
maxValues = 2, // Default to showing 2 values before +n more
|
||||||
}: UseGetResolvedTextProps): ResolvedTextResult {
|
}: UseGetResolvedTextProps): ResolvedTextResult {
|
||||||
const { selectedDashboard } = useDashboard();
|
const { selectedDashboard } = useDashboard();
|
||||||
|
const isString = typeof text === 'string';
|
||||||
|
|
||||||
const processedDashboardVariables = useMemo(() => {
|
const processedDashboardVariables = useMemo(() => {
|
||||||
if (variables) return variables;
|
if (variables) return variables;
|
||||||
@ -108,9 +110,10 @@ function useGetResolvedText({
|
|||||||
[matcher],
|
[matcher],
|
||||||
);
|
);
|
||||||
|
|
||||||
const fullText = useMemo(
|
const fullText = useMemo(() => {
|
||||||
() =>
|
if (!isString) return text;
|
||||||
text.replace(combinedPattern, (match) => {
|
|
||||||
|
return (text as string)?.replace(combinedPattern, (match) => {
|
||||||
const varName = extractVarName(match);
|
const varName = extractVarName(match);
|
||||||
const value = processedVariables[varName];
|
const value = processedVariables[varName];
|
||||||
|
|
||||||
@ -119,12 +122,13 @@ function useGetResolvedText({
|
|||||||
return parts.length > 1 ? parts[1] : value;
|
return parts.length > 1 ? parts[1] : value;
|
||||||
}
|
}
|
||||||
return match;
|
return match;
|
||||||
}),
|
});
|
||||||
[text, processedVariables, combinedPattern, extractVarName],
|
}, [text, processedVariables, combinedPattern, extractVarName, isString]);
|
||||||
);
|
|
||||||
|
|
||||||
const truncatedText = useMemo(() => {
|
const truncatedText = useMemo(() => {
|
||||||
const result = text.replace(combinedPattern, (match) => {
|
if (!isString) return text;
|
||||||
|
|
||||||
|
const result = (text as string)?.replace(combinedPattern, (match) => {
|
||||||
const varName = extractVarName(match);
|
const varName = extractVarName(match);
|
||||||
const value = processedVariables[varName];
|
const value = processedVariables[varName];
|
||||||
|
|
||||||
@ -145,7 +149,14 @@ function useGetResolvedText({
|
|||||||
return `${result.substring(0, maxLength - 3)}...`;
|
return `${result.substring(0, maxLength - 3)}...`;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}, [text, processedVariables, combinedPattern, maxLength, extractVarName]);
|
}, [
|
||||||
|
text,
|
||||||
|
processedVariables,
|
||||||
|
combinedPattern,
|
||||||
|
maxLength,
|
||||||
|
extractVarName,
|
||||||
|
isString,
|
||||||
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
fullText,
|
fullText,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user