Skip to content

Commit

Permalink
combine waitFor with assertion where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
eokoneyo committed Nov 11, 2024
1 parent 2ae9d0c commit b6887e9
Show file tree
Hide file tree
Showing 83 changed files with 1,203 additions and 1,344 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ describe('useFindLists', () => {
act(() => {
result.current.start({ http: httpMock, pageIndex: 1, pageSize: 10 });
});
await waitFor(() => new Promise((resolve) => resolve(null)));

expect(Api.findLists).toHaveBeenCalledWith(
expect.objectContaining({ http: httpMock, pageIndex: 1, pageSize: 10 })
await waitFor(() =>
expect(Api.findLists).toHaveBeenCalledWith(
expect.objectContaining({ http: httpMock, pageIndex: 1, pageSize: 10 })
)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -447,32 +447,32 @@ describe('UnifiedFieldList useExistingFields', () => {
query: { query: 'test', language: 'kuery' },
});

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

expect(ExistingFieldsServiceApi.loadFieldExisting).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
fromDate: '2021-01-01',
toDate: '2022-01-01',
dslQuery: {
bool: {
filter: [
{
multi_match: {
lenient: true,
query: 'test',
type: 'best_fields',
await waitFor(() =>
expect(ExistingFieldsServiceApi.loadFieldExisting).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
fromDate: '2021-01-01',
toDate: '2022-01-01',
dslQuery: {
bool: {
filter: [
{
multi_match: {
lenient: true,
query: 'test',
type: 'best_fields',
},
},
},
],
must: [],
must_not: [],
should: [],
],
must: [],
must_not: [],
should: [],
},
},
},
dataView,
timeFieldName: dataView.timeFieldName,
})
dataView,
timeFieldName: dataView.timeFieldName,
})
)
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ describe('useUpdateUserProfile() hook', () => {
expect(result.current.isLoading).toBeTruthy();

updateDone.next(true); // Resolve the http.post promise
await waitFor(() => new Promise((resolve) => resolve(null)));

expect(result.current.isLoading).toBeFalsy();
await waitFor(() => expect(result.current.isLoading).toBeFalsy());
});

test('should show a success notification by default', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,25 @@ describe('useRootProfile', () => {

it('should return rootProfileLoading as false', async () => {
const { result } = render();
await waitFor(() => new Promise((resolve) => resolve(null)));
expect(result.current.rootProfileLoading).toBe(false);
expect((result.current as Record<string, unknown>).AppWrapper).toBeDefined();
await waitFor(() => {
expect(result.current.rootProfileLoading).toBe(false);
expect((result.current as Record<string, unknown>).AppWrapper).toBeDefined();
});
});

it('should return rootProfileLoading as true when solutionNavId changes', async () => {
const { result, rerender } = render();
await waitFor(() => new Promise((resolve) => resolve(null)));
expect(result.current.rootProfileLoading).toBe(false);
expect((result.current as Record<string, unknown>).AppWrapper).toBeDefined();
await waitFor(() => {
expect(result.current.rootProfileLoading).toBe(false);
expect((result.current as Record<string, unknown>).AppWrapper).toBeDefined();
});
act(() => mockSolutionNavId$.next('newSolutionNavId'));
rerender();
expect(result.current.rootProfileLoading).toBe(true);
expect((result.current as Record<string, unknown>).AppWrapper).toBeUndefined();
await waitFor(() => new Promise((resolve) => resolve(null)));
expect(result.current.rootProfileLoading).toBe(false);
expect((result.current as Record<string, unknown>).AppWrapper).toBeDefined();
await waitFor(() => {
expect(result.current.rootProfileLoading).toBe(false);
expect((result.current as Record<string, unknown>).AppWrapper).toBeDefined();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ describe('useEmbeddableFactory', () => {

expect(loading).toBe(true);

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

const [embeddable] = result.current;
expect(embeddable).toBeDefined();
await waitFor(() => {
const [embeddable] = result.current;
expect(embeddable).toBeDefined();
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,12 @@ describe('Test of <Doc /> helper / hook', () => {
mockSearchResult.complete();
});

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

expect(hook.result.current.slice(0, 2)).toEqual([
ElasticRequestState.Found,
buildDataTableRecord(record),
]);
await waitFor(() =>
expect(hook.result.current.slice(0, 2)).toEqual([
ElasticRequestState.Found,
buildDataTableRecord(record),
])
);
});

test('useEsDocSearch for text based languages', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@ describe('useFetchAnonymizationFields', () => {

await act(async () => {
renderHook(() => useFetchAnonymizationFields());
await waitFor(() => new Promise((resolve) => resolve(null)));
expect(http.fetch).toHaveBeenCalledWith(
'/api/security_ai_assistant/anonymization_fields/_find',
{
method: 'GET',
query: {
page: 1,
per_page: 1000,
},
version: API_VERSIONS.public.v1,
signal: undefined,
}
);
await waitFor(() => {
expect(http.fetch).toHaveBeenCalledWith(
'/api/security_ai_assistant/anonymization_fields/_find',
{
method: 'GET',
query: {
page: 1,
per_page: 1000,
},
version: API_VERSIONS.public.v1,
signal: undefined,
}
);

expect(http.fetch).toHaveBeenCalled();
expect(http.fetch).toHaveBeenCalled();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ describe('conversations api', () => {
const deleteProps = { http, toasts, id: 'test' } as unknown as DeleteConversationParams;

renderHook(() => deleteConversation(deleteProps));
await waitFor(() => new Promise((resolve) => resolve(null)));

expect(deleteProps.http.fetch).toHaveBeenCalledWith(
'/api/security_ai_assistant/current_user/conversations/test',
{
method: 'DELETE',
signal: undefined,
version: '2023-10-31',
}
);
expect(toasts.addError).not.toHaveBeenCalled();
await waitFor(() => {
expect(deleteProps.http.fetch).toHaveBeenCalledWith(
'/api/security_ai_assistant/current_user/conversations/test',
{
method: 'DELETE',
signal: undefined,
version: '2023-10-31',
}
);
expect(toasts.addError).not.toHaveBeenCalled();
});
});
});

Expand All @@ -60,17 +60,17 @@ describe('conversations api', () => {
await act(async () => {
const getProps = { http, toasts, id: 'test' } as unknown as GetConversationByIdParams;
renderHook(() => getConversationById(getProps));
await waitFor(() => new Promise((resolve) => resolve(null)));

expect(getProps.http.fetch).toHaveBeenCalledWith(
'/api/security_ai_assistant/current_user/conversations/test',
{
method: 'GET',
signal: undefined,
version: '2023-10-31',
}
);
expect(toasts.addError).not.toHaveBeenCalled();
await waitFor(() => {
expect(getProps.http.fetch).toHaveBeenCalledWith(
'/api/security_ai_assistant/current_user/conversations/test',
{
method: 'GET',
signal: undefined,
version: '2023-10-31',
}
);
expect(toasts.addError).not.toHaveBeenCalled();
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@ describe('useFetchCurrentUserConversations', () => {

await act(async () => {
renderHook(() => useFetchCurrentUserConversations(defaultProps));
await waitFor(() => new Promise((resolve) => resolve(null)));
expect(defaultProps.http.fetch).toHaveBeenCalledWith(
'/api/security_ai_assistant/current_user/conversations/_find',
{
method: 'GET',
query: {
page: 1,
per_page: 99,
},
version: '2023-10-31',
signal: undefined,
}
);
await waitFor(() => {
expect(defaultProps.http.fetch).toHaveBeenCalledWith(
'/api/security_ai_assistant/current_user/conversations/_find',
{
method: 'GET',
query: {
page: 1,
per_page: 99,
},
version: '2023-10-31',
signal: undefined,
}
);

expect(onFetch).toHaveBeenCalled();
expect(onFetch).toHaveBeenCalled();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { renderHook } from '@testing-library/react-hooks';
import { act, waitFor } from '@testing-library/react';
import { waitFor } from '@testing-library/react';
import { usePerformEvaluation, UsePerformEvaluationParams } from './use_perform_evaluation';
import { postEvaluation as _postEvaluation } from './evaluate';
import { useMutation as _useMutation } from '@tanstack/react-query';
Expand Down Expand Up @@ -51,10 +51,8 @@ describe('usePerformEvaluation', () => {
jest.clearAllMocks();
});
it('should call api with undefined evalParams', async () => {
await act(async () => {
renderHook(() => usePerformEvaluation(defaultProps));
await waitFor(() => new Promise((resolve) => resolve(null)));

renderHook(() => usePerformEvaluation(defaultProps));
await waitFor(() => {
expect(defaultProps.http.post).toHaveBeenCalledWith('/internal/elastic_assistant/evaluate', {
body: undefined,
headers: {
Expand All @@ -81,37 +79,29 @@ describe('usePerformEvaluation', () => {
opts.onError(e);
}
});
await act(async () => {
renderHook(() => usePerformEvaluation(defaultProps));
await waitFor(() => new Promise((resolve) => resolve(null)));

renderHook(() => usePerformEvaluation(defaultProps));
await waitFor(() =>
expect(defaultProps.http.post).toHaveBeenCalledWith('/internal/elastic_assistant/evaluate', {
body: '{"graphs":["d","c"],"datasetName":"kewl","connectorIds":["h","g"],"runName":"test run"}',
headers: {
'Content-Type': 'application/json',
},
signal: undefined,
version: API_VERSIONS.internal.v1,
});
});
})
);
});

it('should return evaluation response', async () => {
await act(async () => {
const { result } = renderHook(() => usePerformEvaluation(defaultProps));
await waitFor(() => new Promise((resolve) => resolve(null)));

await expect(result.current).resolves.toStrictEqual(statusResponse);
});
const { result } = renderHook(() => usePerformEvaluation(defaultProps));
await waitFor(() => expect(result.current).resolves.toStrictEqual(statusResponse));
});

it('should display error toast when api throws error', async () => {
postEvaluationMock.mockRejectedValue(new Error('this is an error'));
await act(async () => {
renderHook(() => usePerformEvaluation(defaultProps));
await waitFor(() => new Promise((resolve) => resolve(null)));

expect(toasts.addError).toHaveBeenCalled();
});
renderHook(() => usePerformEvaluation(defaultProps));
await waitFor(() => expect(toasts.addError).toHaveBeenCalled());
});
});
Loading

0 comments on commit b6887e9

Please sign in to comment.