From 4341bf5eba07cee80ef1dcf3a1bc33b90c2d3601 Mon Sep 17 00:00:00 2001 From: Sravan Kalluri Date: Fri, 22 Nov 2019 12:37:20 -0800 Subject: [PATCH] feat: support of js fixtures (#2) --- src/index.ts | 15 ++++++--------- test/components/container/fixtures/data.html | 8 ++++++++ test/components/container/fixtures/data.js | 4 ++++ test/components/container/index.marko | 9 +++++++++ test/index.test.ts | 19 +++++++++++++++++++ 5 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 test/components/container/fixtures/data.html create mode 100644 test/components/container/fixtures/data.js create mode 100644 test/components/container/index.marko diff --git a/src/index.ts b/src/index.ts index bfac389..2c10382 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,7 +31,7 @@ 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); @@ -39,7 +39,7 @@ export function findComponentFixtures( .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 { @@ -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() { diff --git a/test/components/container/fixtures/data.html b/test/components/container/fixtures/data.html new file mode 100644 index 0000000..ce3e0b6 --- /dev/null +++ b/test/components/container/fixtures/data.html @@ -0,0 +1,8 @@ +
+

+ Hello +

+ Async content +
\ No newline at end of file diff --git a/test/components/container/fixtures/data.js b/test/components/container/fixtures/data.js new file mode 100644 index 0000000..3b2c74a --- /dev/null +++ b/test/components/container/fixtures/data.js @@ -0,0 +1,4 @@ +module.exports = { + renderBody: out => out.w("

Hello

"), + model: Promise.resolve("Async content") +}; diff --git a/test/components/container/index.marko b/test/components/container/index.marko new file mode 100644 index 0000000..a35cf03 --- /dev/null +++ b/test/components/container/index.marko @@ -0,0 +1,9 @@ + + <${input.renderBody}/> + + + <@then|model|> + ${model} + + + diff --git a/test/index.test.ts b/test/index.test.ts index 0585497..c9a121e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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 {