Skip to content

Commit

Permalink
fix flaky unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Li <[email protected]>
  • Loading branch information
joshuali925 committed Nov 7, 2024
1 parent fc4d91f commit 9dd86ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const createSetupContractMock = () => {
isAutoRefreshSelectorEnabled: jest.fn(),
isTimeRangeSelectorEnabled: jest.fn(),
getEnabledUpdated$: jest.fn(),
getTimeUpdate$: jest.fn(),
getRefreshIntervalUpdate$: jest.fn(),
getTimeUpdate$: jest.fn(() => new Observable<unknown>()),
getRefreshIntervalUpdate$: jest.fn(() => new Observable<unknown>()),

Check warning on line 42 in src/plugins/data/public/query/timefilter/timefilter_service.mock.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/timefilter/timefilter_service.mock.ts#L42

Added line #L42 was not covered by tests
getAutoRefreshFetch$: jest.fn(() => new Observable<unknown>()),
getFetch$: jest.fn(),
getFetch$: jest.fn(() => new Observable<unknown>()),
getTime: jest.fn(),
setTime: jest.fn(),
setRefreshInterval: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,17 @@ describe('useSearch', () => {
const services = createMockServices();
(services.uiSettings.get as jest.Mock).mockReturnValueOnce(true);

const { result } = renderHook(() => useSearch(services), { wrapper });
const { result, waitForNextUpdate } = renderHook(() => useSearch(services), { wrapper });

expect(result.current.data$.getValue()).toEqual(
expect.objectContaining({ status: ResultStatus.LOADING })
);

// useSearch updates state async in useEffect, wait for it to finish to
// avoid warning
await act(async () => {
await waitForNextUpdate();
});
});

it('should initialize with uninitialized state when search on page load is disabled', async () => {
Expand All @@ -94,16 +100,20 @@ describe('useSearch', () => {
value: 10,
});

const { result } = renderHook(() => useSearch(services), { wrapper });
const { result, waitForNextUpdate } = renderHook(() => useSearch(services), { wrapper });
expect(result.current.data$.getValue()).toEqual(
expect.objectContaining({ status: ResultStatus.UNINITIALIZED })
);

await act(async () => {
await waitForNextUpdate();
});
});

it('should update startTime when hook rerenders', async () => {
const services = createMockServices();

const { result, rerender } = renderHook(() => useSearch(services), {
const { result, rerender, waitForNextUpdate } = renderHook(() => useSearch(services), {
wrapper,
});

Expand All @@ -114,6 +124,10 @@ describe('useSearch', () => {
const newStartTime = result.current.data$.getValue().queryStatus?.startTime;
expect(newStartTime).toBeDefined();
expect(newStartTime).not.toEqual(initialStartTime);

await act(async () => {
await waitForNextUpdate();
});
});

it('should reset data observable when dataset changes', async () => {
Expand Down

0 comments on commit 9dd86ac

Please sign in to comment.