Skip to content

Commit

Permalink
feat: support of js fixtures (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
sraone authored and mlrawlings committed Nov 22, 2019
1 parent fb33dd8 commit 4341bf5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ export function findComponentFixtures(
const componentName = inferName(componentPath);
const fixturesPath = path.join(path.dirname(componentPath), fixtureDir);
const fixtures = glob
.sync("*.{json,marko}", { cwd: fixturesPath })
.sync("*.{json,marko,js}", { cwd: fixturesPath })
.map((fixtureRelativePath: string) => {
const fixturePath = path.join(fixturesPath, fixtureRelativePath);
const fixtureExtension = path.extname(fixturePath);
const fixtureName = path
.basename(fixturePath)
.slice(0, -1 * fixtureExtension.length);
const renderFn =
fixtureExtension === ".json"
[".json", ".js"].indexOf(fixtureExtension) !== -1
? () => render(require(componentPath), require(fixturePath))
: () => render(require(fixturePath));
return {
Expand All @@ -56,13 +56,10 @@ export function findComponentFixtures(
render: renderFn
};
})
.reduce(
(lookup, current) => {
lookup[current.name] = current;
return lookup;
},
{} as ComponentFixtures["fixtures"]
);
.reduce((lookup, current) => {
lookup[current.name] = current;
return lookup;
}, {} as ComponentFixtures["fixtures"]);
if (Object.keys(fixtures).length) {
return {
get component() {
Expand Down
8 changes: 8 additions & 0 deletions test/components/container/fixtures/data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div
class="container"
>
<p>
Hello
</p>
Async content
</div>
4 changes: 4 additions & 0 deletions test/components/container/fixtures/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
renderBody: out => out.w("<p>Hello</p>"),
model: Promise.resolve("Async content")
};
9 changes: 9 additions & 0 deletions test/components/container/index.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div.container>
<${input.renderBody}/>

<await(input.model)>
<@then|model|>
${model}
</@then>
</await>
</div>
19 changes: 19 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ describe("findProjectFixtures", () => {
test("finds fixtures", () => {
expect(findProjectFixtures(__dirname)).toMatchInlineSnapshot(`
Array [
Object {
"component": [Component "(cwd)/test/components/container/index.marko"],
"fixtures": Object {
"data": Object {
"ext": ".js",
"fixture": Object {
"model": Promise {},
"renderBody": [Function],
},
"name": "data",
"path": "(cwd)/test/components/container/fixtures/data.js",
"render": [Function],
"toString": [Function],
},
},
"fixturesPath": "(cwd)/test/components/container/fixtures",
"name": "container",
"path": "(cwd)/test/components/container/index.marko",
},
Object {
"component": [Component "(cwd)/test/components/hello/index.marko"],
"fixtures": Object {
Expand Down

0 comments on commit 4341bf5

Please sign in to comment.