Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Oct 14, 2021
1 parent e523622 commit ff402c3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/plugins/discover/public/__mocks__/index_patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};

0 comments on commit ff402c3

Please sign in to comment.