From c214d7ece681515bf2bf09401331c2a2a19ebad1 Mon Sep 17 00:00:00 2001 From: Rafa Gares Date: Wed, 11 Dec 2024 12:06:33 +0100 Subject: [PATCH] test: add babel-plugin-formatjs test cases --- .../babel-preset-mc-app/production.spec.js | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 packages/babel-preset-mc-app/production.spec.js diff --git a/packages/babel-preset-mc-app/production.spec.js b/packages/babel-preset-mc-app/production.spec.js new file mode 100644 index 0000000000..aeaaab0a2f --- /dev/null +++ b/packages/babel-preset-mc-app/production.spec.js @@ -0,0 +1,97 @@ +import { transformSync } from '@babel/core'; +import getBabePresetConfigForMcAppForProduction from './production'; + +describe('babel-plugin-formatjs', () => { + const code = ` + import { defineMessages } from 'react-intl'; + + const messages = defineMessages({ + welcome: { + id: 'app.welcome', + defaultMessage: 'Welcome, {name}!', + description: 'Message to greet the user by name', + }, + }); + `; + + it('should remove description by default', () => { + const config = getBabePresetConfigForMcAppForProduction(null); + + const result = transformSync(code, { + filename: 'dummy-file-name.js', + presets: config.presets, + plugins: config.plugins, + }); + + // Check that `defaultMessage` is removed from the output + expect(result.code).toMatchInlineSnapshot(` + ""use strict"; + + var _reactIntl = require("react-intl"); + const messages = (0, _reactIntl.defineMessages)({ + welcome: { + id: "app.welcome", + defaultMessage: "Welcome, {name}!" + } + });" + `); + }); + + it('should remove defaultMessage when i18nRemoveDefaultMessage is true', () => { + const config = getBabePresetConfigForMcAppForProduction(null, { + i18nRemoveDefaultMessage: true, + }); + + const result = transformSync(code, { + filename: 'dummy-file-name.js', + presets: config.presets, + plugins: config.plugins, + }); + + // Check that `defaultMessage` is removed from the output + expect(result.code).toMatchInlineSnapshot(` + ""use strict"; + + var _reactIntl = require("react-intl"); + const messages = (0, _reactIntl.defineMessages)({ + welcome: { + id: "app.welcome" + } + });" + `); + }); + + it('should parse defaultMessage into AST when i18nAst is true', () => { + const config = getBabePresetConfigForMcAppForProduction(null, { + i18nAst: true, + }); + + const result = transformSync(code, { + filename: 'dummy-file-name.js', + presets: config.presets, + plugins: config.plugins, + }); + + // Check that `defaultMessage` is removed from the output + expect(result.code).toMatchInlineSnapshot(` + ""use strict"; + + var _reactIntl = require("react-intl"); + const messages = (0, _reactIntl.defineMessages)({ + welcome: { + id: "app.welcome", + defaultMessage: [{ + "type": 0, + "value": "Welcome, " + }, { + "type": 1, + "value": "name" + }, { + "type": 0, + "value": "!" + }] + } + });" + `); + }); +});