-
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.
[Enterprise Search] Mocks/tests tech debt - avoid hungry mocking (#10…
…1107) * Move enzyme & misc test helpers out of __mocks__/ and into new test_helpers/ They're not technically mocks since nothing is being mocked, so we should move them into a test_helpers folder for specificity & organization * Move React Router mocks into its own separate folder/import This was part of the initial feedback, that it was unclear why importing something for Kea in __mocks__/index.ts was mocking react router along for the ride. Separating this out makes things clearer and imports more explicit + add some handy new mock useX jest.fn()s helpers, so we're not doing `useParams() as jest.Mock` errywhere * Move Kea & logic mocks/helpers into kea_logic subfolder - for organization NOTE: It can't be a plain kea/ folder because then Jest automatically mocks the `kea` module itself kea 🤦 * Fix type failures - Caused by switch from any to unknown (changed back to any + added a .test_helper suffix exclusion for any) * Fix Enterprise Search tests/imports - I checked all application folders but this one, whoops * PR feedback: comment copy * Update tests/files added since PR open with new import locations * Fix misc react router typing - null not being type-able as a boolean - forgot to remove various useParam imports after adding mockUseParams + misc unused kea import, probably added while debugging kea mocks
- Loading branch information
Constance
authored
Jun 9, 2021
1 parent
c225aaa
commit 12986fb
Showing
279 changed files
with
603 additions
and
448 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
65 changes: 65 additions & 0 deletions
65
x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/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,65 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
/** | ||
* Combine all shared mock values/actions into a single obj | ||
* | ||
* NOTE: These variable names MUST start with 'mock*' in order for | ||
* Jest to accept its use within a jest.mock() | ||
*/ | ||
import { mockFlashMessagesValues, mockFlashMessagesActions } from './flash_messages_logic.mock'; | ||
import { mockHttpValues } from './http_logic.mock'; | ||
import { mockKibanaValues } from './kibana_logic.mock'; | ||
import { mockLicensingValues } from './licensing_logic.mock'; | ||
import { mockTelemetryActions } from './telemetry_logic.mock'; | ||
|
||
export const mockAllValues = { | ||
...mockKibanaValues, | ||
...mockLicensingValues, | ||
...mockHttpValues, | ||
...mockFlashMessagesValues, | ||
}; | ||
export const mockAllActions = { | ||
...mockTelemetryActions, | ||
...mockFlashMessagesActions, | ||
}; | ||
|
||
/** | ||
* Import this file directly to mock useValues with a set of default values for all shared logic files. | ||
* Example usage: | ||
* | ||
* import '../../../__mocks__/kea_logic'; // Must come before kea's import, adjust relative path as needed | ||
*/ | ||
jest.mock('kea', () => ({ | ||
...(jest.requireActual('kea') as object), | ||
useValues: jest.fn(() => ({ ...mockAllValues })), | ||
useActions: jest.fn(() => ({ ...mockAllActions })), | ||
})); | ||
|
||
/** | ||
* React component helpers | ||
* | ||
* Call this function to override a specific set of Kea values while retaining all other defaults | ||
* | ||
* Example usage: | ||
* | ||
* import { setMockValues } from '../../../__mocks__/kea_logic'; | ||
* import { SomeComponent } from './'; | ||
* | ||
* it('some test', () => { | ||
* setMockValues({ someValue: 'hello' }); | ||
* shallow(<SomeComponent />); | ||
* }); | ||
*/ | ||
import { useValues, useActions } from 'kea'; | ||
|
||
export const setMockValues = (values: object) => { | ||
(useValues as jest.Mock).mockImplementation(() => ({ ...mockAllValues, ...values })); | ||
}; | ||
export const setMockActions = (actions: object) => { | ||
(useActions as jest.Mock).mockImplementation(() => ({ ...mockAllActions, ...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
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
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
13 changes: 13 additions & 0 deletions
13
x-pack/plugins/enterprise_search/public/applications/__mocks__/react_router/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,13 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
// State and hooks are stored in separate mock files for when a file needs to | ||
// import a basic history mock without automatically jest.mock()ing all of React Router | ||
export * from './state.mock'; | ||
export * from './hooks.mock'; | ||
|
||
// For example usage, @see public/applications/shared/react_router_helpers/eui_link.test.tsx |
28 changes: 28 additions & 0 deletions
28
x-pack/plugins/enterprise_search/public/applications/__mocks__/react_router/state.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,28 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
/** | ||
* NOTE: These variable names MUST start with 'mock*' in order for | ||
* Jest to accept its use within a jest.mock() | ||
*/ | ||
|
||
export const mockHistory = { | ||
createHref: jest.fn(({ pathname }) => `/app/enterprise_search${pathname}`), | ||
push: jest.fn(), | ||
location: { | ||
pathname: '/current-path', | ||
}, | ||
listen: jest.fn(() => jest.fn()), | ||
} as any; | ||
|
||
export const mockLocation = { | ||
key: 'someKey', | ||
pathname: '/current-path', | ||
search: '?query=something', | ||
hash: '#hash', | ||
state: {}, | ||
}; |
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
Oops, something went wrong.