How to use react-intl - 10 common examples

To help you get started, we’ve selected a few react-intl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github LN-Zap / zap-desktop / test / unit / __helpers__ / intl-enzyme-test-helper.js View on Github external
// See https://github.com/yahoo/react-intl/wiki/Testing-with-React-Intl#enzyme

/**
 * Components using the react-intl module require access to the intl context.
 * This is not available when mounting single components in Enzyme.
 * These helper functions aim to address that and wrap a valid,
 * English-locale intl context around them.
 */

import React from 'react'
import { IntlProvider } from 'react-intl'
import { mount, shallow } from 'enzyme'
import messages from '@zap/translations/en.json'
import { intlShape } from '@zap/i18n'
// Create the IntlProvider to retrieve context for wrapping around.
const intlProvider = new IntlProvider({ locale: 'en', messages }, {})
const { intl } = intlProvider.getChildContext()

// nodeWithIntlProp - When using React-Intl `injectIntl` on components, props.intl is required.
const nodeWithIntlProp = node => {
  return React.cloneElement(node, { intl })
}

export const shallowWithIntl = (node, { context, ...additionalOptions } = {}) => {
  return shallow(nodeWithIntlProp(node), {
    context: Object.assign({}, context, { intl }),
    ...additionalOptions,
  })
}

