Skip to content

Commit

Permalink
refactor(vue-script-setup-converter): change to inline snapshot (#55)
Browse files Browse the repository at this point in the history
* test: change to inline snapshot

* test: change to inline snapshot

* test: change to inline snapshot

* test: change to inline snapshot

* test: change to inline snapshot
  • Loading branch information
wattanx authored May 10, 2024
1 parent 0537de9 commit 1ab6a17
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 162 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { expect, test } from "vitest";
import { CallExpression, ScriptTarget, SyntaxKind, Project } from "ts-morph";
import { parse } from "@vue/compiler-sfc";
import prettier from "prettier";
import parserTypeScript from "prettier/parser-typescript";
import { getNodeByKind } from "../helpers/node";
import { convertEmits } from "./emitsConverter";

Expand All @@ -24,12 +22,7 @@ const parseScript = (input: string, lang: "js" | "ts" = "js") => {

const emits = convertEmits(callexpression as CallExpression, lang);

const formatedText = prettier.format(emits, {
parser: "typescript",
plugins: [parserTypeScript],
});

return formatedText;
return emits;
};

const source = `<script>
Expand All @@ -52,10 +45,9 @@ export default defineComponent({
test("defineEmits", () => {
const output = parseScript(source, "js");

const expected = `const emit = defineEmits({ change: (value) => true });
`;

expect(output).toBe(expected);
expect(output).toMatchInlineSnapshot(
`"const emit = defineEmits({change: (value) => true});"`
);
});

test("typed defineEmits", () => {
Expand All @@ -77,9 +69,9 @@ export default defineComponent({
</script>`;
const output = parseScript(source, "ts");

const expected = `const emit = defineEmits<{ (e: "change", value: number): void }>();
`;
expect(output).toBe(expected);
expect(output).toMatchInlineSnapshot(
`"const emit = defineEmits<{(e: 'change', value: number): void}>();"`
);
});

test("string array", () => {
Expand All @@ -101,7 +93,9 @@ export default defineComponent({

const expected = `const emit = defineEmits(["change"]);
`;
expect(output).toBe(expected);
expect(output).toMatchInlineSnapshot(
`"const emit = defineEmits(['change']);"`
);
});

test("validation", () => {
Expand All @@ -125,11 +119,9 @@ export default defineComponent({
</script>`;
const output = parseScript(source, "ts");

const expected = `const emit = defineEmits({
change: (value: number) => {
return value !== 0;
},
});
`;
expect(output).toBe(expected);
expect(output).toMatchInlineSnapshot(`
"const emit = defineEmits({change: (value: number) => {
return value !== 0;
}});"
`);
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { expect, describe, it } from "vitest";
import { ScriptTarget, Project } from "ts-morph";
import { parse } from "@vue/compiler-sfc";
import prettier from "prettier";
import parserTypeScript from "prettier/parser-typescript";
import { convertImportDeclaration } from "./importDeclarationConverter";

const parseScript = (input: string) => {
Expand All @@ -20,12 +18,7 @@ const parseScript = (input: string) => {
const sourceFile = project.createSourceFile("s.tsx", script?.content ?? "");
const convertedImportDeclarationText = convertImportDeclaration(sourceFile);

const formatedText = prettier.format(convertedImportDeclarationText, {
parser: "typescript",
plugins: [parserTypeScript],
});

return formatedText;
return convertedImportDeclarationText;
};

describe("convertImportDeclaration", () => {
Expand All @@ -40,9 +33,8 @@ describe("convertImportDeclaration", () => {

it("returns import declaration text removed defineComponent", () => {
const output = parseScript(source);
const expected = 'import { ref } from "vue";\n';

expect(output).toBe(expected);
expect(output).toMatchInlineSnapshot(`"import { ref } from 'vue';"`);
});
});

Expand All @@ -57,9 +49,8 @@ describe("convertImportDeclaration", () => {

it("returns blank", () => {
const output = parseScript(source);
const expected = "";

expect(output).toBe(expected);
expect(output).toBe("");
});
});

Expand All @@ -74,9 +65,8 @@ describe("convertImportDeclaration", () => {

it("returns import declaration text removed defineNuxtComponent", () => {
const output = parseScript(source);
const expected = 'import { ref } from "#imports";\n';

expect(output).toBe(expected);
expect(output).toMatchInlineSnapshot(`"import { ref } from '#imports';"`);
});
});

Expand All @@ -91,9 +81,8 @@ describe("convertImportDeclaration", () => {

it("returns blank", () => {
const output = parseScript(source);
const expected = "";

expect(output).toBe(expected);
expect(output).toBe("");
});
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { expect, describe, it } from "vitest";
import { CallExpression, ScriptTarget, SyntaxKind, Project } from "ts-morph";
import { parse } from "@vue/compiler-sfc";
import prettier from "prettier";
import parserTypeScript from "prettier/parser-typescript";
import { getNodeByKind } from "../helpers/node";
import { convertPageMeta } from "./pageMetaConverter";

Expand All @@ -24,12 +22,7 @@ const parseScript = (input: string, lang: "js" | "ts" = "js") => {

const pageMeta = convertPageMeta(callExpression as CallExpression, lang);

const formatedText = prettier.format(pageMeta, {
parser: "typescript",
plugins: [parserTypeScript],
});

return formatedText;
return pageMeta;
};

describe("convertPageMeta", () => {
Expand All @@ -47,14 +40,11 @@ describe("convertPageMeta", () => {
it("returns text including definePageMeta", () => {
const output = parseScript(source);

const expected = `definePageMeta({
name: "HelloWorld",
layout: "test-layout",
middleware: "test-middleware",
});
`;

expect(output).toBe(expected);
expect(output).toMatchInlineSnapshot(`
"definePageMeta({
name: 'HelloWorld',layout: 'test-layout',middleware: 'test-middleware'
});"
`);
});
});

Expand All @@ -72,14 +62,11 @@ describe("convertPageMeta", () => {
it("returns text including definePageMeta", () => {
const output = parseScript(source);

const expected = `definePageMeta({
name: "HelloWorld",
layout: "test-layout",
middleware: ["test-middleware-1", "test-middleware-2"],
});
`;

expect(output).toBe(expected);
expect(output).toMatchInlineSnapshot(`
"definePageMeta({
name: 'HelloWorld',layout: 'test-layout',middleware: ['test-middleware-1', 'test-middleware-2']
});"
`);
});
});
});
Loading

0 comments on commit 1ab6a17

Please sign in to comment.