Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a basic unittest. #179

Merged
merged 5 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

1.0.3 (2024-01-15)
------------------

* Added a DOCTYPE so we're not in quircks mode.
* Added a bug related to loading image assets.


1.0.2 (2024-01-15)
------------------

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curveball/browser",
"version": "1.0.2",
"version": "1.0.3",
"description": "Automatic API browser generator. A middleware that turns your JSON responses into HTML if accessed by a browser.",
"type": "module",
"exports": "./dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/html-index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default async function generateHtmlIndex(ctx: Context, options: Options)
}

ctx.response.type = 'text/html; charset=utf-8';
ctx.response.body = ReactDOMServer.renderToString(
ctx.response.body = '<!DOCTYPE html>\n' + ReactDOMServer.renderToString(
<App
resourceState={state}
options={options}
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Middleware, invokeMiddlewares } from '@curveball/kernel';
import generateHtmlIndex from './html-index.js';
import { NavigationLinkMap, Options } from './types.js';
import staticMw from '@curveball/static';
import { fileURLToPath } from 'node:url';
import { join } from 'node:path';

export const supportedContentTypes = [
'application/json',
Expand Down Expand Up @@ -107,10 +109,12 @@ const defaultNavigationLinks: NavigationLinkMap = {

export { Options } from './types.js';

const assetsPath = join(fileURLToPath(new URL(import.meta.url)),'../../assets');

export default function browser(options?: Partial<Options>): Middleware {

const stat = staticMw({
staticDir: __dirname + '/../assets',
staticDir: assetsPath,
pathPrefix: '/_hal-browser/assets',
maxAge: 3600,
});
Expand Down
28 changes: 28 additions & 0 deletions test/json-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Application } from '@curveball/kernel';
import browser from '../src/index.js';
import { expect } from 'chai';

describe('Browser middleware integration test', () => {

it('should render a HTML page when an `Accept` header with text/html is emitted', async() => {

const app = new Application();
const mw = browser();
app.use(mw);

app.use( ctx => {
ctx.response.body = { hello: 'world' };
ctx.response.type = 'application/json';
});

const resp = await app.subRequest('GET', '/', {
Accept: 'text/html'
});

expect(resp.status).to.equal(200);
expect(resp.is('html')).to.equal(true);
expect(resp.body).to.contain('<html><head>');

});

});
5 changes: 0 additions & 5 deletions test/test.ts

This file was deleted.