Skip to content

Commit b91b8b7

Browse files
committed
fix test cases
Signed-off-by: Assem Hafez <assem.hafez@uber.com>
1 parent 7841c38 commit b91b8b7

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

src/views/workflow-history/__tests__/workflow-history.test.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jest.mock('@/hooks/use-page-query-params/use-page-query-params', () =>
2828
jest.fn(() => [{ historySelectedEventId: '1' }, jest.fn()])
2929
);
3030

31-
// Mock the hook to use minimal throttle delay for faster tests
31+
// Mock the hooks to use minimal throttle delay for faster tests
3232
jest.mock('../hooks/use-workflow-history-fetcher', () => {
3333
const actual = jest.requireActual('../hooks/use-workflow-history-fetcher');
3434
return {
@@ -39,6 +39,14 @@ jest.mock('../hooks/use-workflow-history-fetcher', () => {
3939
};
4040
});
4141

42+
jest.mock('../hooks/use-workflow-history-grouper', () => {
43+
const actual = jest.requireActual('../hooks/use-workflow-history-grouper');
44+
return {
45+
__esModule: true,
46+
default: jest.fn(() => actual.default(0)), // 0ms throttle for tests
47+
};
48+
});
49+
4250
jest.mock(
4351
'../workflow-history-compact-event-card/workflow-history-compact-event-card',
4452
() => jest.fn(() => <div>Compact group Card</div>)

src/views/workflow-history/helpers/__tests__/workflow-history-grouper.test.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,18 @@ describe(WorkflowHistoryGrouper.name, () => {
625625
status: 'idle',
626626
});
627627

628-
// Add events but don't wait - status should be processing
628+
// Add events - with batchSize=1, first batch will be processed synchronously but subsequent batches will be async
629629
grouper.updateEvents(completedActivityTaskEvents);
630+
631+
// Check state after first batch (might be synchronous)
630632
state = grouper.getState();
631-
expect(state.status).toBe('processing');
632-
expect(state.remainingEventsCount).toBeGreaterThan(0);
633+
// First batch is processed immediately, so processedEventsCount should be at least 1
634+
expect(state.processedEventsCount).toBeGreaterThan(0);
635+
636+
// If there are remaining events, status could be 'processing'
637+
if (state.remainingEventsCount > 0) {
638+
expect(state.status).toBe('processing');
639+
}
633640

634641
// Wait for processing to complete
635642
await waitForProcessing();
@@ -677,6 +684,11 @@ function setup(options: Partial<Props> = {}) {
677684

678685
// Helper function to wait for next processing cycle
679686
const waitForProcessing = async (timeout = 1000): Promise<void> => {
687+
// Check if already idle (processing completed synchronously)
688+
if (grouper.getState().status === 'idle') {
689+
return Promise.resolve();
690+
}
691+
680692
await new Promise<void>((resolve, reject) => {
681693
const timeoutId = setTimeout(() => {
682694
// Remove this resolver from queue if it times out

src/views/workflow-history/helpers/workflow-history-fetcher.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ export default class WorkflowHistoryFetcher {
4444
if (shouldContinue) {
4545
this.shouldContinue = shouldContinue;
4646
}
47-
// If already started, return
48-
if (this.isStarted) return;
47+
48+
// remove current listener (if exists) to have fresh emits only
49+
this.unsubscribe?.();
50+
this.unsubscribe = null;
51+
4952
this.isStarted = true;
5053
let emitCount = 0;
5154
const currentState = this.observer.getCurrentResult();
5255
const fetchedFirstPage = currentState.status !== 'pending';
53-
const shouldEnableQuery = !fetchedFirstPage && shouldContinue(currentState);
56+
const shouldEnableQuery =
57+
!fetchedFirstPage && this.shouldContinue(currentState);
5458

5559
if (shouldEnableQuery) {
5660
this.observer.setOptions({
@@ -81,8 +85,6 @@ export default class WorkflowHistoryFetcher {
8185
emit(currentState);
8286
}
8387

84-
// remove current listener (if exists) and add new one
85-
this.unsubscribe?.();
8688
this.unsubscribe = this.observer.subscribe((res) => emit(res));
8789
}
8890

src/views/workflow-history/hooks/use-workflow-history-fetcher.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default function useWorkflowHistoryFetcher(
2828

2929
if (!fetcherRef.current) {
3030
fetcherRef.current = new WorkflowHistoryFetcher(queryClient, params);
31+
3132
// Fetch first page
3233
fetcherRef.current.start((state) => !state?.data?.pages?.length);
3334
}

0 commit comments

Comments
 (0)