diff --git a/packages/cli/test/cases/loaders-build.plugins.resource-page/greenwood.config.js b/packages/cli/test/cases/loaders-build.plugins.resource-page/greenwood.config.js index aa5b457b2..d4d60fbc2 100644 --- a/packages/cli/test/cases/loaders-build.plugins.resource-page/greenwood.config.js +++ b/packages/cli/test/cases/loaders-build.plugins.resource-page/greenwood.config.js @@ -7,7 +7,7 @@ class FooResource { this.servePage = options.servePage; this.extensions = ['foo']; - this.contentType = 'text/javascript'; + this.contentType = 'text/html'; } async shouldServe(url) { @@ -15,9 +15,7 @@ class FooResource { } async serve(url) { - let body = await fs.readFile(url, 'utf-8'); - - body = body.replace(/interface (.*){(.*)}/s, ''); + const body = await fs.readFile(url, 'utf-8'); return new Response(body, { headers: new Headers({ @@ -44,7 +42,7 @@ class BarResource { async serve(url) { let body = await fs.readFile(url, 'utf-8'); - body = body.replace(/interface (.*){(.*)}/s, ''); + body = body.replace(/interface (.*){(.*)}/, ''); return new Response(body, { headers: new Headers({ diff --git a/packages/cli/test/cases/loaders-build.plugins.resource-page/loaders-build.plugins.resource-page.spec.js b/packages/cli/test/cases/loaders-build.plugins.resource-page/loaders-build.plugins.resource-page.spec.js index 9e9beba70..01b15eab7 100644 --- a/packages/cli/test/cases/loaders-build.plugins.resource-page/loaders-build.plugins.resource-page.spec.js +++ b/packages/cli/test/cases/loaders-build.plugins.resource-page/loaders-build.plugins.resource-page.spec.js @@ -1,9 +1,9 @@ /* * Use Case - * Run Greenwood with a custom resource plugin and default workspace with a custom page format. + * Run Greenwood with a custom resource plugin and default workspace with a custom page formats. * * User Result - * Should generate a bare bones Greenwood build with expected custom file (.foo) behavior. + * Should generate a bare bones Greenwood build with expected custom page format behaviors. * * User Command * greenwood build @@ -32,6 +32,8 @@ * Custom Workspace * src/ * pages/ + * api + * greeting.bar * about.foo * contact.bar * index.html @@ -47,7 +49,7 @@ import { fileURLToPath, URL } from 'url'; const expect = chai.expect; describe('Build Greenwood With: ', function() { - const LABEL = 'Custom FooResource Plugin and Default Workspace'; + const LABEL = 'Custom Static and Dynamic Page Loaders'; const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js'); const outputPath = fileURLToPath(new URL('.', import.meta.url)); let runner; @@ -115,7 +117,21 @@ describe('Build Greenwood With: ', function() { }); }); - // TODO test an API route with a dynamic format plugin + describe('Custom Format Dynamic API Route', function() { + let handler; + + before(async function() { + handler = (await import(new URL('./public/api/greeting.js', import.meta.url))).handler; + }); + + it('should have the expected output from the API route', async function() { + const response = await handler(new Request(new URL('http://localhost:8080/api/greeting'))); + const data = await response.json(); + + expect(data.message).to.equal('Hello World!!!'); + }); + + }); }); after(function() { diff --git a/packages/cli/test/cases/loaders-build.plugins.resource-page/src/pages/api/greeting.bar b/packages/cli/test/cases/loaders-build.plugins.resource-page/src/pages/api/greeting.bar new file mode 100644 index 000000000..2b58205ef --- /dev/null +++ b/packages/cli/test/cases/loaders-build.plugins.resource-page/src/pages/api/greeting.bar @@ -0,0 +1,13 @@ +interface User { } + +export async function handler(request) { + const params = new URLSearchParams(request.url.slice(request.url.indexOf('?'))); + const name = params.has('name') ? params.get('name') : 'World'; + const body = { message: `Hello ${name}!!!` }; + + return new Response(JSON.stringify(body), { + headers: new Headers({ + 'Content-Type': 'application/json' + }) + }); +} \ No newline at end of file diff --git a/packages/cli/test/cases/loaders-build.plugins.resource-page/src/pages/contact.bar b/packages/cli/test/cases/loaders-build.plugins.resource-page/src/pages/contact.bar index b72e04fb0..ae8d01f4c 100644 --- a/packages/cli/test/cases/loaders-build.plugins.resource-page/src/pages/contact.bar +++ b/packages/cli/test/cases/loaders-build.plugins.resource-page/src/pages/contact.bar @@ -1,3 +1,5 @@ +interface User { } + export default class ContactPage extends HTMLElement { connectedCallback() { this.innerHTML = `