Skip to content

Commit

Permalink
test(define-router): handleApi and getApiConfig e2e tests (#1142)
Browse files Browse the repository at this point in the history
add api handling support to define-router spec and test the result

Co-authored-by: Tyler <[email protected]>
  • Loading branch information
tylersayshi and tylersayshi authored Jan 9, 2025
1 parent 4bd9d24 commit a837acc
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
20 changes: 20 additions & 0 deletions e2e/define-router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,25 @@ for (const mode of ['DEV', 'PRD'] as const) {
await page.goto(`http://localhost:${port}/foo`);
await expect(page.getByTestId('foo-title')).toHaveText('Foo');
});

test('api hi', async ({ page }) => {
await page.goto(`http://localhost:${port}/api/hi`);
await expect(page.getByText('hello world!')).toBeVisible();
});

test('api hi.txt', async ({ page }) => {
await page.goto(`http://localhost:${port}/api/hi.txt`);
await expect(page.getByText('hello from a text file!')).toBeVisible();
});

test('api empty', async ({ page }) => {
await page.goto(`http://localhost:${port}/api/empty`);
await expect(await page.innerHTML('body')).toBe('');
});

test('not found', async ({ page }) => {
await page.goto(`http://localhost:${port}/does-not-exist.txt`);
await expect(page.getByText('Not Found')).toBeVisible();
});
});
}
1 change: 1 addition & 0 deletions e2e/fixtures/define-router/private/hi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello from a text file!
56 changes: 56 additions & 0 deletions e2e/fixtures/define-router/src/entries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Slot, Children } from 'waku/minimal/client';
import Layout from './routes/layout.js';
import Page from './routes/page.js';
import FooPage from './routes/foo/page.js';
import { readFile } from 'node:fs/promises';

const STATIC_PATHS = ['/', '/foo'];
const PATH_PAGE: Record<string, ReactNode> = {
Expand Down Expand Up @@ -59,6 +60,61 @@ const router: ReturnType<typeof defineRouter> = defineRouter({
},
};
},
getApiConfig: async () => [
{
path: [
{ type: 'literal', name: 'api' },
{ type: 'literal', name: 'hi' },
],
isStatic: true,
},
{
path: [
{ type: 'literal', name: 'api' },
{ type: 'literal', name: 'hi.txt' },
],
isStatic: false,
},
{
path: [
{ type: 'literal', name: 'api' },
{ type: 'literal', name: 'empty' },
],
isStatic: true,
},
],
handleApi: async (path) => {
if (path === '/api/hi.txt') {
const hiTxt = await readFile('./private/hi.txt');

return {
status: 200,
body: new ReadableStream({
start(controller) {
controller.enqueue(hiTxt);
controller.close();
},
}),
};
} else if (path === '/api/hi') {
return {
status: 200,
body: new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode('hello world!'));
controller.close();
},
}),
};
} else if (path === '/api/empty') {
return {
status: 200,
};
}
return {
status: 404,
};
},
});

export default router;

0 comments on commit a837acc

Please sign in to comment.