From ff402c367baad9aba8d9c5f0688dbb9e61a666b7 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Thu, 14 Oct 2021 17:26:01 +0200 Subject: [PATCH] Fix test --- .../discover/public/__mocks__/index_patterns.ts | 8 ++++---- .../__snapshots__/source_viewer.test.tsx.snap | 6 +++++- .../application/helpers/use_index_pattern.test.tsx | 13 ++++++++----- .../application/helpers/use_index_pattern.tsx | 6 +++--- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/plugins/discover/public/__mocks__/index_patterns.ts b/src/plugins/discover/public/__mocks__/index_patterns.ts index 4f7dffcf7c4d7..b90338e895623 100644 --- a/src/plugins/discover/public/__mocks__/index_patterns.ts +++ b/src/plugins/discover/public/__mocks__/index_patterns.ts @@ -10,14 +10,14 @@ import { IndexPatternsService } from '../../../data/common'; import { indexPatternMock } from './index_pattern'; export const indexPatternsMock = { - getCache: () => { + getCache: async () => { return [indexPatternMock]; }, - get: (id: string) => { + get: async (id: string) => { if (id === 'the-index-pattern-id') { - return indexPatternMock; + return Promise.resolve(indexPatternMock); } else if (id === 'invalid-index-pattern-id') { - return Promise.reject(new Error('Invalid index pattern id')); + return Promise.reject('Invald'); } }, updateSavedObject: jest.fn(), diff --git a/src/plugins/discover/public/application/components/source_viewer/__snapshots__/source_viewer.test.tsx.snap b/src/plugins/discover/public/application/components/source_viewer/__snapshots__/source_viewer.test.tsx.snap index 9258c4024149b..bdb19066fea15 100644 --- a/src/plugins/discover/public/application/components/source_viewer/__snapshots__/source_viewer.test.tsx.snap +++ b/src/plugins/discover/public/application/components/source_viewer/__snapshots__/source_viewer.test.tsx.snap @@ -619,7 +619,11 @@ exports[`Source Viewer component renders loading state 1`] = ` hasLineNumbers={true} id="1" index="index1" - indexPatternId="xyz" + indexPattern={ + Object { + "getComputedFields": [Function], + } + } intl={ Object { "defaultFormats": Object {}, diff --git a/src/plugins/discover/public/application/helpers/use_index_pattern.test.tsx b/src/plugins/discover/public/application/helpers/use_index_pattern.test.tsx index 775531bd75c16..dfc54d8630742 100644 --- a/src/plugins/discover/public/application/helpers/use_index_pattern.test.tsx +++ b/src/plugins/discover/public/application/helpers/use_index_pattern.test.tsx @@ -8,20 +8,23 @@ import { useIndexPattern } from './use_index_pattern'; import { indexPatternMock } from '../../__mocks__/index_pattern'; import { indexPatternsMock } from '../../__mocks__/index_patterns'; -import { renderHook, act } from '@testing-library/react-hooks'; +import { renderHook } from '@testing-library/react-hooks'; describe('Use Index Pattern', () => { test('returning a valid index pattern', async () => { - const { result } = renderHook(() => useIndexPattern(indexPatternsMock, 'the-index-pattern-id')); - await act(() => Promise.resolve()); + const { result, waitForNextUpdate } = renderHook(() => + useIndexPattern(indexPatternsMock, 'the-index-pattern-id') + ); + await waitForNextUpdate(); expect(result.current.indexPattern).toBe(indexPatternMock); + expect(result.current.error).toBe(undefined); }); test('returning an error', async () => { - const { result } = renderHook(() => + const { result, waitForNextUpdate } = renderHook(() => useIndexPattern(indexPatternsMock, 'invalid-index-pattern-id') ); - await act(() => Promise.resolve()); + await waitForNextUpdate(); expect(result.current.indexPattern).toBe(undefined); expect(result.current.error).toBeTruthy(); }); diff --git a/src/plugins/discover/public/application/helpers/use_index_pattern.tsx b/src/plugins/discover/public/application/helpers/use_index_pattern.tsx index dc99a419dd579..374f83cbbfe72 100644 --- a/src/plugins/discover/public/application/helpers/use_index_pattern.tsx +++ b/src/plugins/discover/public/application/helpers/use_index_pattern.tsx @@ -15,13 +15,13 @@ export const useIndexPattern = (indexPatterns: IndexPatternsContract, indexPatte useEffect(() => { async function loadIndexPattern() { try { - const ip = await indexPatterns.get(indexPatternId); - setIndexPattern(ip); + const item = await indexPatterns.get(indexPatternId); + setIndexPattern(item); } catch (e) { setError(e); } } loadIndexPattern(); - }); + }, [indexPatternId, indexPatterns]); return { indexPattern, error }; };