forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] Pull out various Kibana context mocks into separate files
- I'm creating a reusable useContext mock for shallow()ed enzyme components + add more documentation comments + examples
- Loading branch information
Showing
8 changed files
with
96 additions
and
28 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
25 changes: 0 additions & 25 deletions
25
x-pack/plugins/enterprise_search/public/applications/test_utils/helpers.tsx
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
x-pack/plugins/enterprise_search/public/applications/test_utils/index.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,10 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { mockKibanaContext } from './mock_kibana_context'; | ||
export { mountWithKibanaContext } from './mount_with_context'; | ||
|
||
// Note: mock_shallow_usecontext must be imported directly as a file |
17 changes: 17 additions & 0 deletions
17
x-pack/plugins/enterprise_search/public/applications/test_utils/mock_kibana_context.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,17 @@ | ||
/* | ||
* 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 { httpServiceMock } from '../../../../../../src/core/public/mocks'; | ||
|
||
/** | ||
* A set of default Kibana context values to use across component tests. | ||
* @see enterprise_search/public/index.tsx for the KibanaContext definition/import | ||
*/ | ||
export const mockKibanaContext = { | ||
http: httpServiceMock.createSetupContract(), | ||
setBreadcrumbs: jest.fn(), | ||
enterpriseSearchUrl: 'http://localhost:3002', | ||
}; |
39 changes: 39 additions & 0 deletions
39
x-pack/plugins/enterprise_search/public/applications/test_utils/mock_shallow_usecontext.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,39 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* NOTE: This variable name MUST start with 'mock*' in order for | ||
* Jest to accept its use within a jest.mock() | ||
*/ | ||
import { mockKibanaContext } from './mock_kibana_context'; | ||
|
||
jest.mock('react', () => ({ | ||
...jest.requireActual('react'), | ||
useContext: jest.fn(() => mockKibanaContext), | ||
})); | ||
|
||
/** | ||
* Example usage within a component test using shallow(): | ||
* | ||
* import '../../../test_utils/mock_shallow_usecontext'; // Must come before React's import, adjust relative path as needed | ||
* | ||
* import React from 'react'; | ||
* import { shallow } from 'enzyme'; | ||
* | ||
* // ... etc. | ||
*/ | ||
|
||
/** | ||
* If you need to override the default mock context values, you can do so via jest.mockImplementation: | ||
* | ||
* import React, { useContext } from 'react'; | ||
* | ||
* // ... etc. | ||
* | ||
* it('some test', () => { | ||
* useContext.mockImplementationOnce(() => ({ enterpriseSearchUrl: 'someOverride' })); | ||
* }); | ||
*/ |
27 changes: 27 additions & 0 deletions
27
x-pack/plugins/enterprise_search/public/applications/test_utils/mount_with_context.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,27 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { mount } from 'enzyme'; | ||
|
||
import { KibanaContext } from '../'; | ||
import { mockKibanaContext } from './mock_kibana_context'; | ||
|
||
/** | ||
* This helper mounts a component with a set of default KibanaContext, | ||
* while also allowing custom context to be passed in via a second arg | ||
* | ||
* Example usage: | ||
* | ||
* const wrapper = mountWithKibanaContext(<Component />, { enterpriseSearchUrl: 'someOverride' }); | ||
*/ | ||
export const mountWithKibanaContext = (node, contextProps) => { | ||
return mount( | ||
<KibanaContext.Provider value={{ ...mockKibanaContext, ...contextProps }}> | ||
{node} | ||
</KibanaContext.Provider> | ||
); | ||
}; |