Skip to content

Commit

Permalink
Update snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandernanberg committed Dec 22, 2024
1 parent 4b43eda commit 6ca460e
Show file tree
Hide file tree
Showing 9 changed files with 1,095 additions and 2,517 deletions.
35 changes: 4 additions & 31 deletions packages/babel-plugin-fbtee/src/__tests__/FbtTestUtil.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -7,7 +7,6 @@ import presetTypeScript from '@babel/preset-typescript';
import { Node } from '@babel/types';
import { expect } from '@jest/globals';
import prettier from 'prettier-2';
import { SENTINEL } from '../FbtConstants.tsx';
import fbt from '../index.tsx';

export function payload(obj: Record<string, unknown>): string {
Expand All @@ -32,7 +31,7 @@ export function snapshotTransform(
source: string,
pluginOptions?: PluginOptions,
): string {
return transform(source, { fbtBase64: true, ...pluginOptions });
return prettier.format(transform(source, pluginOptions), { parser: 'babel' });
}

async function transformKeepJsx(
Expand All @@ -54,8 +53,7 @@ async function transformKeepJsx(
export const snapshotTransformKeepJsx = (
source: string,
pluginOptions?: PluginOptions,
): Promise<string> =>
transformKeepJsx(source, { fbtBase64: true, ...pluginOptions });
): Promise<string> => transformKeepJsx(source, pluginOptions);

export function withFbsImportStatement(code: string): string {
return `import { fbs } from "fbtee";
Expand All @@ -75,14 +73,7 @@ export function withFbtImportStatement(code: string): string {
*/
export const jsCodeFbtCallSerializer = {
serialize(rawValue: string) {
const decoded = rawValue.replaceAll(
/(["'])__FBT__(.*?)__FBT__\1/gm,
(_match, _quote, body) => {
const json = Buffer.from(body, 'base64').toString('utf8');
return `/* ${SENTINEL} start */ ${json} /* ${SENTINEL} end */`;
},
);
return prettier.format(decoded, { parser: 'babel' });
return prettier.format(rawValue, { parser: 'babel' });
},

test(rawValue: unknown): rawValue is string {
Expand Down Expand Up @@ -338,21 +329,3 @@ export function testSection(
});
});
}

export function testCase(
name: string,
plugins: Array<PluginItem>,
testData: TestCases,
options: Options,
) {
describe(name, () =>
testSection(
testData,
(source: string) =>
babel.transformSync(source, {
plugins,
})?.code || '',
options,
),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,28 @@

exports[`Test declarative (jsx) <fbs> syntax translation should convert a common string 1`] = `
import { fbs } from "fbtee";
const fbsCommonElem = fbs._(
/* __FBT__ start */ {
jsfbt: { m: [], t: { desc: "Button to post a comment", text: "Post" } },
project: "",
} /* __FBT__ end */
);
const fbsCommonElem = fbs._("Post", null, {
hk: "2y9OIZ",
});
`;
exports[`Test declarative (jsx) <fbs> syntax translation should convert a simple string 1`] = `
import { fbs } from "fbtee";
const fbsElem = fbs._(
/* __FBT__ start */ {
jsfbt: { m: [], t: { desc: "str_description", text: "a simple string" } },
project: "",
} /* __FBT__ end */
);
const fbsElem = fbs._("a simple string", null, {
hk: "1jd7FI",
});
`;
exports[`Test declarative (jsx) <fbs> syntax translation should convert a string with a parameter 1`] = `
import { fbs } from "fbtee";
const fbsElem = fbs._(
/* __FBT__ start */ {
jsfbt: {
m: [],
t: { desc: "str_description", text: "a string with a {param name}" },
},
project: "",
} /* __FBT__ end */,
[fbs._param("param name", parameter)]
"a string with a {param name}",
[fbs._param("param name", parameter)],
{
hk: "46D2WH",
}
);
`;
Expand All @@ -41,58 +32,46 @@ exports[`Test declarative (jsx) <fbs> syntax translation should handle <fbs:enum
import { fbs } from "fbtee";
import aEnum from "Test$FbtEnum";
var x = fbs._(
/* __FBT__ start */ {
jsfbt: {
m: [null],
t: {
id1: { desc: "enums!", text: "Click to see groups" },
id2: { desc: "enums!", text: "Click to see photos" },
id3: { desc: "enums!", text: "Click to see videos" },
},
},
project: "",
} /* __FBT__ end */,
[fbs._enum(id, aEnum)]
{
id1: "Click to see groups",
id2: "Click to see photos",
id3: "Click to see videos",
},
[fbs._enum(id, aEnum)],
{
hk: "3SHnwE",
}
);
`;
exports[`Test functional fbs() syntax translation should convert a common string 1`] = `
import { fbs } from "fbtee";
const fbsCommonCall = fbs._(
/* __FBT__ start */ {
jsfbt: { m: [], t: { desc: "Button to post a comment", text: "Post" } },
project: "",
} /* __FBT__ end */
);
const fbsCommonCall = fbs._("Post", null, {
hk: "2y9OIZ",
});
`;
exports[`Test functional fbs() syntax translation should convert a simple string 1`] = `
import { fbs } from "fbtee";
const fbsCall = fbs._(
/* __FBT__ start */ {
jsfbt: { m: [], t: { desc: "str_description", text: "a simple string" } },
project: "",
} /* __FBT__ end */
);
const fbsCall = fbs._("a simple string", null, {
hk: "1jd7FI",
});
`;
exports[`Test functional fbs() syntax translation should convert a string with a gender parameter 1`] = `
import { fbs } from "fbtee";
import IntlVariations from "IntlVariations";
const fbsCall = fbs._(
/* __FBT__ start */ {
jsfbt: {
m: [{ token: "param name", type: 1 }],
t: {
"*": { desc: "str_description", text: "a string with a {param name}" },
},
},
project: "",
} /* __FBT__ end */,
[fbs._param("param name", parameter, [1, IntlVariations.GENDER_MALE])]
{
"*": "a string with a {param name}",
},
[fbs._param("param name", parameter, [1, IntlVariations.GENDER_MALE])],
{
hk: "3lgixW",
}
);
`;
Expand All @@ -101,18 +80,15 @@ exports[`Test functional fbs() syntax translation should handle fbs.enum 1`] = `
import { fbs } from "fbtee";
import aEnum from "Test$FbtEnum";
var x = fbs._(
/* __FBT__ start */ {
jsfbt: {
m: [null],
t: {
id1: { desc: "enums!", text: "Click to see groups" },
id2: { desc: "enums!", text: "Click to see photos" },
id3: { desc: "enums!", text: "Click to see videos" },
},
},
project: "",
} /* __FBT__ end */,
[fbs._enum(id, aEnum)]
{
id1: "Click to see groups",
id2: "Click to see photos",
id3: "Click to see videos",
},
[fbs._enum(id, aEnum)],
{
hk: "3SHnwE",
}
);
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,115 +3,98 @@
exports[`Test double-lined params should remove the new line for param names that are two lines 1`] = `
import { fbt } from "fbtee";
fbt._(
/* __FBT__ start */ {
jsfbt: { m: [], t: { desc: "d", text: "{two lines} test" } },
project: "",
} /* __FBT__ end */,
"{two lines} test",
[
fbt._param(
"two lines",
/*#__PURE__*/ React.createElement(
"b",
null,
fbt._(
/* __FBT__ start */ {
jsfbt: { m: [], t: { desc: "test", text: "simple" } },
project: "",
} /* __FBT__ end */
)
fbt._("simple", null, {
hk: "2pjKFw",
})
)
),
]
],
{
hk: "2xRGl8",
}
);
`;

exports[`fbt variable binding detection should handle commonJS require() 1`] = `
const fbt = require("fbtee");
fbt._(
/* __FBT__ start */ {
jsfbt: { m: [], t: { desc: "Bar", text: "Foo" } },
project: "",
} /* __FBT__ end */
);
fbt._("Foo", null, {
hk: "3ktBJ2",
});
`;

exports[`fbt variable binding detection using ES6 should handle fbt default export 1`] = `
import fbt from "fbtee";
fbt._(
/* __FBT__ start */ {
jsfbt: { m: [], t: { desc: "Bar", text: "Foo" } },
project: "",
} /* __FBT__ end */
);
fbt._("Foo", null, {
hk: "3ktBJ2",
});
`;

exports[`fbt variable binding detection using ES6 should handle the named fbt export 1`] = `
import { fbt } from "fbtee";
fbt._(
/* __FBT__ start */ {
jsfbt: { m: [], t: { desc: "Bar", text: "Foo" } },
project: "",
} /* __FBT__ end */
);
fbt._("Foo", null, {
hk: "3ktBJ2",
});
`;

exports[`fbt() API: using FBT subject should accept "subject" as a parameter 1`] = `
import { fbt } from "fbtee";
fbt._(
/* __FBT__ start */ {
jsfbt: {
m: [{ token: "__subject__", type: 1 }],
t: { "*": { desc: "Bar", text: "Foo" } },
},
project: "",
} /* __FBT__ end */,
[fbt._subject(foo)]
{
"*": "Foo",
},
[fbt._subject(foo)],
{
hk: "7I4k2",
}
);
`;

exports[`fbt() API: using FBT subject with string templates should accept "subject" as a parameter 1`] = `
import { fbt } from "fbtee";
fbt._(
/* __FBT__ start */ {
jsfbt: {
m: [{ token: "__subject__", type: 1 }],
t: { "*": { desc: "Bar", text: "Foo" } },
},
project: "",
} /* __FBT__ end */,
[fbt._subject(foo)]
{
"*": "Foo",
},
[fbt._subject(foo)],
{
hk: "7I4k2",
}
);
`;

exports[`fragments inside of <fbt:param> 1`] = `
import { fbt } from "fbtee";
fbt._(
/* __FBT__ start */ {
jsfbt: { m: [], t: { desc: "d", text: "{param} test" } },
project: "",
} /* __FBT__ end */,
"{param} test",
[
fbt._param(
"param",
/*#__PURE__*/ React.createElement(
React.Fragment,
null,
/*#__PURE__*/ React.createElement("b", null, "Test"),
fbt._(
/* __FBT__ start */ {
jsfbt: { m: [], t: { desc: "test", text: "simple" } },
project: "",
} /* __FBT__ end */
)
fbt._("simple", null, {
hk: "2pjKFw",
})
)
),
]
],
{
hk: "154Qct",
}
);
`;
Loading

0 comments on commit 6ca460e

Please sign in to comment.