Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useState } from 'react';
import { act } from '@testing-library/react';
import { act, renderHook } from '@testing-library/react';
import { useDispatch } from 'react-redux';
import { useLocation } from 'react-router-dom-v5-compat';
import { k8sGet } from '@console/dynamic-plugin-sdk/src/utils/k8s';
import { ALL_NAMESPACES_KEY } from '@console/shared/src/constants';
import { useFlag } from '@console/shared/src/hooks/flag';
import { testHook } from '@console/shared/src/test-utils/hooks-utils';
import { usePreferredNamespace } from '../../user-preferences/namespace/usePreferredNamespace';
import { useValuesForNamespaceContext } from '../namespace';
import { useLastNamespace } from '../useLastNamespace';
Expand Down Expand Up @@ -75,7 +74,7 @@ describe('useValuesForNamespaceContext', () => {
usePreferredNamespaceMock.mockReturnValue([preferredNamespace, jest.fn(), true]);
useLastNamespaceMock.mockReturnValue([lastNamespace, jest.fn(), true]);

const { result, rerender } = testHook(() => useValuesForNamespaceContext());
const { result, rerender } = renderHook(() => useValuesForNamespaceContext());
await act(async () => {
rerender();
});
Expand All @@ -93,7 +92,7 @@ describe('useValuesForNamespaceContext', () => {
usePreferredNamespaceMock.mockReturnValue([preferredNamespace, jest.fn(), true]);
useLastNamespaceMock.mockReturnValue([lastNamespace, jest.fn(), true]);

const { result, rerender } = testHook(() => useValuesForNamespaceContext());
const { result, rerender } = renderHook(() => useValuesForNamespaceContext());
await act(async () => {
rerender();
});
Expand All @@ -110,7 +109,7 @@ describe('useValuesForNamespaceContext', () => {
usePreferredNamespaceMock.mockReturnValue([preferredNamespace, jest.fn(), true]);
useLastNamespaceMock.mockReturnValue([lastNamespace, jest.fn(), true]);

const { result, rerender } = testHook(() => useValuesForNamespaceContext());
const { result, rerender } = renderHook(() => useValuesForNamespaceContext());
await act(async () => {
rerender();
});
Expand All @@ -127,7 +126,7 @@ describe('useValuesForNamespaceContext', () => {
usePreferredNamespaceMock.mockReturnValue([undefined, jest.fn(), true]);
useLastNamespaceMock.mockReturnValue([lastNamespace, jest.fn(), true]);

const { result, rerender } = testHook(() => useValuesForNamespaceContext());
const { result, rerender } = renderHook(() => useValuesForNamespaceContext());
await act(async () => {
rerender();
});
Expand All @@ -144,7 +143,7 @@ describe('useValuesForNamespaceContext', () => {
usePreferredNamespaceMock.mockReturnValue([undefined, jest.fn(), true]);
useLastNamespaceMock.mockReturnValue([undefined, jest.fn(), true]);

const { result, rerender } = testHook(() => useValuesForNamespaceContext());
const { result, rerender } = renderHook(() => useValuesForNamespaceContext());
await act(async () => {
rerender();
});
Expand All @@ -161,7 +160,7 @@ describe('useValuesForNamespaceContext', () => {
usePreferredNamespaceMock.mockReturnValue([preferredNamespace, jest.fn(), false]);
useLastNamespaceMock.mockReturnValue([lastNamespace, jest.fn(), false]);

const { result, rerender } = testHook(() => useValuesForNamespaceContext());
const { result, rerender } = renderHook(() => useValuesForNamespaceContext());
await act(async () => {
rerender();
});
Expand All @@ -178,7 +177,7 @@ describe('useValuesForNamespaceContext', () => {
usePreferredNamespaceMock.mockReturnValue([preferredNamespace, jest.fn(), true]);
useLastNamespaceMock.mockReturnValue([lastNamespace, jest.fn(), true]);

const { result, rerender } = testHook(() => useValuesForNamespaceContext());
const { result, rerender } = renderHook(() => useValuesForNamespaceContext());
await act(async () => {
rerender();
});
Expand All @@ -195,7 +194,7 @@ describe('useValuesForNamespaceContext', () => {
usePreferredNamespaceMock.mockReturnValue([undefined, jest.fn(), false]);
useLastNamespaceMock.mockReturnValue([lastNamespace, jest.fn(), false]);

const { result, rerender } = testHook(() => useValuesForNamespaceContext());
const { result, rerender } = renderHook(() => useValuesForNamespaceContext());
await act(async () => {
rerender();
});
Expand All @@ -212,7 +211,7 @@ describe('useValuesForNamespaceContext', () => {
usePreferredNamespaceMock.mockReturnValue([undefined, jest.fn(), true]);
useLastNamespaceMock.mockReturnValue([lastNamespace, jest.fn(), true]);

const { result, rerender } = testHook(() => useValuesForNamespaceContext());
const { result, rerender } = renderHook(() => useValuesForNamespaceContext());
await act(async () => {
rerender();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { renderHook } from '@testing-library/react';
import { useSelector } from 'react-redux';
import { useResolvedExtensions } from '@console/dynamic-plugin-sdk/src/api/useResolvedExtensions';
import { useUserSettingsCompatibility } from '@console/shared/src/hooks/useUserSettingsCompatibility';
import { testHook } from '@console/shared/src/test-utils/hooks-utils';
import { TourActions } from '../const';
import { tourReducer, useTourValuesForContext, useTourStateForPerspective } from '../tour-context';
import { TourDataType } from '../type';
Expand Down Expand Up @@ -104,20 +104,18 @@ describe('guided-tour-context', () => {
() => null,
true,
]);
testHook(() => {
const contextValue = useTourValuesForContext();
const { tourState, tour, totalSteps } = contextValue;
expect(tourState).toEqual({
startTour: true,
completedTour: false,
stepNumber: 0,
});
expect(tour).toEqual({
...mockTour,
steps: [{ flags: ['A'], heading: 'g', content: 'h' }],
});
expect(totalSteps).toEqual(1);
const { result } = renderHook(() => useTourValuesForContext());
const { tourState, tour, totalSteps } = result.current;
expect(tourState).toEqual({
startTour: true,
completedTour: false,
stepNumber: 0,
});
expect(tour).toEqual({
...mockTour,
steps: [{ flags: ['A'], heading: 'g', content: 'h' }],
});
expect(totalSteps).toEqual(1);
});

it('should return tour null from the hook', () => {
Expand All @@ -130,13 +128,11 @@ describe('guided-tour-context', () => {
() => null,
true,
]);
testHook(() => {
const contextValue = useTourValuesForContext();
const { tourState, tour, totalSteps } = contextValue;
expect(tourState).toEqual(undefined);
expect(tour).toEqual(null);
expect(totalSteps).toEqual(undefined);
});
const { result } = renderHook(() => useTourValuesForContext());
const { tourState, tour, totalSteps } = result.current;
expect(tourState).toEqual(undefined);
expect(tour).toEqual(null);
expect(totalSteps).toEqual(undefined);
});

it('should return null from the hook if tour is available but data isnot loaded', () => {
Expand All @@ -150,13 +146,11 @@ describe('guided-tour-context', () => {
() => null,
false,
]);
testHook(() => {
const contextValue = useTourValuesForContext();
const { tourState, tour, totalSteps } = contextValue;
expect(tourState).toEqual(undefined);
expect(tour).toEqual(null);
expect(totalSteps).toEqual(undefined);
});
const { result } = renderHook(() => useTourValuesForContext());
const { tourState, tour, totalSteps } = result.current;
expect(tourState).toEqual(undefined);
expect(tour).toEqual(null);
expect(totalSteps).toEqual(undefined);
});
});

Expand All @@ -167,11 +161,10 @@ describe('guided-tour-context', () => {
() => null,
true,
]);
testHook(() => {
const [state, , loaded] = useTourStateForPerspective('dev');
expect(state).toEqual({ a: true });
expect(loaded).toEqual(true);
});
const { result } = renderHook(() => useTourStateForPerspective('dev'));
const [state, , loaded] = result.current;
expect(state).toEqual({ a: true });
expect(loaded).toEqual(true);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { testHook } from '@console/shared/src/test-utils/hooks-utils';
import { renderHook } from '@testing-library/react';
import useTranslationExt from '../useTranslationExt';

describe('useTranslationExt', () => {
it('should return the input if key does not match translation pattern', () => {
testHook(() => {
const { t: translate } = useTranslationExt();
expect(translate('%')).toBe('%');
expect(translate('a%')).toBe('a%');
expect(translate('%a')).toBe('%a');
expect(translate('%%')).toBe('%%');
expect(translate('foo')).toBe('foo');
});
const { result } = renderHook(() => useTranslationExt());
const { t: translate } = result.current;
expect(translate('%')).toBe('%');
expect(translate('a%')).toBe('a%');
expect(translate('%a')).toBe('%a');
expect(translate('%%')).toBe('%%');
expect(translate('foo')).toBe('foo');
});

it('should parse as a translation key', () => {
testHook(() => {
const { t: translate } = useTranslationExt();
expect(translate('%test~key%')).toBe('key');
});
const { result } = renderHook(() => useTranslationExt());
const { t: translate } = result.current;
expect(translate('%test~key%')).toBe('key');
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { renderHook } from '@testing-library/react';
import {
CatalogItemType,
CatalogItemTypeMetadata,
CatalogItemProvider,
CatalogItemFilter,
CatalogItemMetadataProvider,
} from '@console/dynamic-plugin-sdk/src/extensions';
import { testHook } from '@console/shared/src/test-utils/hooks-utils';
import useCatalogExtensions from '../useCatalogExtensions';

let mockExtensions: (
Expand Down Expand Up @@ -38,9 +38,9 @@ describe('useCatalogExtensions', () => {
},
},
];
const allExtensions = testHook(() => useCatalogExtensions('test-catalog')).result.current[0];
const allExtensions = renderHook(() => useCatalogExtensions('test-catalog')).result.current[0];
expect(allExtensions).toEqual([mockExtensions[0], mockExtensions[1]]);
const extensions = testHook(() => useCatalogExtensions('test-catalog', 'type2')).result
const extensions = renderHook(() => useCatalogExtensions('test-catalog', 'type2')).result
.current[0];
expect(extensions).toEqual([mockExtensions[1]]);
});
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('useCatalogExtensions', () => {
},
},
];
const catalogTypeExtensions = testHook(() => useCatalogExtensions('test-catalog')).result
const catalogTypeExtensions = renderHook(() => useCatalogExtensions('test-catalog')).result
.current[0];
expect(catalogTypeExtensions).toEqual([
mockExtensions[0],
Expand Down Expand Up @@ -161,10 +161,10 @@ describe('useCatalogExtensions', () => {
},
];

const allExtensions = testHook(() => useCatalogExtensions('test-catalog')).result.current[1];
const allExtensions = renderHook(() => useCatalogExtensions('test-catalog')).result.current[1];
expect(allExtensions).toEqual([mockExtensions[0], mockExtensions[1]]);

const extensions = testHook(() => useCatalogExtensions('test-catalog', 'type2')).result
const extensions = renderHook(() => useCatalogExtensions('test-catalog', 'type2')).result
.current[1];
expect(extensions).toEqual([mockExtensions[1]]);
});
Expand All @@ -189,10 +189,10 @@ describe('useCatalogExtensions', () => {
},
];

const allExtensions = testHook(() => useCatalogExtensions('test-catalog')).result.current[2];
const allExtensions = renderHook(() => useCatalogExtensions('test-catalog')).result.current[2];
expect(allExtensions).toEqual([mockExtensions[0], mockExtensions[1]]);

const extensions = testHook(() => useCatalogExtensions('test-catalog', 'type2')).result
const extensions = renderHook(() => useCatalogExtensions('test-catalog', 'type2')).result
.current[2];
expect(extensions).toEqual([mockExtensions[1]]);
});
Expand All @@ -217,10 +217,10 @@ describe('useCatalogExtensions', () => {
},
];

const allExtensions = testHook(() => useCatalogExtensions('test-catalog')).result.current[3];
const allExtensions = renderHook(() => useCatalogExtensions('test-catalog')).result.current[3];
expect(allExtensions).toEqual([mockExtensions[0], mockExtensions[1]]);

const extensions = testHook(() => useCatalogExtensions('test-catalog', 'type2')).result
const extensions = renderHook(() => useCatalogExtensions('test-catalog', 'type2')).result
.current[3];
expect(extensions).toEqual([mockExtensions[1]]);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useState } from 'react';
import { renderHook } from '@testing-library/react';
import {
PREFERRED_CREATE_EDIT_METHOD_USER_SETTING_VALUE_LATEST,
usePreferredCreateEditMethod,
} from '@console/app/src/components/user-preferences/synced-editor/usePreferredCreateEditMethod';
import { testHook } from '@console/shared/src/test-utils/hooks-utils';
import { useUserSettings } from '../../../hooks/useUserSettings';
import { EditorType } from '../editor-toggle';
import { useEditorType } from '../useEditorType';
Expand Down Expand Up @@ -44,7 +44,7 @@ describe('useEditorType', () => {
it('should return editor type corresponding to preferred editor type if it is defined and enabled', () => {
mockUserSettings.mockReturnValue([EditorType.Form, jest.fn(), true]);
mockUsePreferredCreateEditMethod.mockReturnValue([EditorType.YAML, true]);
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const { result } = renderHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const [editorType, , loaded] = result.current;
expect(editorType).toEqual(EditorType.YAML);
expect(loaded).toBe(true);
Expand All @@ -56,7 +56,7 @@ describe('useEditorType', () => {
PREFERRED_CREATE_EDIT_METHOD_USER_SETTING_VALUE_LATEST,
true,
]);
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const { result } = renderHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const [editorType, , loaded] = result.current;
expect(editorType).toEqual(EditorType.YAML);
expect(loaded).toBe(true);
Expand All @@ -65,7 +65,7 @@ describe('useEditorType', () => {
it('should return editor type corresponding to last viewed editor type if it is defined and enabled preferred editor type is defined but disabled', () => {
mockUserSettings.mockReturnValue([EditorType.YAML, jest.fn(), true]);
mockUsePreferredCreateEditMethod.mockReturnValue([EditorType.Form, true]);
const { result } = testHook(() =>
const { result } = renderHook(() =>
useEditorType(
lastViewUserSettingKey,
defaultValue,
Expand All @@ -80,7 +80,7 @@ describe('useEditorType', () => {
it('should return editor type corresponding to last viewed editor type if it is defined and enabled and preferred editor type is not defined', () => {
mockUserSettings.mockReturnValue([EditorType.YAML, jest.fn(), true]);
mockUsePreferredCreateEditMethod.mockReturnValue([undefined, true]);
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const { result } = renderHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const [editorType, , loaded] = result.current;
expect(editorType).toEqual(EditorType.YAML);
expect(loaded).toBe(true);
Expand All @@ -89,7 +89,7 @@ describe('useEditorType', () => {
it('should return editor type corresponding to default value if both preferred and last viewed editor type are not defined or disabled', () => {
mockUserSettings.mockReturnValue([undefined, jest.fn(), true]);
mockUsePreferredCreateEditMethod.mockReturnValue([undefined, true]);
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const { result } = renderHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const [editorType, , loaded] = result.current;
expect(editorType).toEqual(defaultValue);
expect(loaded).toBe(true);
Expand All @@ -98,7 +98,7 @@ describe('useEditorType', () => {
it('should return false for loaded and null for editor type if preferred editor type has not loaded', () => {
mockUserSettings.mockReturnValue([EditorType.YAML, jest.fn(), true]);
mockUsePreferredCreateEditMethod.mockReturnValue([undefined, false]);
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const { result } = renderHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const [editorType, , loaded] = result.current;
expect(editorType).toEqual(null);
expect(loaded).toBe(false);
Expand All @@ -107,7 +107,7 @@ describe('useEditorType', () => {
it('should return false for loaded and null for editor type if last viewed editor type has not loaded', () => {
mockUserSettings.mockReturnValue([undefined, jest.fn(), false]);
mockUsePreferredCreateEditMethod.mockReturnValue([EditorType.YAML, true]);
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const { result } = renderHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const [editorType, , loaded] = result.current;
expect(editorType).toEqual(null);
expect(loaded).toBe(false);
Expand All @@ -117,7 +117,7 @@ describe('useEditorType', () => {
mockUserSettings.mockReturnValue([undefined, jest.fn(), true]);
mockUsePreferredCreateEditMethod.mockReturnValue([EditorType.YAML, true]);
mockUseState.mockReturnValue([null, jest.fn()]);
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const { result } = renderHook(() => useEditorType(lastViewUserSettingKey, defaultValue));
const [editorType, , loaded] = result.current;
expect(editorType).toEqual(null);
expect(loaded).toBe(false);
Expand All @@ -127,7 +127,7 @@ describe('useEditorType', () => {
mockUserSettings.mockReturnValue([undefined, jest.fn(), true]);
mockUsePreferredCreateEditMethod.mockReturnValue([EditorType.YAML, true]);
mockUseState.mockReturnValue([null, jest.fn()]);
const { result } = testHook(() => useEditorType(lastViewUserSettingKey, null));
const { result } = renderHook(() => useEditorType(lastViewUserSettingKey, null));
const [editorType, , loaded] = result.current;
expect(editorType).toEqual(null);
expect(loaded).toBe(true);
Expand Down
Loading