2023-01-30 11:55:10 -05:00
|
|
|
import mdx from '@astrojs/mdx';
|
|
|
|
|
2024-01-31 03:37:34 -05:00
|
|
|
import * as assert from 'node:assert/strict';
|
2023-01-30 11:55:10 -05:00
|
|
|
import { before, describe, it } from 'node:test';
|
2023-02-09 11:50:04 -05:00
|
|
|
import * as cheerio from 'cheerio';
|
2023-01-30 11:55:10 -05:00
|
|
|
import { parseHTML } from 'linkedom';
|
|
|
|
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
|
|
|
|
|
|
describe('Head injection w/ MDX', () => {
|
|
|
|
let fixture;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: new URL('./fixtures/css-head-mdx/', import.meta.url),
|
|
|
|
integrations: [mdx()],
|
2023-08-18 01:52:06 -05:00
|
|
|
// test suite was authored when inlineStylesheets defaulted to never
|
|
|
|
build: { inlineStylesheets: 'never' },
|
2024-09-18 09:16:40 -05:00
|
|
|
experimental: { contentLayer: true },
|
2023-01-30 11:55:10 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('build', () => {
|
|
|
|
before(async () => {
|
|
|
|
await fixture.build();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('only injects contents into head', async () => {
|
|
|
|
const html = await fixture.readFile('/indexThree/index.html');
|
|
|
|
const { document } = parseHTML(html);
|
|
|
|
|
2023-02-02 14:21:32 -05:00
|
|
|
const links = document.querySelectorAll('head link[rel=stylesheet]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(links.length, 1);
|
2023-01-30 11:55:10 -05:00
|
|
|
|
2023-02-02 14:21:32 -05:00
|
|
|
const scripts = document.querySelectorAll('head script[type=module]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(scripts.length, 1);
|
2023-01-30 11:55:10 -05:00
|
|
|
});
|
2023-02-07 10:56:32 -05:00
|
|
|
|
|
|
|
it('injects into the head for content collections', async () => {
|
|
|
|
const html = await fixture.readFile('/posts/test/index.html');
|
|
|
|
const { document } = parseHTML(html);
|
|
|
|
|
|
|
|
const links = document.querySelectorAll('head link[rel=stylesheet]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(links.length, 1);
|
2023-02-07 10:56:32 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('injects content from a component using Content#render()', async () => {
|
|
|
|
const html = await fixture.readFile('/DirectContentUsage/index.html');
|
|
|
|
const { document } = parseHTML(html);
|
|
|
|
|
|
|
|
const links = document.querySelectorAll('head link[rel=stylesheet]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(links.length, 1);
|
2023-02-07 10:56:32 -05:00
|
|
|
|
|
|
|
const scripts = document.querySelectorAll('head script[type=module]');
|
2024-05-08 04:24:47 -05:00
|
|
|
assert.equal(scripts.length, 1);
|
2023-02-07 10:56:32 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Using component using slots.render() API', async () => {
|
|
|
|
const html = await fixture.readFile('/remote/index.html');
|
|
|
|
const { document } = parseHTML(html);
|
|
|
|
|
|
|
|
const links = document.querySelectorAll('head link[rel=stylesheet]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(links.length, 1);
|
2023-02-07 10:56:32 -05:00
|
|
|
});
|
2023-02-09 11:50:04 -05:00
|
|
|
|
|
|
|
it('Using component but no layout', async () => {
|
|
|
|
const html = await fixture.readFile('/noLayoutWithComponent/index.html');
|
|
|
|
// Using cheerio here because linkedom doesn't support head tag injection
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
const headLinks = $('head link[rel=stylesheet]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(headLinks.length, 1);
|
2023-02-09 11:52:23 -05:00
|
|
|
|
2023-02-09 11:50:04 -05:00
|
|
|
const bodyLinks = $('body link[rel=stylesheet]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(bodyLinks.length, 0);
|
2023-02-09 11:50:04 -05:00
|
|
|
});
|
2023-02-13 07:49:10 -05:00
|
|
|
|
|
|
|
it('JSX component rendering Astro children within head buffering phase', async () => {
|
|
|
|
const html = await fixture.readFile('/posts/using-component/index.html');
|
|
|
|
// Using cheerio here because linkedom doesn't support head tag injection
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
const headLinks = $('head link[rel=stylesheet]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(headLinks.length, 1);
|
2023-02-13 07:49:10 -05:00
|
|
|
|
|
|
|
const bodyLinks = $('body link[rel=stylesheet]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(bodyLinks.length, 0);
|
2023-02-13 07:49:10 -05:00
|
|
|
});
|
2023-04-06 17:33:47 -05:00
|
|
|
|
|
|
|
it('Injection caused by delayed slots', async () => {
|
|
|
|
const html = await fixture.readFile('/componentwithtext/index.html');
|
|
|
|
|
|
|
|
// Using cheerio here because linkedom doesn't support head tag injection
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
const headLinks = $('head link[rel=stylesheet]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(headLinks.length, 1);
|
2023-04-06 17:33:47 -05:00
|
|
|
|
|
|
|
const bodyLinks = $('body link[rel=stylesheet]');
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(bodyLinks.length, 0);
|
2023-04-06 17:33:47 -05:00
|
|
|
});
|
2023-01-30 11:55:10 -05:00
|
|
|
});
|
|
|
|
});
|