Skip to content

Commit 0f2f788

Browse files
authored
Fix reading property of empty object (#1100)
Signed-off-by: Assem Hafez <[email protected]>
1 parent 5a1b5f3 commit 0f2f788

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/views/workflow-history/hooks/__tests__/use-initial-selected-event.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,34 @@ describe('useInitialSelectedEvent', () => {
190190
expect(result.current.initialEventFound).toBe(true);
191191
expect(result.current.initialEventGroupIndex).toBe(3);
192192
});
193+
194+
// add test cases for filteredEventGroupsEntries being empty after filtering
195+
it.only('should return initialEventGroupIndex as undefined when filteredEventGroupsEntries no longer contains the event', () => {
196+
const initialFilteredEventGroupsEntries: [string, HistoryEventsGroup][] = [
197+
['2', mockEventGroups['2']],
198+
];
199+
200+
// initial render with filteredEventGroupsEntries containing the event
201+
const { result, rerender } = renderHook(
202+
({
203+
filteredEventGroupsEntries = initialFilteredEventGroupsEntries,
204+
}: {
205+
filteredEventGroupsEntries?: [string, HistoryEventsGroup][];
206+
} = {}) =>
207+
useInitialSelectedEvent({
208+
selectedEventId: '2',
209+
eventGroups: mockEventGroups,
210+
filteredEventGroupsEntries: filteredEventGroupsEntries,
211+
})
212+
);
213+
expect(result.current.initialEventFound).toBe(true);
214+
expect(result.current.initialEventGroupIndex).toBe(0);
215+
216+
//rerender with empty filteredEventGroupsEntries no longer containing the event
217+
rerender({
218+
filteredEventGroupsEntries: [],
219+
});
220+
expect(result.current.initialEventGroupIndex).toBe(undefined);
221+
expect(result.current.initialEventFound).toBe(true);
222+
});
193223
});

src/views/workflow-history/hooks/use-initial-selected-event.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { useMemo, useRef, useState } from 'react';
33
import { type UseInitialSelectedEventParams } from './use-initial-selected-event.types';
44

55
/*
6-
* This hook is used to search for the event and the group of the event that
7-
* was selected when the component is mounted. It returns a boolean indicating if the
8-
* initial event should be searched for, a boolean indicating if the initial
9-
* event was found, and the index of the group that contains the event.
6+
* This hook is used to search for the event within event groups. It captures the first selected event when the component is mounted
7+
* and keeps searching for the event in all event groups until it is found.
8+
* Afterwards it return the index of the group in the filtered set of event groups that is presented on the UI and retrun undefined if filtered groups do not contain the event/group.
109
*/
1110
export default function useInitialSelectedEvent({
1211
selectedEventId,
@@ -35,7 +34,7 @@ export default function useInitialSelectedEvent({
3534
// If group index not change do not search again.
3635
if (
3736
foundGroupIndexRef.current &&
38-
filteredEventGroupsEntries[foundGroupIndexRef.current][0] === groupId
37+
filteredEventGroupsEntries[foundGroupIndexRef.current]?.[0] === groupId
3938
)
4039
return foundGroupIndexRef.current;
4140

0 commit comments

Comments
 (0)