-
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.
🔥 Delete/repurpose mount with context helpers
- Remove mountWithKibanaContext completely - Change mountWithContext to mountWithIntl, since that's the only context remaining, and move it to its own file - Change mountWithAsyncContext to just mountAsync and move it to its own file + add an option to pair it w/ mountWithIntl for formatted date/number support
- Loading branch information
Showing
7 changed files
with
96 additions
and
124 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
46 changes: 46 additions & 0 deletions
46
x-pack/plugins/enterprise_search/public/applications/__mocks__/mount_async.mock.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,46 @@ | ||
/* | ||
* 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 { act } from 'react-dom/test-utils'; | ||
import { mount, ReactWrapper } from 'enzyme'; | ||
|
||
import { mountWithIntl } from './'; | ||
|
||
/** | ||
* This helper is intended for components that have async effects | ||
* (e.g. http fetches) on mount. It mostly adds act/update boilerplate | ||
* that's needed for the wrapper to play nice with Enzyme/Jest | ||
* | ||
* Example usage: | ||
* | ||
* const wrapper = mountAsync(<Component />); | ||
*/ | ||
|
||
interface IOptions { | ||
i18n?: boolean; | ||
} | ||
|
||
export const mountAsync = async ( | ||
children: React.ReactElement, | ||
options: IOptions | ||
): Promise<ReactWrapper> => { | ||
let wrapper: ReactWrapper | undefined; | ||
|
||
// We get a lot of act() warning/errors in the terminal without this. | ||
// TBH, I don't fully understand why since Enzyme's mount is supposed to | ||
// have act() baked in - could be because of the wrapping context provider? | ||
await act(async () => { | ||
wrapper = options.i18n ? mountWithIntl(children) : mount(children); | ||
}); | ||
if (wrapper) { | ||
wrapper.update(); // This seems to be required for the DOM to actually update | ||
|
||
return wrapper; | ||
} else { | ||
throw new Error('Could not mount wrapper'); | ||
} | ||
}; |
83 changes: 0 additions & 83 deletions
83
x-pack/plugins/enterprise_search/public/applications/__mocks__/mount_with_context.mock.tsx
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/enterprise_search/public/applications/__mocks__/mount_with_i18n.mock.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,21 @@ | ||
/* | ||
* 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 { I18nProvider } from '@kbn/i18n/react'; | ||
|
||
/** | ||
* This helper wraps a component with react-intl's <I18nProvider> which | ||
* fixes "Could not find required `intl` object" console errors when running tests | ||
* | ||
* Example usage (should be the same as mount()): | ||
* | ||
* const wrapper = mountWithI18n(<Component />); | ||
*/ | ||
export const mountWithIntl = (children: React.ReactElement) => { | ||
return mount(<I18nProvider>{children}</I18nProvider>); | ||
}; |
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