From 463fcfec4f0a67f7e4970485325f9685d5f91e9a Mon Sep 17 00:00:00 2001 From: Alexander Nanberg Date: Sat, 4 Jan 2025 10:38:53 +0100 Subject: [PATCH] Remove use of testSection util for snapshots --- .../src/__tests__/FbtTestUtil.tsx | 86 +- .../__snapshots__/fbtAutoWrap-test.tsx.snap | 2 +- .../__tests__/__snapshots__/jsx-test.tsx.snap | 4 +- .../src/__tests__/fbtAutoWrap-test.tsx | 668 ++++++----- .../src/__tests__/fbtStaticJSModule-test.tsx | 278 ++--- .../src/__tests__/jsx-test.tsx | 1010 ++++++++++------- 6 files changed, 1094 insertions(+), 954 deletions(-) diff --git a/packages/babel-plugin-fbtee/src/__tests__/FbtTestUtil.tsx b/packages/babel-plugin-fbtee/src/__tests__/FbtTestUtil.tsx index 67488685..e2338fed 100644 --- a/packages/babel-plugin-fbtee/src/__tests__/FbtTestUtil.tsx +++ b/packages/babel-plugin-fbtee/src/__tests__/FbtTestUtil.tsx @@ -1,4 +1,4 @@ -import babel, { PluginItem, PluginOptions, transformSync } from '@babel/core'; +import { PluginOptions, transformSync } from '@babel/core'; import generate from '@babel/generator'; import { parse as babelParse } from '@babel/parser'; import syntaxJSX from '@babel/plugin-syntax-jsx'; @@ -35,10 +35,7 @@ export function snapshotTransform( return transform(source, { fbtBase64: true, ...pluginOptions }); } -async function transformKeepJsx( - source: string, - pluginOptions?: PluginOptions, -): Promise { +function transformKeepJsx(source: string, pluginOptions?: PluginOptions) { return prettier.format( transformSync(source, { ast: false, @@ -54,8 +51,7 @@ async function transformKeepJsx( export const snapshotTransformKeepJsx = ( source: string, pluginOptions?: PluginOptions, -): Promise => - transformKeepJsx(source, { fbtBase64: true, ...pluginOptions }); +) => transformKeepJsx(source, { fbtBase64: true, ...pluginOptions }); export function withFbsImportStatement(code: string): string { return `import { fbs } from "fbtee"; @@ -167,7 +163,7 @@ function parse(code: string) { }); } -export function generateFormattedCodeFromAST(node: Node) { +function generateFormattedCodeFromAST(node: Node) { return generate.default(node, { comments: true }, '').code.trim(); } @@ -197,24 +193,6 @@ function normalizeSourceCode(sourceCode: string) { .code.trim(); } -/** - * This function allows you to use mutliline template strings in your test - * cases without worrying about non standard loc's. It does this by stripping - * leading whitespace so the contents lines up based on the first lines - * offset. - */ -export function stripCodeBlockWhitespace(code: string) { - // Find standard whitespace offset for block - const match = code.match(/(\n\s*)\S/); - const strippedCode = - match == null - ? code - : // Strip from each line - code.replaceAll(new RegExp(match[1], 'g'), '\n'); - - return strippedCode; -} - const indent = (code: string) => code .split('\n') @@ -274,44 +252,6 @@ ${indent(excerptDiffFromReceived)} } } -export function testSectionAsync( - testData: TestCases, - transform: ( - source: string, - options?: Record, - ) => Promise, - options: Options, -) { - Object.entries(testData).forEach(([title, testInfo]) => { - test(title, async () => { - if (testInfo.throws === true) { - await expect( - transform(testInfo.input, testInfo.options), - ).rejects.toThrow(); - } else if (typeof testInfo.throws === 'string') { - await expect( - transform(testInfo.input, testInfo.options), - ).rejects.toThrow(testInfo.throws); - } else { - expect( - (async () => { - const transformOutput = await transform( - testInfo.input, - testInfo.options, - ); - if (options && options.matchSnapshot) { - expect(transformOutput).toMatchSnapshot(); - } else if (testInfo.output) { - assertSourceAstEqual(testInfo.output, transformOutput, options); - } - return true; - })(), - ).resolves.toBe(true); - } - }); - }); -} - export function testSection( testData: TestCases, transform: (source: string, options?: Options) => string, @@ -338,21 +278,3 @@ export function testSection( }); }); } - -export function testCase( - name: string, - plugins: Array, - testData: TestCases, - options: Options, -) { - describe(name, () => - testSection( - testData, - (source: string) => - babel.transformSync(source, { - plugins, - })?.code || '', - options, - ), - ); -} diff --git a/packages/babel-plugin-fbtee/src/__tests__/__snapshots__/fbtAutoWrap-test.tsx.snap b/packages/babel-plugin-fbtee/src/__tests__/__snapshots__/fbtAutoWrap-test.tsx.snap index 6470bf59..2beac853 100644 --- a/packages/babel-plugin-fbtee/src/__tests__/__snapshots__/fbtAutoWrap-test.tsx.snap +++ b/packages/babel-plugin-fbtee/src/__tests__/__snapshots__/fbtAutoWrap-test.tsx.snap @@ -998,7 +998,7 @@ fbt._( `; -exports[`Test jsx auto-wrapping of implicit parameters should wrap a string next to an explicit that has a implicit within it 1`] = ` +exports[`Test jsx auto-wrapping of implicit parameters should wrap a string next to an explicit that has an implicit within it 1`] = ` import { fbt } from "fbtee"; fbt._( /* __FBT__ start */ { diff --git a/packages/babel-plugin-fbtee/src/__tests__/__snapshots__/jsx-test.tsx.snap b/packages/babel-plugin-fbtee/src/__tests__/__snapshots__/jsx-test.tsx.snap index b5e8981c..dde5bc0c 100644 --- a/packages/babel-plugin-fbtee/src/__tests__/__snapshots__/jsx-test.tsx.snap +++ b/packages/babel-plugin-fbtee/src/__tests__/__snapshots__/jsx-test.tsx.snap @@ -311,7 +311,7 @@ var x = fbt._( `; -exports[`Test declarative (jsx) fbt syntax translation should handle single expression with concentated strings 1`] = ` +exports[`Test declarative (jsx) fbt syntax translation should handle single expression with concatenated strings 1`] = ` import { fbt } from "fbtee"; fbt._( /* __FBT__ start */ { @@ -506,7 +506,7 @@ fbt._( `; -exports[`Test declarative (jsx) fbt syntax translation should support non-breasking space character 1`] = ` +exports[`Test declarative (jsx) fbt syntax translation should support non-breaking space character 1`] = ` import { fbt } from "fbtee"; fbt._( /* __FBT__ start */ { diff --git a/packages/babel-plugin-fbtee/src/__tests__/fbtAutoWrap-test.tsx b/packages/babel-plugin-fbtee/src/__tests__/fbtAutoWrap-test.tsx index 57ea0c42..ac0edf08 100644 --- a/packages/babel-plugin-fbtee/src/__tests__/fbtAutoWrap-test.tsx +++ b/packages/babel-plugin-fbtee/src/__tests__/fbtAutoWrap-test.tsx @@ -2,64 +2,72 @@ import { describe, expect } from '@jest/globals'; import { jsCodeFbtCallSerializer, snapshotTransform, - testSection, withFbtImportStatement, } from './FbtTestUtil.tsx'; expect.addSnapshotSerializer(jsCodeFbtCallSerializer); -const testData = { - 'can handle multiple variations in nested strings': { - input: withFbtImportStatement( - ` - Level 1 - - {foo} - - - Level 2 - - {bar} - - - - Level 3 - - cat - - - - `, - ), - }, +describe('Test jsx auto-wrapping of implicit parameters', () => { + test('can handle multiple variations in nested strings', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` + Level 1 + + {foo} + + + Level 2 + + {bar} + + + + Level 3 + + cat + + + + `, + ), + ), + ).toMatchSnapshot(); + }); - 'can handle multiple variations in nested strings and a subject gender': { - input: withFbtImportStatement( - ` - Level 1 - - some_pronoun - - - Level 2 - - cat - - - `, - ), - }, + test('can handle multiple variations in nested strings and a subject gender', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` + Level 1 + + some_pronoun + + + Level 2 + + cat + + + `, + ), + ), + ).toMatchSnapshot(); + }); - 'can handle multiple variations in nested strings with substrings that look identical': - { - input: withFbtImportStatement( - ` + test('can handle multiple variations in nested strings with substrings that look identical', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` Level 1 {/* Substring A */} @@ -73,275 +81,335 @@ const testData = { `, + ), ), - }, + ).toMatchSnapshot(); + }); - 'prevent token name collisions among fbt constructs across all nesting levels (v1)': - { - input: withFbtImportStatement( - ` - Level 1 - - {foo} - - - Level 2 - + test('prevent token name collisions among fbt constructs across all nesting levels (v1)', () => { + expect(() => + snapshotTransform( + withFbtImportStatement( + ` + Level 1 + {foo} - - - `, + + + Level 2 + + {foo} + + + `, + ), ), + ).toThrow("There's already a token called `foo` in this fbt call."); + }); - throws: "There's already a token called `foo` in this fbt call.", - }, - - 'prevent token name collisions among fbt constructs across all nesting levels (v2)': - { - input: withFbtImportStatement( - ` - Level 1 - - {bar} - - - Level 2 - - {foo} - - - Level 3 - - cat - - - - `, + test('prevent token name collisions among fbt constructs across all nesting levels (v2)', () => { + expect(() => + snapshotTransform( + withFbtImportStatement( + ` + Level 1 + + {bar} + + + Level 2 + + {foo} + + + Level 3 + + cat + + + + `, + ), ), + ).toThrow("There's already a token called `foo` in this fbt call."); + }); - throws: "There's already a token called `foo` in this fbt call.", - }, - - 'should auto wrap a simple test with a nested level': { - input: withFbtImportStatement( - ` - - Your friends - liked - - your video - ;`, - ), - }, - - 'should auto wrap a simple test with one level': { - input: withFbtImportStatement( - ` - Your friends - liked your video - ;`, - ), - }, + test('should auto wrap a simple test with a nested level', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` + + Your friends + liked + + your video + ;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should work with multiple calls in one file': { - input: withFbtImportStatement( - `
- -
first
- fbt call -
- -
second
- test -
-
;`, - ), - }, + test('should auto wrap a simple test with one level', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` + Your friends + liked your video + ;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should wrap a child nested in an explicit ': { - input: withFbtImportStatement( - ` - -
- - explicit fbt param -
with a nested implicit param
+ test('should work with multiple calls in one file', () => { + expect( + snapshotTransform( + withFbtImportStatement( + `
+ +
first
+ fbt call
-
- -
;`, - ), - }, + +
second
+ test +
+
;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should wrap a child next to an explicit ': { - input: withFbtImportStatement( - ` - -
- explicit param next to -
-
-
an implicit param
-
;`, - ), - }, + test('should wrap a child nested in an explicit ', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` + +
+ + explicit fbt param +
with a nested implicit param
+
+
+
+
;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should wrap a single unwrapped child and a string above': { - input: withFbtImportStatement( - ` - - This is - a nested - - test - ;`, - ), - }, + test('should wrap a child next to an explicit ', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` + +
+ explicit param next to +
+
+
an implicit param
+
;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should wrap a single unwrapped child and a string below': { - input: withFbtImportStatement( - ` -
this is
- a singly nested test -
;`, - ), - }, + test('should wrap a single unwrapped child and a string above', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` + + This is + a nested + + test + ;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should wrap a string next to an explicit that has a implicit within it': - { - input: withFbtImportStatement( - ` - outer string that should not appear in inner desc - -
- - explicit fbt param -
with a nested implicit param
-
-
-
-
;`, + test('should wrap a single unwrapped child and a string below', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` +
this is
+ a singly nested test +
;`, + ), ), - }, + ).toMatchSnapshot(); + }); - 'should wrap an outer and inner child': { - input: withFbtImportStatement( - ` -
-
this is
- a doubly -
- nested test -
;`, - ), - }, + test('should wrap a string next to an explicit that has an implicit within it', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` + outer string that should not appear in inner desc + +
+ + explicit fbt param +
with a nested implicit param
+
+
+
+
;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should wrap explicit params nested in implicit params with []': { - input: withFbtImportStatement( - ` -
- this is a test - - - - to make sure that explicit tags - under an implicit node - - - - - - - and ones that are next - to each other - - - - under an implicit tag are wrapped with [ ] -
- - - - but free standing ones - are not - - - -
;`, - ), - }, + test('should wrap an outer and inner child', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` +
+
this is
+ a doubly +
+ nested test +
;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should wrap two children with one nested level': { - input: withFbtImportStatement( - ` -
-
this is
- a doubly -
- nested test -
with an additional level
-
;`, - ), - }, + test('should wrap explicit params nested in implicit params with []', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` +
+ this is a test + + + + to make sure that explicit tags + under an implicit node + + + + + + + and ones that are next + to each other + + + + under an implicit tag are wrapped with [ ] +
+ + + + but free standing ones + are not + + + +
;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should wrap two nested next to each other': { - input: withFbtImportStatement( - ` -
- one -
two
-
-
- three -
four
-
-
;`, - ), - }, + test('should wrap two children with one nested level', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` +
+
this is
+ a doubly +
+ nested test +
with an additional level
+
;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should wrap two nested next to each other with an extra level': { - input: withFbtImportStatement( - ` -
- one -
- two -
test
-
-
-
- three -
four
-
-
;`, - ), - }, + test('should wrap two nested next to each other', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` +
+ one +
two
+
+
+ three +
four
+
+
;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should wrap two unwrapped children': { - input: withFbtImportStatement( - ` -
wrap once
-
wrap twice
-
;`, - ), - }, + test('should wrap two nested next to each other with an extra level', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` +
+ one +
+ two +
test
+
+
+
+ three +
four
+
+
;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should wrap two unwrapped children and 1 nested': { - input: withFbtImportStatement( - ` -
- wrap once -
and also
-
-
wrap twice
- complicated -
;`, - ), - }, -}; + test('should wrap two unwrapped children', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` +
wrap once
+
wrap twice
+
;`, + ), + ), + ).toMatchSnapshot(); + }); -describe('Test jsx auto-wrapping of implicit parameters', () => - testSection(testData, snapshotTransform, { matchSnapshot: true })); + test('should wrap two unwrapped children and 1 nested', () => { + expect( + snapshotTransform( + withFbtImportStatement( + ` +
+ wrap once +
and also
+
+
wrap twice
+ complicated +
;`, + ), + ), + ).toMatchSnapshot(); + }); +}); diff --git a/packages/babel-plugin-fbtee/src/__tests__/fbtStaticJSModule-test.tsx b/packages/babel-plugin-fbtee/src/__tests__/fbtStaticJSModule-test.tsx index 05bb704a..b194f0fb 100644 --- a/packages/babel-plugin-fbtee/src/__tests__/fbtStaticJSModule-test.tsx +++ b/packages/babel-plugin-fbtee/src/__tests__/fbtStaticJSModule-test.tsx @@ -5,7 +5,6 @@ import { jsCodeFbtCallSerializer, payload, snapshotTransform, - testSection, transform, withFbtImportStatement, } from './FbtTestUtil.tsx'; @@ -26,140 +25,163 @@ describe('fbt preserveWhitespace argument', () => { // Here we are intentionally testing for the wrong behavior. We will come // back and update the expected output after we fix space normalization. describe('should NOT preserve whitespaces that do not neighbor raw text', () => { - const snapshotTestData = { - 'jsx elements and raw text': { - input: withFbtImportStatement(` - var x = - - - Where do - - spaces - go? - Good - question - ! - ; - `), - }, - 'jsx elements and string variation arguments nested inside jsx element': { - input: withFbtImportStatement(` - var x = - - OuterJsx1 - RawText - OuterJsx2 - - InnerJsx1 - (plural) - InnerJsx2 - - ; - `), - }, - 'jsx elements with string variation arguments': { - input: withFbtImportStatement(` - var x = - - - There should be - - - - a space - - - ! - ; - `), - }, - 'should not preserve whitespace around text in JSXExpression': { - input: withFbtImportStatement(` - var x = - - OuterJsx1 - {'textInJSXExpression'} - OuterJsx2 - - rawText - {'textInJSXExpression'} - InnerJsx1 + it('jsx elements and raw text', () => { + expect( + snapshotTransform( + withFbtImportStatement(` + var x = + + + Where do + + spaces + go? + Good + question + ! + ; + `), + ), + ).toMatchSnapshot(); + }); + + it('jsx elements and string variation arguments nested inside jsx element', () => { + expect( + snapshotTransform( + withFbtImportStatement(` + var x = + + OuterJsx1 + RawText + OuterJsx2 + + InnerJsx1 + (plural) + InnerJsx2 + + ; + `), + ), + ).toMatchSnapshot(); + }); + + it('jsx elements with string variation arguments', () => { + expect( + snapshotTransform( + withFbtImportStatement(` + var x = + + + There should be + + + + a space + + + ! + ; + `), + ), + ).toMatchSnapshot(); + }); + + it('should not preserve whitespace around text in JSXExpression', () => { + expect( + snapshotTransform( + withFbtImportStatement(` + var x = + + OuterJsx1 {'textInJSXExpression'} - (plural) - {'text' + 'InJSXExpression'} - InnerJsx2 - {\`text${'InJSXExpression'}\`} - - ; - `), - }, - 'should preserve voluntarily added spaces between NON-raw text': { - input: withFbtImportStatement(` - var x = - - OuterJsx1 - {' '} - OuterJsx2 - - {' '} - InnerJsx1 - {' '} - (plural) - {' '} - InnerJsx2 - {' '} - InnerJsx3 + OuterJsx2 + + rawText + {'textInJSXExpression'} + InnerJsx1 + {'textInJSXExpression'} + (plural) + {'text' + 'InJSXExpression'} + InnerJsx2 + {\`text${'InJSXExpression'}\`} + + ; + `), + ), + ).toMatchSnapshot(); + }); + + it('should preserve voluntarily added spaces between NON-raw text', () => { + expect( + snapshotTransform( + withFbtImportStatement(` + var x = + + OuterJsx1 {' '} - -
; - `), - }, - 'should treat comments in JSXExpression like they are not here': { - input: withFbtImportStatement(` - var x = - - OuterJsx1 - {/* someComment */} - OuterJsx2 - - {/* someComment */} - InnerJsx1 - {/* someComment */} - (plural) + OuterJsx2 + + {' '} + InnerJsx1 + {' '} + (plural) + {' '} + InnerJsx2 + {' '} + InnerJsx3 + {' '} + + ; + `), + ), + ).toMatchSnapshot(); + }); + + it('should treat comments in JSXExpression like they are not here', () => { + expect( + snapshotTransform( + withFbtImportStatement(` + var x = + + OuterJsx1 {/* someComment */} - rawText - -
; - `), - }, - }; - testSection(snapshotTestData, snapshotTransform, { - matchSnapshot: true, + OuterJsx2 + + {/* someComment */} + InnerJsx1 + {/* someComment */} + (plural) + {/* someComment */} + rawText + +
; + `), + ), + ).toMatchSnapshot(); }); }); describe('should preserve whitespace around text', () => { - const snapshotTestData = { - 'with inner text and string variation': { - input: withFbtImportStatement(` - var x = - - outerText - outerJsx - - innerJsx - innerText - (plural) - - ; - `), - }, - }; - testSection(snapshotTestData, snapshotTransform, { - matchSnapshot: true, + it('with inner text and string variation', () => { + expect( + snapshotTransform( + withFbtImportStatement(` + var x = + + outerText + outerJsx + + innerJsx + innerText + (plural) + + ; + `), + ), + ).toMatchSnapshot(); }); }); diff --git a/packages/babel-plugin-fbtee/src/__tests__/jsx-test.tsx b/packages/babel-plugin-fbtee/src/__tests__/jsx-test.tsx index 37d01815..f209bc86 100644 --- a/packages/babel-plugin-fbtee/src/__tests__/jsx-test.tsx +++ b/packages/babel-plugin-fbtee/src/__tests__/jsx-test.tsx @@ -2,465 +2,593 @@ import { describe, expect, it, test } from '@jest/globals'; import { jsCodeFbtCallSerializer, snapshotTransformKeepJsx, - testSectionAsync, withFbtImportStatement, } from './FbtTestUtil.tsx'; expect.addSnapshotSerializer(jsCodeFbtCallSerializer); -const testData = { - 'Enable explicit whitespace': { - input: withFbtImportStatement( - `var x = - {one} - {" "} - {two} - {\` \`} - {three} - ;`, - ), - }, - - 'Squelch whitespace when in an expression': { - input: withFbtImportStatement( - `var x = - - {"Squelched white space... "} - with some - {' other stuff.'} - ; - baz();`, - ), - }, - - 'fbt:param with multiple children should error': { - input: withFbtImportStatement( - ` - - {foo} - {bar} - - `, - ), +describe('Test declarative (jsx) fbt syntax translation', () => { + test('Enable explicit whitespace', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + {one} + {" "} + {two} + {\` \`} + {three} + ;`, + ), + ), + ).toMatchSnapshot(); + }); - throws: `fbt:param expects an {expression} or JSX element, and only one`, - }, - - 'fbt:param with multiple empty expression containers should be ok': { - input: withFbtImportStatement( - ` - - {} - {/* comment */} - {foo} - {} - - `, - ), - }, - - 'should be able to house arbitrary markup within fbt:param nodes': { - input: withFbtImportStatement( - `
- - {formatDate(date, "F d, Y")} - by - - - {user.name} - - - -
;`, - ), - }, + test('Squelch whitespace when in an expression', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + + {"Squelched white space... "} + with some + {' other stuff.'} + ; + baz();`, + ), + ), + ).toMatchSnapshot(); + }); - 'should be able to nest within React nodes': { - input: withFbtImportStatement( - `var x =
- - A nested string - -
;`, - ), - }, + test('fbt:param with multiple children should error', () => { + expect(() => + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + + {foo} + {bar} + + `, + ), + ), + ).toThrow(`fbt:param expects an {expression} or JSX element, and only one`); + }); - 'should convert simple strings': { - input: withFbtImportStatement( - `var x = A simple string;`, - ), - }, - - 'should correctly destruct expression values in options': { - input: withFbtImportStatement( - `str - - {getNum()} - - `, - ), - }, - - 'should filter comment and empty expressions from children': { - input: withFbtImportStatement( - `var x = - { - }A sim{/* - ignore - me - */}ple s{ }tri{}ng{/*ignore me*/};`, - ), - }, - - 'should handle common string': { - input: withFbtImportStatement(`Done`), - - options: { - fbtCommon: { Done: 'The description for the common string "Done"' }, - }, - }, - - 'should handle concatenated descriptions': { - input: withFbtImportStatement( - ` - Here it is - ;`, - ), - }, - - 'should handle empty string': { - input: withFbtImportStatement( - `var x = - A parameterized message to: - - ;`, - ), - }, - - 'should handle enums (with array values)': { - input: withFbtImportStatement( - `var x = - Click to see - - ;`, - ), - }, - - 'should handle enums (with object values)': { - input: withFbtImportStatement( - `var x = - Click to see - - ;`, - ), - }, - - 'should handle enums with more text': { - input: withFbtImportStatement( - `var x = - Click to see - - Hey-hey! - ;`, - ), - }, - - 'should handle fbt common attribute without value': { - input: withFbtImportStatement(`Okay`), - - options: { - fbtCommon: { Okay: 'The description for the common string "Okay"' }, - }, - }, - - 'should handle number={true} - (same output as above test)': { - input: withFbtImportStatement( - `var x = - Click to see - {c} - links - ;`, - ), - }, + test('fbt:param with multiple empty expression containers should be ok', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + + {} + {/* comment */} + {foo} + {} + + `, + ), + ), + ).toMatchSnapshot(); + }); - 'should handle object pronoun': { - input: withFbtImportStatement( - ` - I know . - ;`, - ), - }, - - 'should handle params': { - input: withFbtImportStatement( - `var x = - A parameterized message to: - {theName} - ;`, - ), - }, + test('should be able to house arbitrary markup within fbt:param nodes', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `
+ + {formatDate(date, "F d, Y")} + by + + + {user.name} + + + +
;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should handle single expression with concentated strings': { - input: withFbtImportStatement( - ` - {"foo" + "bar"} - `, - ), - }, - - 'should handle subject+reflexive pronouns': { - input: - // She wished herself a happy birthday. - withFbtImportStatement( - ` - - wished a happy birthday. - ;`, - ), - }, - - 'should handle template descriptions': { - input: withFbtImportStatement( - ` - Here it is - ;`, - ), - }, - - 'should handle variations': { - input: withFbtImportStatement( - `var x = - Click to see - {c} - links - ;`, - ), - }, - - 'should ignore __private attributes': { - input: withFbtImportStatement( - ` - - {foo} - - `, - ), - }, - - 'should ignore non-expression children in fbt:param': { - input: withFbtImportStatement( - ` - - !{foo}! - - `, - ), - }, - - 'should insert param value for same-param': { - input: withFbtImportStatement( - `str - {Bar} and - - `, - ), - }, - - 'should maintain order of params and enums': { - input: withFbtImportStatement( - ` - Hello, - - {foo} - - - - {bar} - - `, - ), - }, - - 'should not insert extra space': { - input: withFbtImportStatement( - ` - Hello, - {guest} - ! - `, - ), - }, - - 'should strip out more newlines': { - input: withFbtImportStatement( - `var x = - - A simple string... - with some other stuff. - ; - baz();`, - ), - }, - - 'should strip out newlines': { - input: withFbtImportStatement( - `var x = - - Preamble - {blah} - ; - baz();`, - ), - }, + test('should be able to nest within React nodes', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x =
+ + A nested string + +
;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should support html escapes': { - input: withFbtImportStatement( - `×`, - ), - }, + test('should convert simple strings', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = A simple string;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should support non-breasking space character': { - // multiple spaces are normalized to a single space - // but   characters are preserved - input: withFbtImportStatement( - ` - text with non-breaking   space - `, - ), - }, - - 'should support unicode characters': { - input: withFbtImportStatement( - `// A backslash \\ in comments - - A copyright sign {'\\u00A9'}, - a multi byte character {'\\uD83D\\uDCA9'}, - and a backslash {'\\\\'}. - `, - ), - }, + test('should correctly destruct expression values in options', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `str + + {getNum()} + + `, + ), + ), + ).toMatchSnapshot(); + }); - 'should throw for fbt that has description and common attribute (without value)': - { - input: withFbtImportStatement(`No`), + test('should filter comment and empty expressions from children', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + { + }A sim{/* + ignore + me + */}ple s{ }tri{}ng{/*ignore me*/};`, + ), + ), + ).toMatchSnapshot(); + }); - options: { - fbtCommon: { No: 'The description for the common string "No"' }, - }, + test('should handle common string', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement(`Done`), + { fbtCommon: { Done: 'The description for the common string "Done"' } }, + ), + ).toMatchSnapshot(); + }); - throws: ` must not have "desc" attribute.`, - }, + test('should handle concatenated descriptions', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + Here it is + ;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should throw for strings with `common` attribute equal to false': { - input: withFbtImportStatement(`Yes`), + test('should handle empty string', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + A parameterized message to: + + ;`, + ), + ), + ).toMatchSnapshot(); + }); - options: { - fbtCommon: { Yes: 'The description for the common string "Yes"' }, - }, + test('should handle enums (with array values)', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + Click to see + + ;`, + ), + ), + ).toMatchSnapshot(); + }); - throws: `This node requires a 'desc' attribute.`, - }, + test('should handle enums (with object values)', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + Click to see + + ;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should throw on invalid attributes in fbt:param': { - input: withFbtImportStatement( - ` - - {foo} - - `, - ), + test('should handle enums with more text', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + Click to see + + Hey-hey! + ;`, + ), + ), + ).toMatchSnapshot(); + }); - throws: `Invalid option "qux". Only allowed: gender, number, name`, - }, + test('should handle fbt common attribute without value', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement(`Okay`), + { fbtCommon: { Okay: 'The description for the common string "Okay"' } }, + ), + ).toMatchSnapshot(); + }); - 'should throw on undefined common string': { - input: withFbtImportStatement( - `Some undefined common string`, - ), + test('should handle number={true} - (same output as above test)', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + Click to see + {c} + links + ;`, + ), + ), + ).toMatchSnapshot(); + }); - options: {}, + test('should handle object pronoun', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + I know . + ;`, + ), + ), + ).toMatchSnapshot(); + }); - throws: `Unknown string "Some undefined common string" for `, - }, + test('should handle params', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + A parameterized message to: + {theName} + ;`, + ), + ), + ).toMatchSnapshot(); + }); - 'should treat multiline descs as a single line': { - input: withFbtImportStatement( - ` - lol - `, - ), - }, - - 'should work with fragments': { - input: withFbtImportStatement( - ` - - {formatDate(date, "F d, Y")} - by - - - {user.name} - - - - ;`, - ), - }, - - 'should work with implicit fragments': { - input: withFbtImportStatement( - `<> - - {formatDate(date, "F d, Y")} - by - - - {user.name} - - - - ;`, - ), - }, -}; + test('should handle single expression with concatenated strings', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + {"foo" + "bar"} + `, + ), + ), + ).toMatchSnapshot(); + }); + + test('should handle subject+reflexive pronouns', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + + wished a happy birthday. + ;`, + ), + ), + ).toMatchSnapshot(); + }); + + test('should handle template descriptions', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + Here it is + ;`, + ), + ), + ).toMatchSnapshot(); + }); + + test('should handle variations', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + Click to see + {c} + links + ;`, + ), + ), + ).toMatchSnapshot(); + }); -describe('Test declarative (jsx) fbt syntax translation', () => - testSectionAsync(testData, snapshotTransformKeepJsx, { - matchSnapshot: true, - })); + test('should ignore __private attributes', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + + {foo} + + `, + ), + ), + ).toMatchSnapshot(); + }); + + test('should ignore non-expression children in fbt:param', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + + !{foo}! + + `, + ), + ), + ).toMatchSnapshot(); + }); + + test('should insert param value for same-param', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `str + {Bar} and + + `, + ), + ), + ).toMatchSnapshot(); + }); + + test('should maintain order of params and enums', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + Hello, + + {foo} + + + + {bar} + + `, + ), + ), + ).toMatchSnapshot(); + }); + + test('should not insert extra space', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + Hello, + {guest} + ! + `, + ), + ), + ).toMatchSnapshot(); + }); + + test('should strip out more newlines', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + + A simple string... + with some other stuff. + ; + baz();`, + ), + ), + ).toMatchSnapshot(); + }); + + test('should strip out newlines', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `var x = + + Preamble + {blah} + ; + baz();`, + ), + ), + ).toMatchSnapshot(); + }); + + test('should support html escapes', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement(`×`), + ), + ).toMatchSnapshot(); + }); + + test('should support non-breaking space character', () => { + // multiple spaces are normalized to a single space + // but   characters are preserved + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + text with non-breaking   space + `, + ), + ), + ).toMatchSnapshot(); + }); + + test('should support unicode characters', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `// A backslash \\ in comments + + A copyright sign {'\\u00A9'}, + a multi byte character {'\\uD83D\\uDCA9'}, + and a backslash {'\\\\'}. + `, + ), + ), + ).toMatchSnapshot(); + }); + + test('should throw for fbt that has description and common attribute (without value)', () => { + expect(() => + snapshotTransformKeepJsx( + withFbtImportStatement(`No`), + { fbtCommon: { No: 'The description for the common string "No"' } }, + ), + ).toThrow(` must not have "desc" attribute.`); + }); + + test('should throw for strings with `common` attribute equal to false', () => { + expect(() => + snapshotTransformKeepJsx( + withFbtImportStatement(`Yes`), + { fbtCommon: { Yes: 'The description for the common string "Yes"' } }, + ), + ).toThrow(`This node requires a 'desc' attribute.`); + }); + + test('should throw on invalid attributes in fbt:param', () => { + expect(() => + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + + {foo} + + `, + ), + ), + ).toThrow(`Invalid option "qux". Only allowed: gender, number, name`); + }); + + test('should throw on undefined common string', () => { + expect(() => + snapshotTransformKeepJsx( + withFbtImportStatement( + `Some undefined common string`, + ), + {}, + ), + ).toThrow( + `Unknown string "Some undefined common string" for `, + ); + }); + + test('should treat multiline descs as a single line', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + lol + `, + ), + ), + ).toMatchSnapshot(); + }); + + test('should work with fragments', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + ` + + {formatDate(date, "F d, Y")} + by + + + {user.name} + + + + ;`, + ), + ), + ).toMatchSnapshot(); + }); + + test('should work with implicit fragments', () => { + expect( + snapshotTransformKeepJsx( + withFbtImportStatement( + `<> + + {formatDate(date, "F d, Y")} + by + + + {user.name} + + + + ;`, + ), + ), + ).toMatchSnapshot(); + }); +}); describe('Test fbt transforms without the jsx transform', () => { it('not nested', async () => { expect( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` let x = @@ -473,7 +601,7 @@ describe('Test fbt transforms without the jsx transform', () => { it('nested in div', async () => { expect( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` let x =
@@ -488,7 +616,7 @@ describe('Test fbt transforms without the jsx transform', () => { it('short bool syntax for doNotExtract attribute', async () => { expect( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` let x = Test; `), @@ -498,7 +626,7 @@ describe('Test fbt transforms without the jsx transform', () => { it('short bool syntax for number attribute', async () => { expect( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` let x = @@ -512,7 +640,7 @@ describe('Test fbt transforms without the jsx transform', () => { describe('when using within template literals', () => { it('should work with a basic ', async () => { expect( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` html\`
\${ @@ -528,7 +656,7 @@ describe('Test fbt transforms without the jsx transform', () => { it('should work with basic auto-parameterization', async () => { expect( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` html\`
\${ @@ -547,7 +675,7 @@ describe('Test fbt transforms without the jsx transform', () => { it('should dedupe plurals', async () => { expect( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` There @@ -563,7 +691,7 @@ describe('Test fbt transforms without the jsx transform', () => { it('should work with a nested within an ', async () => { expect( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` html\`
\${ @@ -595,7 +723,7 @@ describe('Test fbt transforms without the jsx transform', () => { // TODO: Fix preserving whitespace for JSX text. it('should fail to preserve whitespace in text when preserveWhitespace=true (known bug)', async () => { expect( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` Some text with 3 spaces in between. @@ -608,7 +736,7 @@ describe('Test fbt transforms without the jsx transform', () => { // TODO: We should NOT insert a space between two 's it(`[legacy buggy behavior] should insert a space character between two fbt constructs that don't neighbor raw text`, async () => expect( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` You can add @@ -630,14 +758,14 @@ test('Test common fbt with value-less `common` attribute should have same runtim fbtCommon: { Submit: 'The description for the common string "Submit"' }, }; expect( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` let x = Submit; `), options, ), ).toEqual( - await snapshotTransformKeepJsx( + snapshotTransformKeepJsx( withFbtImportStatement(` let x = Submit; `),