Skip to content

Commit

Permalink
add test cases for custom API routes page format
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Aug 21, 2024
1 parent 69724eb commit 93c18d5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ class FooResource {
this.servePage = options.servePage;

this.extensions = ['foo'];
this.contentType = 'text/javascript';
this.contentType = 'text/html';
}

async shouldServe(url) {
return url.pathname.split('.').pop() === this.extensions[0] && this.servePage;
}

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({
Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -32,6 +32,8 @@
* Custom Workspace
* src/
* pages/
* api
* greeting.bar
* about.foo
* contact.bar
* index.html
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
})
});
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
interface User { }

export default class ContactPage extends HTMLElement {
connectedCallback() {
this.innerHTML = `
Expand Down

0 comments on commit 93c18d5

Please sign in to comment.