Skip to content

Commit

Permalink
rewrite tests to remove hack for waitForNextUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
eokoneyo committed Dec 18, 2024
1 parent 9f5a3af commit a80bf74
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ describe('useAggregatedAnomaliesByJob', () => {
wrapper: TestProviders,
});

await waitFor(() => new Promise((resolve) => resolve(null)));

act(() => {
result.current.refetch();
});
Expand Down Expand Up @@ -145,12 +143,13 @@ describe('useAggregatedAnomaliesByJob', () => {
const { result } = renderHook(() => useAggregatedAnomaliesByJob({ skip: false, from, to }), {
wrapper: TestProviders,
});
await waitFor(() => new Promise((resolve) => resolve(null)));

const names = result.current.data.map(({ name }) => name);
await waitFor(() => {
const names = result.current.data.map(({ name }) => name);

expect(names[0]).toEqual(firstJobSecurityName);
expect(names[1]).toEqual(secondJobSecurityName);
expect(names[0]).toEqual(firstJobSecurityName);
expect(names[1]).toEqual(secondJobSecurityName);
});
});

it('does not throw error when aggregations is undefined', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,70 +18,70 @@ describe('useLocalStorage', () => {

it('should return an empty array when there is no messages', async () => {
const { result } = renderHook(() => useMessagesStorage());
await waitFor(() => new Promise((resolve) => resolve(null)));
const { getMessages } = result.current;
act(() => {
await waitFor(() => {
expect(getMessages('case')).toEqual([]);
});
});

it('should add a message', async () => {
const { result } = renderHook(() => useMessagesStorage());
await waitFor(() => new Promise((resolve) => resolve(null)));
const { getMessages, addMessage } = result.current;
act(() => {
addMessage('case', 'id-1');
expect(getMessages('case')).toEqual(['id-1']);
});

await waitFor(() => expect(getMessages('case')).toEqual(['id-1']));
});

it('should add multiple messages', async () => {
const { result } = renderHook(() => useMessagesStorage());
await waitFor(() => new Promise((resolve) => resolve(null)));
const { getMessages, addMessage } = result.current;

act(() => {
addMessage('case', 'id-1');
addMessage('case', 'id-2');
expect(getMessages('case')).toEqual(['id-1', 'id-2']);
});

await waitFor(() => expect(getMessages('case')).toEqual(['id-1', 'id-2']));
});

it('should remove a message', async () => {
const { result } = renderHook(() => useMessagesStorage());
await waitFor(() => new Promise((resolve) => resolve(null)));
const { getMessages, addMessage, removeMessage } = result.current;

await act(async () => {
addMessage('case', 'id-1');
addMessage('case', 'id-2');
removeMessage('case', 'id-2');
expect(getMessages('case')).toEqual(['id-1']);
});

await waitFor(() => expect(getMessages('case')).toEqual(['id-1']));
});

it('should return presence of a message', async () => {
const { result } = renderHook(() => useMessagesStorage());
await waitFor(() => new Promise((resolve) => resolve(null)));
const { hasMessage, addMessage, removeMessage } = result.current;
await act(async () => {
addMessage('case', 'id-1');
addMessage('case', 'id-2');
removeMessage('case', 'id-2');
});

await waitFor(() => {
expect(hasMessage('case', 'id-1')).toEqual(true);
expect(hasMessage('case', 'id-2')).toEqual(false);
});
});

it('should clear all messages', async () => {
const { result } = renderHook(() => useMessagesStorage());
await waitFor(() => new Promise((resolve) => resolve(null)));
const { getMessages, addMessage, clearAllMessages } = result.current;
await act(async () => {
addMessage('case', 'id-1');
addMessage('case', 'id-2');
clearAllMessages('case');
expect(getMessages('case')).toEqual([]);
});
await waitFor(() => expect(getMessages('case')).toEqual([]));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { IndexFieldSearch } from './use_data_view';
import { useDataView } from './use_data_view';
import { mocksSource } from './mock';
import { mockGlobalState, TestProviders } from '../../mock';
import { act, waitFor, renderHook } from '@testing-library/react';
import { act, renderHook } from '@testing-library/react';
import { useKibana } from '../../lib/kibana';

const mockDispatch = jest.fn();
Expand Down Expand Up @@ -87,8 +87,6 @@ describe('source/index.tsx', () => {
wrapper: TestProviders,
});

await waitFor(() => new Promise((resolve) => resolve(null)));

await act(async () => {
await result.current.indexFieldsSearch({ dataViewId: 'neato' });
});
Expand All @@ -108,8 +106,6 @@ describe('source/index.tsx', () => {
wrapper: TestProviders,
});

await waitFor(() => new Promise((resolve) => resolve(null)));

await act(async () => {
indexFieldsSearch = result.current.indexFieldsSearch;
});
Expand All @@ -135,7 +131,7 @@ describe('source/index.tsx', () => {
const { result } = renderHook(() => useDataView(), {
wrapper: TestProviders,
});
await waitFor(() => new Promise((resolve) => resolve(null)));

await act(async () => {
indexFieldsSearch = result.current.indexFieldsSearch;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ describe('useFullScreen', () => {
wrapper: WrapperContainer,
}));

await waitFor(() => new Promise((resolve) => resolve(null)));
act(() => {
result.current.setGlobalFullScreen(true);
});
Expand All @@ -78,7 +77,7 @@ describe('useFullScreen', () => {
({ result } = renderHook(() => useGlobalFullScreen(), {
wrapper: WrapperContainer,
}));
await waitFor(() => new Promise((resolve) => resolve(null)));

act(() => {
result.current.setGlobalFullScreen(false);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe('useUserInfo', () => {
const { result } = renderHook(() => useUserInfo(), {
wrapper: TestProviders,
});
await waitFor(() => new Promise((resolve) => resolve(null)));

expect(result.current).toEqual({
canUserCRUD: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('useQueryAlerts', () => {
initialProps: [mockAlertsQuery, indexName],
}
);
await waitFor(() => new Promise((resolve) => resolve(null)));

rerender([mockAlertsQuery, 'new-mock-index-name']);
await waitFor(() => expect(spyOnfetchRules).toHaveBeenCalledTimes(2));
});
Expand Down Expand Up @@ -116,8 +116,6 @@ describe('useQueryAlerts', () => {
const localProps = { ...defaultProps, skip: false };
const { rerender } = renderHook(() => useQueryAlerts<unknown, unknown>(localProps));

await waitFor(() => new Promise((resolve) => resolve(null)));

localProps.skip = true;
rerender();
rerender();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ describe('getUseCellActionsHook', () => {
}
);

await waitFor(() => new Promise((resolve) => resolve(null)));
await waitFor(() => {
const cellAction = result.current.getCellActions('host.name', 0)[0];

const cellAction = result.current.getCellActions('host.name', 0)[0];
renderCellAction(cellAction);

renderCellAction(cellAction);

expect(screen.getByTestId('dataGridColumnCellAction-action1')).toBeInTheDocument();
expect(screen.getByTestId('dataGridColumnCellAction-action1')).toBeInTheDocument();
});
});

it('should not render cell actions correctly for eventRendered view', async () => {
Expand Down

0 comments on commit a80bf74

Please sign in to comment.