Skip to content

Commit

Permalink
tests: fix astro test adapter not passing locals, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wrapperup authored and ematipico committed Jun 13, 2023
1 parent dead4c6 commit 678c62e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { APIContext, ComponentInstance, Params, Props, RouteData } from '../../@types/astro';
import { renderPage as runtimeRenderPage } from '../../runtime/server/index.js';
import { render, renderPage as runtimeRenderPage } from '../../runtime/server/index.js';
import { attachToResponse } from '../cookies/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import type { LogOptions } from '../logger/core.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/fixtures/ssr-locals/src/pages/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export async function post({ locals }) {
export async function get({ locals }) {
let out = { ...locals };

return new Response(JSON.stringify(out), {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
const { foo } = Astro.locals;
---
<h1>{ foo }</h1>
<h1 id="foo">{ foo }</h1>
16 changes: 8 additions & 8 deletions packages/astro/test/ssr-locals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';

describe('SSR Environment Variables', () => {
describe('SSR Astro.locals from server', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

Expand All @@ -19,20 +19,20 @@ describe('SSR Environment Variables', () => {
it('Can access Astro.locals in page', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/foo');
const route = match
const response = await app.render(request);
const locals = { foo: 'bar' };
const response = await app.render(request, undefined, locals);
const html = await response.text();

const $ = cheerio.load(html);
expect($('#ssr').text()).to.equal('true');
expect($('#foo').text()).to.equal('bar');
});

it('Can access Astro.locals in API', async () => {
it('Can access Astro.locals in api context', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/api');
const response = await app.render(request);
const locals = { foo: 'bar' };
const response = await app.render(request, undefined, locals);
expect(response.status).to.equal(200);
expect(response.headers.get('Content-Type')).to.equal('application/json;charset=utf-8');
expect(response.headers.get('Content-Length')).to.not.be.empty;
const body = await response.json();

expect(body.foo).to.equal('bar');
Expand Down
5 changes: 2 additions & 3 deletions packages/astro/test/test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ export default function ({ provideAddress = true, extendAdapter } = { provideAdd
this.#manifest = manifest;
}
async render(request, routeData) {
async render(request, routeData, locals) {
const url = new URL(request.url);
if(this.#manifest.assets.has(url.pathname)) {
const filePath = new URL('../client/' + this.removeBase(url.pathname), import.meta.url);
const data = await fs.promises.readFile(filePath);
return new Response(data);
}
Reflect.set(request, Symbol.for('astro.locals'), {});
${provideAddress ? `request[Symbol.for('astro.clientAddress')] = '0.0.0.0';` : ''}
return super.render(request, routeData);
return super.render(request, routeData, locals);
}
}
Expand Down

0 comments on commit 678c62e

Please sign in to comment.