astro/packages/astro/test/astro-client-only.test.js
Matthew Phillips 8f9463e07f
Fixes client:only CSS in Svelte components (#4782)
* Fixes client:only CSS in Svelte components

* Add changeset
2022-09-17 12:32:35 -04:00

96 lines
2.9 KiB
JavaScript

import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import { loadFixture } from './test-utils.js';
describe('Client only components', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-client-only/',
});
await fixture.build();
});
it('Loads pages using client:only hydrator', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerioLoad(html);
// test 1: <astro-island> is empty
expect($('astro-island').html()).to.equal('');
// test 2: svelte renderer is on the page
expect($('astro-island').attr('renderer-url')).to.be.ok;
});
it('Adds the CSS to the page', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerioLoad(html);
const href = $('link[rel=stylesheet]').attr('href');
const css = await fixture.readFile(href);
expect(css).to.match(/yellowgreen/, 'Svelte styles are added');
expect(css).to.match(/Courier New/, 'Global styles are added');
});
it('Adds the CSS to the page - standalone svelte component', async () => {
const html = await fixture.readFile('/persistent-counter-standalone/index.html');
const $ = cheerioLoad(html);
expect($('head link[rel=stylesheet]')).to.have.a.lengthOf(1);
const href = $('link[rel=stylesheet]').attr('href');
const css = await fixture.readFile(href);
expect(css).to.match(/tomato/, 'Svelte styles are added');
});
it('Includes CSS from components that use CSS modules', async () => {
const html = await fixture.readFile('/css-modules/index.html');
const $ = cheerioLoad(html);
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1);
});
it('Includes CSS from package components', async () => {
const html = await fixture.readFile('/pkg/index.html');
const $ = cheerioLoad(html);
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1);
});
});
describe('Client only components subpath', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
site: 'https://site.com',
base: '/blog',
root: './fixtures/astro-client-only/',
});
await fixture.build();
});
it('Loads pages using client:only hydrator', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerioLoad(html);
// test 1: <astro-island> is empty
expect($('astro-island').html()).to.equal('');
// test 2: svelte renderer is on the page
expect($('astro-island').attr('renderer-url')).to.be.ok;
});
it('Adds the CSS to the page', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerioLoad(html);
const href = $('link[rel=stylesheet]').attr('href');
const css = await fixture.readFile(href.replace(/\/blog/, ''));
expect(css).to.match(/yellowgreen/, 'Svelte styles are added');
expect(css).to.match(/Courier New/, 'Global styles are added');
});
});