-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add jest tests, fix types, run initial unsaved changes check in initi…
…alizer
- Loading branch information
1 parent
0647056
commit 51794d1
Showing
13 changed files
with
534 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/plugins/embeddable/public/react_embeddable_system/react_embeddable_api.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { render, waitFor } from '@testing-library/react'; | ||
import { getMockPresentationContainer } from '@kbn/presentation-containers/mocks'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import React from 'react'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { useReactEmbeddableApiHandle, ReactEmbeddableParentContext } from './react_embeddable_api'; | ||
import { DefaultEmbeddableApi } from './types'; | ||
|
||
describe('react embeddable api', () => { | ||
const defaultApi = { | ||
unsavedChanges: new BehaviorSubject<object | undefined>(undefined), | ||
resetUnsavedChanges: jest.fn(), | ||
serializeState: jest.fn().mockReturnValue({ bork: 'borkbork' }), | ||
}; | ||
|
||
const parentApi = getMockPresentationContainer(); | ||
|
||
const TestComponent = React.forwardRef<DefaultEmbeddableApi>((_, ref) => { | ||
useReactEmbeddableApiHandle(defaultApi, ref, '123'); | ||
return <div />; | ||
}); | ||
|
||
it('returns the given API', () => { | ||
const { result } = renderHook(() => | ||
useReactEmbeddableApiHandle<DefaultEmbeddableApi & { bork: () => 'bork' }>( | ||
{ | ||
...defaultApi, | ||
bork: jest.fn().mockReturnValue('bork'), | ||
}, | ||
{} as any, | ||
'superBork' | ||
) | ||
); | ||
|
||
expect(result.current.thisApi.bork()).toEqual('bork'); | ||
expect(result.current.thisApi.serializeState()).toEqual({ bork: 'borkbork' }); | ||
}); | ||
|
||
it('publishes the API into the provided ref', async () => { | ||
const ref = React.createRef<DefaultEmbeddableApi>(); | ||
renderHook(() => useReactEmbeddableApiHandle(defaultApi, ref, '123')); | ||
await waitFor(() => expect(ref.current).toBeDefined()); | ||
expect(ref.current?.serializeState); | ||
expect(ref.current?.serializeState()).toEqual({ bork: 'borkbork' }); | ||
}); | ||
|
||
it('publishes the API into an imperative handle', async () => { | ||
const ref = React.createRef<DefaultEmbeddableApi>(); | ||
render(<TestComponent ref={ref} />); | ||
await waitFor(() => expect(ref.current).toBeDefined()); | ||
expect(ref.current?.serializeState); | ||
expect(ref.current?.serializeState()).toEqual({ bork: 'borkbork' }); | ||
}); | ||
|
||
it('returns an API with a parent when rendered inside a parent context', async () => { | ||
const ref = React.createRef<DefaultEmbeddableApi>(); | ||
render( | ||
<ReactEmbeddableParentContext.Provider value={{ parentApi }}> | ||
<TestComponent ref={ref} /> | ||
</ReactEmbeddableParentContext.Provider> | ||
); | ||
await waitFor(() => expect(ref.current).toBeDefined()); | ||
expect(ref.current?.serializeState); | ||
expect(ref.current?.serializeState()).toEqual({ bork: 'borkbork' }); | ||
|
||
expect(ref.current?.parentApi?.getLastSavedStateForChild).toBeDefined(); | ||
expect(ref.current?.parentApi?.registerPanelApi).toBeDefined(); | ||
}); | ||
|
||
it('calls registerPanelApi on its parent', async () => { | ||
const ref = React.createRef<DefaultEmbeddableApi>(); | ||
render( | ||
<ReactEmbeddableParentContext.Provider value={{ parentApi }}> | ||
<TestComponent ref={ref} /> | ||
</ReactEmbeddableParentContext.Provider> | ||
); | ||
expect(parentApi?.registerPanelApi).toHaveBeenCalledWith('123', expect.any(Object)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
src/plugins/embeddable/public/react_embeddable_system/react_embeddable_parenting.ts
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
src/plugins/embeddable/public/react_embeddable_system/react_embeddable_registry.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { | ||
registerReactEmbeddableFactory, | ||
reactEmbeddableRegistryHasKey, | ||
getReactEmbeddableFactory, | ||
} from './react_embeddable_registry'; | ||
import { ReactEmbeddableFactory } from './types'; | ||
|
||
describe('react embeddable registry', () => { | ||
const testEmbeddableFactory: ReactEmbeddableFactory = { | ||
deserializeState: jest.fn(), | ||
getComponent: jest.fn(), | ||
}; | ||
|
||
it('throws an error if requested embeddable factory type is not registered', () => { | ||
expect(() => getReactEmbeddableFactory('notRegistered')).toThrowErrorMatchingInlineSnapshot( | ||
`"No embeddable factory found for type: notRegistered"` | ||
); | ||
}); | ||
|
||
it('can register and get an embeddable factory', () => { | ||
registerReactEmbeddableFactory('test', testEmbeddableFactory); | ||
expect(getReactEmbeddableFactory('test')).toBe(testEmbeddableFactory); | ||
}); | ||
|
||
it('can check if a factory is registered', () => { | ||
registerReactEmbeddableFactory('test', testEmbeddableFactory); | ||
expect(reactEmbeddableRegistryHasKey('test')).toBe(true); | ||
expect(reactEmbeddableRegistryHasKey('notRegistered')).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/plugins/embeddable/public/react_embeddable_system/react_embeddable_renderer.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
import { render, waitFor, screen } from '@testing-library/react'; | ||
|
||
import React from 'react'; | ||
import { registerReactEmbeddableFactory } from './react_embeddable_registry'; | ||
import { ReactEmbeddableRenderer } from './react_embeddable_renderer'; | ||
import { ReactEmbeddableFactory } from './types'; | ||
|
||
describe('react embeddable renderer', () => { | ||
const testEmbeddableFactory: ReactEmbeddableFactory<{ name: string; bork: string }> = { | ||
deserializeState: jest.fn(), | ||
getComponent: jest.fn().mockResolvedValue(() => { | ||
return <div>SUPER TEST COMPONENT</div>; | ||
}), | ||
}; | ||
|
||
it('deserializes given state', () => { | ||
registerReactEmbeddableFactory('test', testEmbeddableFactory); | ||
render(<ReactEmbeddableRenderer type={'test'} state={{ rawState: { blorp: 'blorp?' } }} />); | ||
expect(testEmbeddableFactory.deserializeState).toHaveBeenCalledWith({ | ||
rawState: { blorp: 'blorp?' }, | ||
}); | ||
}); | ||
|
||
it('renders the given component once it resolves', () => { | ||
registerReactEmbeddableFactory('test', testEmbeddableFactory); | ||
render(<ReactEmbeddableRenderer type={'test'} state={{ rawState: { blorp: 'blorp?' } }} />); | ||
waitFor(() => { | ||
expect(screen.findByText('SUPER TEST COMPONENT')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
69 changes: 69 additions & 0 deletions
69
src/plugins/embeddable/public/react_embeddable_system/react_embeddable_titles.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { | ||
initializeReactEmbeddableTitles, | ||
SerializedReactEmbeddableTitles, | ||
} from './react_embeddable_titles'; | ||
|
||
describe('react embeddable titles', () => { | ||
const rawState: SerializedReactEmbeddableTitles = { | ||
title: 'very cool title', | ||
description: 'less cool description', | ||
hidePanelTitles: false, | ||
}; | ||
|
||
it('should initialize publishing subjects with the provided rawState', () => { | ||
const { titlesApi } = initializeReactEmbeddableTitles(rawState); | ||
expect(titlesApi.panelTitle.value).toBe(rawState.title); | ||
expect(titlesApi.panelDescription.value).toBe(rawState.description); | ||
expect(titlesApi.hidePanelTitle.value).toBe(rawState.hidePanelTitles); | ||
}); | ||
|
||
it('should update publishing subject values when set functions are called', () => { | ||
const { titlesApi } = initializeReactEmbeddableTitles(rawState); | ||
|
||
titlesApi.setPanelTitle('even cooler title'); | ||
titlesApi.setPanelDescription('super uncool description'); | ||
titlesApi.setHidePanelTitle(true); | ||
|
||
expect(titlesApi.panelTitle.value).toEqual('even cooler title'); | ||
expect(titlesApi.panelDescription.value).toEqual('super uncool description'); | ||
expect(titlesApi.hidePanelTitle.value).toBe(true); | ||
}); | ||
|
||
it('should correctly serialize current state', () => { | ||
const { serializeTitles, titlesApi } = initializeReactEmbeddableTitles(rawState); | ||
titlesApi.setPanelTitle('UH OH, A TITLE'); | ||
|
||
const serializedTitles = serializeTitles(); | ||
expect(serializedTitles).toMatchInlineSnapshot(` | ||
Object { | ||
"description": "less cool description", | ||
"hidePanelTitles": false, | ||
"title": "UH OH, A TITLE", | ||
} | ||
`); | ||
}); | ||
|
||
it('should return the correct set of comparators', () => { | ||
const { titleComparators } = initializeReactEmbeddableTitles(rawState); | ||
|
||
expect(titleComparators.title).toBeDefined(); | ||
expect(titleComparators.description).toBeDefined(); | ||
expect(titleComparators.hidePanelTitles).toBeDefined(); | ||
}); | ||
|
||
it('should correctly compare hidePanelTitles with custom comparator', () => { | ||
const { titleComparators } = initializeReactEmbeddableTitles(rawState); | ||
|
||
expect(titleComparators.hidePanelTitles![2]!(true, false)).toBe(false); | ||
expect(titleComparators.hidePanelTitles![2]!(undefined, false)).toBe(true); | ||
expect(titleComparators.hidePanelTitles![2]!(true, undefined)).toBe(false); | ||
}); | ||
}); |
Oops, something went wrong.