Home / File/ astro-directives.test.js — astro Source File

astro-directives.test.js — astro Source File

Architecture documentation for astro-directives.test.js, a javascript file in the astro codebase. 5 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  553352e4_4e3f_6cf0_798c_ff5e92b998b6["astro-directives.test.js"]
  0a624eac_945e_c9e8_c9de_3feb9de2dd15["test-utils.js"]
  553352e4_4e3f_6cf0_798c_ff5e92b998b6 --> 0a624eac_945e_c9e8_c9de_3feb9de2dd15
  dd4f09ce_3fd7_8295_f616_8876cda4555c["loadFixture"]
  553352e4_4e3f_6cf0_798c_ff5e92b998b6 --> dd4f09ce_3fd7_8295_f616_8876cda4555c
  e1e2fac7_5a95_7a88_cb1e_0a3b91c4e607["strict"]
  553352e4_4e3f_6cf0_798c_ff5e92b998b6 --> e1e2fac7_5a95_7a88_cb1e_0a3b91c4e607
  6b0635f9_51ea_77aa_767b_7857878e98a6["node:test"]
  553352e4_4e3f_6cf0_798c_ff5e92b998b6 --> 6b0635f9_51ea_77aa_767b_7857878e98a6
  deb87372_5629_35f8_9a54_e755a08f776a["cheerio"]
  553352e4_4e3f_6cf0_798c_ff5e92b998b6 --> deb87372_5629_35f8_9a54_e755a08f776a
  style 553352e4_4e3f_6cf0_798c_ff5e92b998b6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Directives', async () => {
	let fixture;

	before(async () => {
		fixture = await loadFixture({
			root: './fixtures/astro-directives/',
			// test suite was authored when inlineStylesheets defaulted to never
			build: { inlineStylesheets: 'never' },
		});
		await fixture.build();
	});

	it('Passes define:vars to script elements', async () => {
		const html = await fixture.readFile('/define-vars/index.html');
		const $ = cheerio.load(html);

		assert.equal($('script').length, 5);

		let i = 0;
		for (const script of $('script').toArray()) {
			// Wrap script in scope ({}) to avoid redeclaration errors
			assert.equal($(script).text().startsWith('(function(){'), true);
			assert.equal($(script).text().endsWith('})();'), true);
			if (i < 2) {
				// Inline defined variables
				assert.equal($(script).toString().includes('const foo = "bar"'), true);
			} else if (i < 3) {
				// Convert invalid keys to valid identifiers
				assert.equal($(script).toString().includes('const dashCase = "bar"'), true);
			} else if (i < 4) {
				// Closing script tags in strings are escaped
				assert.equal($(script).toString().includes('const bar = "<script>bar\\x3C/script>"'), true);
			} else {
				// Vars with undefined values are handled
				assert.equal($(script).toString().includes('const undef = undefined'), true);
			}
			i++;
		}
	});

	it('Passes define:vars to style elements', async () => {
		const html = await fixture.readFile('/define-vars/index.html');
		const $ = cheerio.load(html);

		// All styles should be bundled
		assert.equal($('style').length, 0);

		// Inject style attribute on top-level element in page
		assert.equal($('html').attr('style').toString().includes('--bg: white;'), true);
		assert.equal($('html').attr('style').toString().includes('--fg: black;'), true);

		// Inject style attribute on top-level elements in component
		assert.equal($('h1').attr('style').toString().includes('--textColor: red;'), true);
	});

	it('Properly handles define:vars on style elements with style object', async () => {
		const html = await fixture.readFile('/define-vars/index.html');
		const $ = cheerio.load(html);

		// All styles should be bundled
		assert.equal($('style').length, 0);

		// Inject style attribute on top-level element in page
		assert.equal(
			$('#compound-style')
				.attr('style')
				.toString()
				.includes('color:var(--fg);--bg: white;--fg: black;'),
			true,
		);
	});

	it('set:html', async () => {
		const html = await fixture.readFile('/set-html/index.html');
		const $ = cheerio.load(html);

		assert.equal($('#text').length, 1);
		assert.equal($('#text').text(), 'a');

		assert.equal($('#zero').length, 1);
		assert.equal($('#zero').text(), '0');

		assert.equal($('#number').length, 1);
		assert.equal($('#number').text(), '1');

		assert.equal($('#undefined').length, 1);
		assert.equal($('#undefined').text(), '');

		assert.equal($('#null').length, 1);
		assert.equal($('#null').text(), '');

		assert.equal($('#false').length, 1);
		assert.equal($('#false').text(), '');

		assert.equal($('#true').length, 1);
		assert.equal($('#true').text(), 'true');
	});

	it('ignores client directives on Astro components', async () => {
		const html = await fixture.readFile('/client/index.html');
		const $ = cheerio.load(html);

		const hello = $('h1.hello');
		assert.equal(hello.text(), 'Hello');

		// Astro components should not have client directives
		assert.equal(hello.attr('client:load'), undefined);

		// Should not create Astro islands
		assert.equal($('astro-island').length, 0);
	});
});

Dependencies

Frequently Asked Questions

What does astro-directives.test.js do?
astro-directives.test.js is a source file in the astro codebase, written in javascript. It belongs to the IntegrationAdapters domain.
What does astro-directives.test.js depend on?
astro-directives.test.js imports 5 module(s): cheerio, loadFixture, node:test, strict, test-utils.js.
Where is astro-directives.test.js in the architecture?
astro-directives.test.js is located at packages/astro/test/astro-directives.test.js (domain: IntegrationAdapters, directory: packages/astro/test).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free