astro/packages/astro/test/astro-dynamic.test.js
Matthew Phillips fc52321a88
Consolidate hydration scripts into just one (#3571)
* Remove redundant hydration scripts

* Prebuild the island JS

* Fix build

* Updates to tests

* Update more references

* Custom element test now has two classic scripts

* Account for non-default exports

* Restructure hydration directives

* Move nested logic into the island component

* Remove try/catch
2022-06-15 08:50:05 -04:00

80 lines
2.2 KiB
JavaScript

import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
describe('Dynamic components', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-dynamic/',
});
await fixture.build();
});
it('Loads packages that only run code in client', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('script').length).to.eq(1);
});
it('Loads pages using client:media hydrator', async () => {
const root = new URL('http://example.com/media/index.html');
const html = await fixture.readFile('/media/index.html');
const $ = cheerio.load(html);
// test 1: static value rendered
expect($('script').length).to.equal(1);
});
it('Loads pages using client:only hydrator', async () => {
const html = await fixture.readFile('/client-only/index.html');
const $ = cheerio.load(html);
// test 1: <astro-island> is empty.
expect($('astro-island').html()).to.equal('');
// test 2: component url
const href = $('astro-island').attr('component-url');
expect(href).to.include(`/entry`);
});
});
describe('Dynamic components subpath', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
site: 'https://site.com',
base: '/blog',
root: './fixtures/astro-dynamic/',
});
await fixture.build();
});
it('Loads packages that only run code in client', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('script').length).to.eq(1);
});
it('Loads pages using client:media hydrator', async () => {
const html = await fixture.readFile('/media/index.html');
const $ = cheerio.load(html);
// test 1: static value rendered
expect($('script').length).to.equal(1);
});
it('Loads pages using client:only hydrator', async () => {
const html = await fixture.readFile('/client-only/index.html');
const $ = cheerio.load(html);
// test 1: <astro-island> is empty.
expect($('astro-island').html()).to.equal('');
// test 2: has component url
const attr = $('astro-island').attr('component-url');
expect(attr).to.include(`blog/entry`);
});
});