Files
001code-html--cocos/scratch-gui/test/helpers/intl-helpers.jsx
刘宇飞 6e0a1fbcbb Initial commit of 001code-html Scratch frontend project.
Includes scratch-gui, scratch-vm, scratch-blocks, scratch-render, scratch-l10n, and deployment config.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-16 15:37:45 +08:00

40 lines
1.2 KiB
JavaScript

/*
* Helpers for using enzyme and react-test-renderer with react-intl
* Directly from https://github.com/yahoo/react-intl/wiki/Testing-with-React-Intl
*/
import React from 'react';
import renderer from 'react-test-renderer';
import {IntlProvider, intlShape} from 'react-intl';
import {mount, shallow} from 'enzyme';
const intlProvider = new IntlProvider({locale: 'en'}, {});
const {intl} = intlProvider.getChildContext();
const nodeWithIntlProp = node => React.cloneElement(node, {intl});
const shallowWithIntl = (node, {context} = {}) => shallow(
nodeWithIntlProp(node),
{
context: Object.assign({}, context, {intl})
}
);
const mountWithIntl = (node, {context, childContextTypes} = {}) => mount(
nodeWithIntlProp(node),
{
context: Object.assign({}, context, {intl}),
childContextTypes: Object.assign({}, {intl: intlShape}, childContextTypes)
}
);
// react-test-renderer component for use with snapshot testing
const componentWithIntl = (children, props = {locale: 'en'}) => renderer.create(
<IntlProvider {...props}>{children}</IntlProvider>
);
export {
componentWithIntl,
shallowWithIntl,
mountWithIntl
};