Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of testSection util for snapshots #19

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 4 additions & 82 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 Down Expand Up @@ -35,10 +35,7 @@ export function snapshotTransform(
return transform(source, { fbtBase64: true, ...pluginOptions });
}

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

export function withFbsImportStatement(code: string): string {
return `import { fbs } from "fbtee";
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -274,44 +252,6 @@ ${indent(excerptDiffFromReceived)}
}
}

export function testSectionAsync(
testData: TestCases,
transform: (
source: string,
options?: Record<string, unknown>,
) => Promise<string>,
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,
Expand All @@ -338,21 +278,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 @@ -998,7 +998,7 @@ fbt._(

`;

exports[`Test jsx auto-wrapping of implicit parameters should wrap a string next to an explicit <fbt:param> that has a implicit <fbt:param> within it 1`] = `
exports[`Test jsx auto-wrapping of implicit parameters should wrap a string next to an explicit <fbt:param> that has an implicit <fbt:param> within it 1`] = `
import { fbt } from "fbtee";
fbt._(
/* __FBT__ start */ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 */ {
Expand Down Expand Up @@ -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 */ {
Expand Down
Loading
Loading