-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e4a575
commit 92d1180
Showing
5 changed files
with
341 additions
and
28 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...ic/applications/app_search/components/documents/search_experience/__mocks__/hooks.mock.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,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
jest.mock('../hooks', () => ({ | ||
useSearchContextActions: jest.fn(() => ({})), | ||
useSearchContextState: jest.fn(() => ({})), | ||
})); | ||
|
||
import { useSearchContextState, useSearchContextActions } from '../hooks'; | ||
|
||
export const setMockSearchContextState = (values: object) => { | ||
(useSearchContextState as jest.Mock).mockImplementation(() => values); | ||
}; | ||
export const setMockSearchContextActions = (actions: object) => { | ||
(useSearchContextActions as jest.Mock).mockImplementation(() => actions); | ||
}; |
84 changes: 84 additions & 0 deletions
84
...arch/public/applications/app_search/components/documents/search_experience/hooks.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,84 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
const mockAction = jest.fn(); | ||
|
||
let mockSubcription: (state: object) => void; | ||
const mockDriver = { | ||
state: { foo: 'foo' }, | ||
actions: { bar: mockAction }, | ||
subscribeToStateChanges: jest.fn().mockImplementation((fn) => { | ||
mockSubcription = fn; | ||
}), | ||
unsubscribeToStateChanges: jest.fn(), | ||
}; | ||
|
||
jest.mock('react', () => ({ | ||
...(jest.requireActual('react') as object), | ||
useContext: jest.fn(() => ({ | ||
driver: mockDriver, | ||
})), | ||
})); | ||
|
||
import React from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { mount } from 'enzyme'; | ||
|
||
import { useSearchContextState, useSearchContextActions } from './hooks'; | ||
|
||
describe('hooks', () => { | ||
describe('useSearchContextState', () => { | ||
it('exposes search state', () => { | ||
const TestComponent = () => { | ||
const { foo } = useSearchContextState(); | ||
return <div>{foo}</div>; | ||
}; | ||
|
||
const wrapper = mount(<TestComponent />); | ||
expect(wrapper.text()).toEqual('foo'); | ||
}); | ||
|
||
it('subscribes to state changes', () => { | ||
const TestComponent = () => { | ||
const { foo } = useSearchContextState(); | ||
return <div>{foo}</div>; | ||
}; | ||
|
||
const wrapper = mount(<TestComponent />); | ||
|
||
act(() => { | ||
mockSubcription({ foo: 'bar' }); | ||
}); | ||
|
||
expect(wrapper.text()).toEqual('bar'); | ||
}); | ||
|
||
it('unsubscribes to state changes when unmounted', () => { | ||
const TestComponent = () => { | ||
const { foo } = useSearchContextState(); | ||
return <div>{foo}</div>; | ||
}; | ||
|
||
const wrapper = mount(<TestComponent />); | ||
|
||
wrapper.unmount(); | ||
|
||
expect(mockDriver.unsubscribeToStateChanges).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('useSearchContextActions', () => { | ||
it('exposes actions', () => { | ||
const TestComponent = () => { | ||
const { bar } = useSearchContextActions(); | ||
bar(); | ||
return null; | ||
}; | ||
|
||
mount(<TestComponent />); | ||
expect(mockAction).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
...ise_search/public/applications/app_search/components/documents/search_experience/hooks.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,31 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { useContext, useEffect, useState } from 'react'; | ||
|
||
// @ts-expect-error types are not available for this package yet | ||
import { SearchContext } from '@elastic/react-search-ui'; | ||
|
||
export const useSearchContextState = () => { | ||
const { driver } = useContext(SearchContext); | ||
const [state, setState] = useState(driver.state); | ||
|
||
useEffect(() => { | ||
driver.subscribeToStateChanges((newState: object) => { | ||
setState(newState); | ||
}); | ||
return () => { | ||
driver.unsubscribeToStateChanges(); | ||
}; | ||
}, [state]); | ||
|
||
return state; | ||
}; | ||
|
||
export const useSearchContextActions = () => { | ||
const { driver } = useContext(SearchContext); | ||
return driver.actions; | ||
}; |
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
Oops, something went wrong.