export const mountWithIntl = (node, { context, childContextTypes, ...additionalOptions } = {}) => {
  return mount(nodeWithIntlProp(node), {
github quran / quran.com-frontend / tests / helpers / intl-enzyme-test-helper.js View on Github external
* Based on: https://gist.github.com/mirague/c05f4da0d781a9b339b501f1d5d33c37/
 *
 * Components using the react-intl module require access to the intl context.
 * This is not available when mounting single components in Enzyme.
 * These helper functions aim to address that and wrap a valid,
 * English-locale intl context around them.
 */

import React from 'react';
import { IntlProvider, intlShape } from 'react-intl';
import { mount, shallow } from 'enzyme';


// Create the IntlProvider to retrieve context for wrapping around.
// NOTE: Phantomjs requires usage of `var`
var intlProvider = new IntlProvider({ locale: 'en' }, {}); // eslint-disable-line
var intl = intlProvider.getChildContext(); // eslint-disable-line

/**
 * When using React-Intl `injectIntl` on components, props.intl is required.
 */
function nodeWithIntlProp(node) {
  return React.cloneElement(node, intl);
}

/**
 * Export these methods.
 */
export function shallowWithIntl(node) {
  return shallow(nodeWithIntlProp(node), { context: intl });
}
github ailabstw / pttai.js / src / components / testUtils.js View on Github external
/**
 * Components using the react-intl module require access to the intl context.
 * This is not available when mounting single components in Enzyme.
 * These helper functions aim to address that and wrap a valid,
 * English-locale intl context around them.
 */

import React from 'react'
import { IntlProvider, intlShape } from 'react-intl'
import { mount, shallow } from 'enzyme'

// You can pass your messages to the IntlProvider. Optional: remove if unneeded.
const messages = require('../translations/en') // en.json

// Create the IntlProvider to retrieve context for wrapping around.
const intlProvider = new IntlProvider({ locale: 'en', messages }, {})
const { intl } = intlProvider.getChildContext()

/**
 * When using React-Intl `injectIntl` on components, props.intl is required.
 */
function nodeWithIntlProp (node) {
  return React.cloneElement(node, { intl })
}

export function shallowWithIntl (node, { context, ...additionalOptions } = {}) {
  return shallow(
    nodeWithIntlProp(node),
    {
      context: Object.assign({}, context, { intl }),
      ...additionalOptions
    }
github streetmix / streetmix / test / helpers / intl-enzyme-test-helper.js View on Github external
* This is not available when mounting single components in Enzyme.
 * These helper functions aim to address that and wrap a valid,
 * English-locale intl context around them.
 *
 * https://github.com/yahoo/react-intl/wiki/Testing-with-React-Intl
 */

import React from 'react'
import { IntlProvider, intlShape } from 'react-intl'
import { mount, shallow } from 'enzyme'

// You can pass your messages to the IntlProvider. Optional: remove if unneeded.
const messages = require('../../assets/locales/en/main.json')

// Create the IntlProvider to retrieve context for wrapping around.
const intlProvider = new IntlProvider({ locale: 'en', messages }, {})
const { intl } = intlProvider.getChildContext()

/**
 * When using React-Intl `injectIntl` on components, props.intl is required.
 */
function nodeWithIntlProp (node) {
  return React.cloneElement(node, { intl })
}

export function shallowWithIntl (node, { context, ...additionalOptions } = {}) {
  return shallow(
    nodeWithIntlProp(node),
    {
      context: Object.assign({}, context, { intl }),
      ...additionalOptions
    }
github voluntarily / vly2 / lib / react-intl-test-helper.js View on Github external
/**
 * Components using the react-intl module require access to the intl context.
 * This is not available when mounting single components in Enzyme.
 * These helper functions aim to address that and wrap a valid,
 * English-locale intl context around them.
 */

import React from 'react'
import { IntlProvider, intlShape } from 'react-intl'
import { mount, shallow, render } from 'enzyme'

// You can pass your messages to the IntlProvider. Optional: remove if unneeded.
import messages from '../lang/en'

// Create the IntlProvider to retrieve context for wrapping around.
const intlProvider = new IntlProvider({ locale: 'en', messages }, {})
export const { intl } = intlProvider.getChildContext()

/**
 * When using React-Intl `injectIntl` on components, props.intl is required.
 */
const nodeWithIntlProp = node => {
  return React.cloneElement(node, { intl })
}

export const shallowWithIntl = node => {
  return shallow(nodeWithIntlProp(node), { context: { intl } })
}

export const mountWithIntl = node => {
  return mount(nodeWithIntlProp(node), {
    context: { intl },
github flow-typed / flow-typed / definitions / npm / react-intl_v2.x.x / flow_v0.63.x- / test_react-intl_v2.x.x.js View on Github external
injectIntl,
  FormattedMessage,
  FormattedHTMLMessage,
  FormattedDate,
  FormattedTime,
  FormattedRelative,
  FormattedNumber,
  FormattedPlural,
  IntlProvider
} from "react-intl";
import type { IntlShape, MessageDescriptor } from "react-intl";
import { it, describe } from "flow-typed-test";

intlShape({ foo: "bar" }, "propName", "TestComponentName");
// $ExpectError number. This type is incompatible with void
const result1: number = intlShape(
  { foo: "bar" },
  "propName",
  "TestComponentName"
);

const localeData = {
  locale: "fi",
  testKey: { foo: "bar" },
  testKey2: { baz: "buu" }
};
addLocaleData(localeData);
// $ExpectError number. This type is incompatible with void
const resultLocaleData: number = addLocaleData(localeData);

const messages = {
  messagekey1: {
github flow-typed / flow-typed / definitions / npm / react-intl_v2.x.x / flow_v0.57.x-v0.62.x / test_react-intl_v2.x.x.js View on Github external
intlShape,
  addLocaleData,
  defineMessages,
  injectIntl,
  FormattedMessage,
  FormattedHTMLMessage,
  FormattedDate,
  FormattedTime,
  FormattedRelative,
  FormattedNumber,
  FormattedPlural,
  IntlProvider
} from "react-intl";
import type { IntlShape, MessageDescriptor, IntlInjectedComponentClass } from "react-intl";

intlShape({ foo: "bar" }, "propName", "TestComponentName");
// $ExpectError number. This type is incompatible with void
const result1: number = intlShape(
  { foo: "bar" },
  "propName",
  "TestComponentName"
);

const localeData = {
  locale: "fi",
  testKey: { foo: "bar" },
  testKey2: { baz: "buu" }
};
addLocaleData(localeData);
// $ExpectError number. This type is incompatible with void
const resultLocaleData: number = addLocaleData(localeData);
github flow-typed / flow-typed / definitions / npm / react-intl_v2.x.x / flow_v0.57.x-v0.62.x / test_react-intl_v2.x.x.js View on Github external
defineMessages,
  injectIntl,
  FormattedMessage,
  FormattedHTMLMessage,
  FormattedDate,
  FormattedTime,
  FormattedRelative,
  FormattedNumber,
  FormattedPlural,
  IntlProvider
} from "react-intl";
import type { IntlShape, MessageDescriptor, IntlInjectedComponentClass } from "react-intl";

intlShape({ foo: "bar" }, "propName", "TestComponentName");
// $ExpectError number. This type is incompatible with void
const result1: number = intlShape(
  { foo: "bar" },
  "propName",
  "TestComponentName"
);

const localeData = {
  locale: "fi",
  testKey: { foo: "bar" },
  testKey2: { baz: "buu" }
};
addLocaleData(localeData);
// $ExpectError number. This type is incompatible with void
const resultLocaleData: number = addLocaleData(localeData);

const messages = {
  messagekey1: {
github flow-typed / flow-typed / definitions / npm / react-intl_v2.x.x / flow_v0.53.x-v0.56.x / test_react-intl_v2.x.x.js View on Github external
defineMessages,
  injectIntl,
  FormattedMessage,
  FormattedHTMLMessage,
  FormattedDate,
  FormattedTime,
  FormattedRelative,
  FormattedNumber,
  FormattedPlural,
  IntlProvider
} from "react-intl";
import type { IntlShape, MessageDescriptor } from "react-intl";

intlShape({ foo: "bar" }, "propName", "TestComponentName");
// $ExpectError number. This type is incompatible with void
const result1: number = intlShape(
  { foo: "bar" },
  "propName",
  "TestComponentName"
);

const localeData = {
  locale: "fi",
  testKey: { foo: "bar" },
  testKey2: { baz: "buu" }
};
addLocaleData(localeData);
// $ExpectError number. This type is incompatible with void
const resultLocaleData: number = addLocaleData(localeData);

const messages = {
  messagekey1: {
github flow-typed / flow-typed / definitions / npm / react-intl_v2.x.x / flow_v0.25.x-v0.52.x / test_react-intl_v2.x.x.js View on Github external
defineMessages,
  injectIntl,
  FormattedMessage,
  FormattedHTMLMessage,
  FormattedDate,
  FormattedTime,
  FormattedRelative,
  FormattedNumber,
  FormattedPlural,
  IntlProvider
} from "react-intl";
import type { IntlShape } from "react-intl";

intlShape({ foo: "bar" }, "propName", "TestComponentName");
// $ExpectError number. This type is incompatible with void
const result1: number = intlShape(
  { foo: "bar" },
  "propName",
  "TestComponentName"
);

const localeData = {
  locale: "fi",
  testKey: { foo: "bar" },
  testKey2: { baz: "buu" }
};
addLocaleData(localeData);
// $ExpectError number. This type is incompatible with void
const resultLocaleData: number = addLocaleData(localeData);

const messages = {
  messagekey1: {