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
Expand Up @@ -139,6 +139,21 @@ describe('guided-tour-context', () => {
expect(tour).toEqual(null);
expect(totalSteps).toEqual(undefined);
});

it('should not flash startTour when loaded transitions to true with completed tour', () => {
useSelectorMock.mockReturnValue({ A: true, B: false });
useResolvedExtensionsMock.mockReturnValue(mockTourExtension);
// Start with loaded: false (async ConfigMap fetch in progress)
useUserPreferenceMock.mockReturnValue([{ dev: { completed: false } }, () => null, false]);
const { result, rerender } = renderHook(() => useTourValuesForContext());
expect(result.current.tour).toEqual(null);

// Simulate ConfigMap load completing with completed: true
useUserPreferenceMock.mockReturnValue([{ dev: { completed: true } }, () => null, true]);
rerender();
const { tourState } = result.current;
expect(tourState?.startTour).not.toBe(true);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
expect(tourState?.startTour).not.toBe(true);
expect(tourState?.startTour).toBe(false);

});
Comment on lines +143 to +156
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Strengthen the test assertion to verify the tour context is returned.

The test currently only checks tourState?.startTour is not true, which would pass even if tour remains null (since undefined !== true). To properly verify the initialization guard allows the context through after loading, assert that:

  1. tour is not null (context was returned)
  2. startTour is explicitly false
🧪 Strengthened test assertions
     // Simulate ConfigMap load completing with completed: true
     useUserPreferenceMock.mockReturnValue([{ dev: { completed: true } }, () => null, true]);
     rerender();
-    const { tourState } = result.current;
-    expect(tourState?.startTour).not.toBe(true);
+    const { tourState, tour } = result.current;
+    expect(tour).not.toBeNull();
+    expect(tourState?.startTour).toBe(false);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('should not flash startTour when loaded transitions to true with completed tour', () => {
useSelectorMock.mockReturnValue({ A: true, B: false });
useResolvedExtensionsMock.mockReturnValue(mockTourExtension);
// Start with loaded: false (async ConfigMap fetch in progress)
useUserPreferenceMock.mockReturnValue([{ dev: { completed: false } }, () => null, false]);
const { result, rerender } = renderHook(() => useTourValuesForContext());
expect(result.current.tour).toEqual(null);
// Simulate ConfigMap load completing with completed: true
useUserPreferenceMock.mockReturnValue([{ dev: { completed: true } }, () => null, true]);
rerender();
const { tourState } = result.current;
expect(tourState?.startTour).not.toBe(true);
});
it('should not flash startTour when loaded transitions to true with completed tour', () => {
useSelectorMock.mockReturnValue({ A: true, B: false });
useResolvedExtensionsMock.mockReturnValue(mockTourExtension);
// Start with loaded: false (async ConfigMap fetch in progress)
useUserPreferenceMock.mockReturnValue([{ dev: { completed: false } }, () => null, false]);
const { result, rerender } = renderHook(() => useTourValuesForContext());
expect(result.current.tour).toEqual(null);
// Simulate ConfigMap load completing with completed: true
useUserPreferenceMock.mockReturnValue([{ dev: { completed: true } }, () => null, true]);
rerender();
const { tourState, tour } = result.current;
expect(tour).not.toBeNull();
expect(tourState?.startTour).toBe(false);
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@frontend/packages/console-app/src/components/tour/__tests__/tour-context.spec.ts`
around lines 143 - 156, Update the test for useTourValuesForContext so it
asserts the tour context is actually returned and that startTour is explicitly
false: after rerender, add an assertion that result.current.tour is not null
(ensuring the context was initialized) and change the startTour assertion to
expect(false) (i.e., expect(result.current.tourState?.startTour).toBe(false))
instead of only checking it is not true; reference useTourValuesForContext,
tour, tourState, and startTour when locating the test to modify.

});

describe('useTourStatePerspective', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Reducer, Dispatch, ReducerAction } from 'react';
import { createContext, useReducer, useState, useEffect, useCallback } from 'react';
import { createContext, useReducer, useRef, useState, useEffect, useCallback } from 'react';
import { pick, union, isEqual } from 'lodash';
import { createSelector } from 'reselect';
import { useActivePerspective } from '@console/dynamic-plugin-sdk';
Expand Down Expand Up @@ -151,14 +151,18 @@ export const useTourValuesForContext = (): TourContextType => {
startTour: !completed,
});

const initializedWithLoadedData = useRef(false);
useEffect(() => {
tourDispatch({ type: TourActions.initialize, payload: { completed } });
setPerspective(activePerspective);
if (loaded) {
initializedWithLoadedData.current = true;
}
// only run effect when the active perspective changes
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [activePerspective, loaded]);

if (!tour || !loaded) return { tour: null };
if (!tour || !loaded || !initializedWithLoadedData.current) return { tour: null };
const {
properties: {
tour: { intro, steps: unfilteredSteps, end },
Expand Down