mdx-script-style-raw.test.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import mdx from '@astrojs/mdx';
  2. import { describe, it, before, after } from 'node:test';
  3. import * as assert from 'node:assert/strict';
  4. import { parseHTML } from 'linkedom';
  5. import { loadFixture } from '../../../astro/test/test-utils.js';
  6. const FIXTURE_ROOT = new URL('./fixtures/mdx-script-style-raw/', import.meta.url);
  7. describe('MDX script style raw', () => {
  8. describe('dev', () => {
  9. let fixture;
  10. let devServer;
  11. before(async () => {
  12. fixture = await loadFixture({
  13. root: FIXTURE_ROOT,
  14. integrations: [mdx()],
  15. });
  16. devServer = await fixture.startDevServer();
  17. });
  18. after(async () => {
  19. await devServer.stop();
  20. });
  21. it('works with with raw script and style strings', async () => {
  22. const res = await fixture.fetch('/index.html');
  23. assert.equal(res.status, 200);
  24. const html = await res.text();
  25. const { document } = parseHTML(html);
  26. const scriptContent = document.getElementById('test-script').innerHTML;
  27. assert.equal(
  28. scriptContent.includes("console.log('raw script')"),
  29. true,
  30. 'script should not be html-escaped'
  31. );
  32. const styleContent = document.getElementById('test-style').innerHTML;
  33. assert.equal(
  34. styleContent.includes('h1[id="script-style-raw"]'),
  35. true,
  36. 'style should not be html-escaped'
  37. );
  38. });
  39. });
  40. describe('build', () => {
  41. it('works with with raw script and style strings', async () => {
  42. const fixture = await loadFixture({
  43. root: FIXTURE_ROOT,
  44. integrations: [mdx()],
  45. });
  46. await fixture.build();
  47. const html = await fixture.readFile('/index.html');
  48. const { document } = parseHTML(html);
  49. const scriptContent = document.getElementById('test-script').innerHTML;
  50. assert.equal(
  51. scriptContent.includes("console.log('raw script')"),
  52. true,
  53. 'script should not be html-escaped'
  54. );
  55. const styleContent = document.getElementById('test-style').innerHTML;
  56. assert.equal(
  57. styleContent.includes('h1[id="script-style-raw"]'),
  58. true,
  59. 'style should not be html-escaped'
  60. );
  61. });
  62. });
  63. });