0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/node/test/locals.test.js
Emanuele Stoppa 8e2923cc62
feat: expose locals to render api and from requests in dev mode (#7385)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: wrapperup <wrapperup4@gmail.com>
2023-06-21 13:07:16 +01:00

53 lines
1.2 KiB
JavaScript

import nodejs from '../dist/index.js';
import { loadFixture, createRequestAndResponse } from './test-utils.js';
import { expect } from 'chai';
describe('API routes', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/locals/',
output: 'server',
adapter: nodejs({ mode: 'middleware' }),
});
await fixture.build();
});
it('Can render locals in page', async () => {
const { handler } = await import('./fixtures/locals/dist/server/entry.mjs');
let { req, res, text } = createRequestAndResponse({
method: 'POST',
url: '/foo',
});
let locals = { foo: 'bar' };
handler(req, res, () => {}, locals);
req.send();
let html = await text();
expect(html).to.contain('<h1>bar</h1>');
});
it('Can access locals in API', async () => {
const { handler } = await import('./fixtures/locals/dist/server/entry.mjs');
let { req, res, done } = createRequestAndResponse({
method: 'POST',
url: '/api',
});
let locals = { foo: 'bar' };
handler(req, res, () => {}, locals);
req.send();
let [buffer] = await done;
let json = JSON.parse(buffer.toString('utf-8'));
expect(json.foo).to.equal('bar');
});
});