Skip to content

Commit

Permalink
feat: add arc support
Browse files Browse the repository at this point in the history
  • Loading branch information
PrashantAshok committed Jun 5, 2023
1 parent e6b10e1 commit 2812abe
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/jest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { cleanup } from "@marko/testing-library";
import { findProjectFixtures, defaultNormalizer } from "./";
import { toMatchFile } from "jest-file-snapshot";
import { getArcFlag } from "./utils";

expect.extend({ toMatchFile });

Expand All @@ -15,8 +16,11 @@ export default function snapshotComponentFixtures(
Object.keys(component.fixtures).forEach(name => {
it(name, async () => {
const fixture = component.fixtures[name];
const fileExtension = getArcFlag(component.path); // this returns "[mobile]"
const replaceValue = fileExtension.length === 0 ? ".html" : `${fileExtension}.html`;

expect(await fixture.toString(normalize)).toMatchFile(
fixture.path.replace(fixture.ext, ".html")
fixture.path.replace(fixture.ext, replaceValue)
);
});
});
Expand Down
6 changes: 5 additions & 1 deletion src/mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "path";
import assert from "assert";
import { cleanup } from "@marko/testing-library";
import { findProjectFixtures, defaultNormalizer } from "./";
import { getArcFlag } from "./utils";

declare const before: (fn: () => any) => any;

Expand All @@ -17,8 +18,11 @@ export default function snapshotComponentFixtures(
Object.keys(component.fixtures).forEach(name => {
it(name, async () => {
const fixture = component.fixtures[name];
const fileExtension = getArcFlag(component.path); // this returns "[mobile]"
const replaceValue = fileExtension.length === 0 ? ".html" : `${fileExtension}.html`;

snapshot(
fixture.path.replace(fixture.ext, ".html"),
fixture.path.replace(fixture.ext, replaceValue),
await fixture.toString(normalize)
);
});
Expand Down
21 changes: 21 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from "path";

export function getArcFlag(filename: string) {
const start = filename.lastIndexOf(path.sep) + 1;
const leftDot = filename.indexOf(".", start);

const closeBracket = leftDot - 1;
if (filename[closeBracket] === "]") {
const openBracket = filename.lastIndexOf("[", closeBracket);
if (openBracket > start) {
// If we match a "]" before the extension and find a "[" before that,
// then we have an arc flag. Strip it off.
return (
filename.slice(openBracket, closeBracket + 1)
);
}
}

return '';
}

3 changes: 3 additions & 0 deletions test/components/hello/fixtures/data[mobile].html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Hello Arc File Data!
</div>
3 changes: 3 additions & 0 deletions test/components/hello/fixtures/template[mobile].html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Hello Template!
</div>
2 changes: 2 additions & 0 deletions test/components/hello/index[mobile].marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class {}
<div>Hello Arc File ${input.name}!</div>
26 changes: 26 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,32 @@ describe("findProjectFixtures", () => {
"name": "hello",
"path": "(cwd)/test/components/hello/index.marko",
},
Object {
"component": [Component "(cwd)/test/components/hello/index[mobile].marko"],
"fixtures": Object {
"data": Object {
"ext": ".json",
"fixture": Object {
"name": "Data",
},
"name": "data",
"path": "(cwd)/test/components/hello/fixtures/data.json",
"render": [Function],
"toString": [Function],
},
"template": Object {
"ext": ".marko",
"fixture": [Component "(cwd)/test/components/hello/fixtures/template.marko"],
"name": "template",
"path": "(cwd)/test/components/hello/fixtures/template.marko",
"render": [Function],
"toString": [Function],
},
},
"fixturesPath": "(cwd)/test/components/hello/fixtures",
"name": "hello",
"path": "(cwd)/test/components/hello/index[mobile].marko",
},
]
`);
});
Expand Down

0 comments on commit 2812abe

Please sign in to comment.