Skip to content

Commit

Permalink
test: Ensure that when we build with server islands that the baseURL …
Browse files Browse the repository at this point in the history
…is added
  • Loading branch information
Jacob-Roberts committed Nov 22, 2024
1 parent b41ced9 commit 8d4025b
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions packages/astro/test/server-islands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,61 @@ describe('Server islands', () => {
});
});

describe('SSR (change base URL)', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/server-islands/ssr',
adapter: testAdapter(),
integrations: [
{
name: 'change-base-url',
hooks: {
'astro:config:setup': async ({ updateConfig, command }) => {
if (command === 'build') {
updateConfig({
serverIslandDynamicBase: 'https://api.example.com',
});
}
},
},
},
],
});
});

describe('prod', () => {
before(async () => {
process.env.ASTRO_KEY = 'eKBaVEuI7YjfanEXHuJe/pwZKKt3LkAHeMxvTU7aR0M=';
await fixture.build();
});

after(async () => {
delete process.env.ASTRO_KEY;
});

it('server island script has base URL', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request);
const html = await response.text();

const $ = cheerio.load(html);
const serverIslandEl = $('h2#island');
assert.equal(serverIslandEl.length, 0);

const serverIslandScript = $('script[data-island-id]');
assert.equal(serverIslandScript.length, 1, 'has the island script');

assert.match(
serverIslandScript.text(),
/await fetch\('https:\/\/api.example.com\/_server-islands\//i,
);
});
});
});

describe('Hybrid mode', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
Expand Down Expand Up @@ -118,4 +173,52 @@ describe('Server islands', () => {
});
});
});

describe('Hybrid mode (change base URL)', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/server-islands/hybrid',
integrations: [
{
name: 'change-base-url',
hooks: {
'astro:config:setup': async ({ updateConfig, command }) => {
if (command === 'build') {
updateConfig({
serverIslandDynamicBase: 'https://api.example.com',
});
}
},
},
},
],
});
});

describe('build', () => {
before(async () => {
await fixture.build({
adapter: testAdapter(),
});
});

it('Omits the island HTML from the static HTML', async () => {
let html = await fixture.readFile('/client/index.html');

const $ = cheerio.load(html);
const serverIslandEl = $('h2#island');
assert.equal(serverIslandEl.length, 0);

const serverIslandScript = $('script[data-island-id]');
assert.equal(serverIslandScript.length, 1, 'has the island script');

assert.match(
serverIslandScript.text(),
/await fetch\('https:\/\/api.example.com\/_server-islands\//i,
);
});
});
});
});

0 comments on commit 8d4025b

Please sign in to comment.