css-head-mdx.test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import mdx from '@astrojs/mdx';
  2. import { parseHTML } from 'linkedom';
  3. import { describe, it, before } from 'node:test';
  4. import * as assert from 'node:assert/strict';
  5. import { loadFixture } from '../../../astro/test/test-utils.js';
  6. import * as cheerio from 'cheerio';
  7. describe('Head injection w/ MDX', () => {
  8. let fixture;
  9. before(async () => {
  10. fixture = await loadFixture({
  11. root: new URL('./fixtures/css-head-mdx/', import.meta.url),
  12. integrations: [mdx()],
  13. // test suite was authored when inlineStylesheets defaulted to never
  14. build: { inlineStylesheets: 'never' },
  15. });
  16. });
  17. describe('build', () => {
  18. before(async () => {
  19. await fixture.build();
  20. });
  21. it('only injects contents into head', async () => {
  22. const html = await fixture.readFile('/indexThree/index.html');
  23. const { document } = parseHTML(html);
  24. const links = document.querySelectorAll('head link[rel=stylesheet]');
  25. assert.equal(links.length, 1);
  26. const scripts = document.querySelectorAll('head script[type=module]');
  27. assert.equal(scripts.length, 1);
  28. });
  29. it('injects into the head for content collections', async () => {
  30. const html = await fixture.readFile('/posts/test/index.html');
  31. const { document } = parseHTML(html);
  32. const links = document.querySelectorAll('head link[rel=stylesheet]');
  33. assert.equal(links.length, 1);
  34. });
  35. it('injects content from a component using Content#render()', async () => {
  36. const html = await fixture.readFile('/DirectContentUsage/index.html');
  37. const { document } = parseHTML(html);
  38. const links = document.querySelectorAll('head link[rel=stylesheet]');
  39. assert.equal(links.length, 1);
  40. const scripts = document.querySelectorAll('head script[type=module]');
  41. assert.equal(scripts.length, 2);
  42. });
  43. it('Using component using slots.render() API', async () => {
  44. const html = await fixture.readFile('/remote/index.html');
  45. const { document } = parseHTML(html);
  46. const links = document.querySelectorAll('head link[rel=stylesheet]');
  47. assert.equal(links.length, 1);
  48. });
  49. it('Using component but no layout', async () => {
  50. const html = await fixture.readFile('/noLayoutWithComponent/index.html');
  51. // Using cheerio here because linkedom doesn't support head tag injection
  52. const $ = cheerio.load(html);
  53. const headLinks = $('head link[rel=stylesheet]');
  54. assert.equal(headLinks.length, 1);
  55. const bodyLinks = $('body link[rel=stylesheet]');
  56. assert.equal(bodyLinks.length, 0);
  57. });
  58. it('JSX component rendering Astro children within head buffering phase', async () => {
  59. const html = await fixture.readFile('/posts/using-component/index.html');
  60. // Using cheerio here because linkedom doesn't support head tag injection
  61. const $ = cheerio.load(html);
  62. const headLinks = $('head link[rel=stylesheet]');
  63. assert.equal(headLinks.length, 1);
  64. const bodyLinks = $('body link[rel=stylesheet]');
  65. assert.equal(bodyLinks.length, 0);
  66. });
  67. it('Injection caused by delayed slots', async () => {
  68. const html = await fixture.readFile('/componentwithtext/index.html');
  69. // Using cheerio here because linkedom doesn't support head tag injection
  70. const $ = cheerio.load(html);
  71. const headLinks = $('head link[rel=stylesheet]');
  72. assert.equal(headLinks.length, 1);
  73. const bodyLinks = $('body link[rel=stylesheet]');
  74. assert.equal(bodyLinks.length, 0);
  75. });
  76. });
  77. });