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

astro-attrs.test.js — astro Source File

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

Entity Profile

Dependency Diagram

graph LR
  b96b90b9_2162_b915_5fc2_ea67d47cb318["astro-attrs.test.js"]
  0a624eac_945e_c9e8_c9de_3feb9de2dd15["test-utils.js"]
  b96b90b9_2162_b915_5fc2_ea67d47cb318 --> 0a624eac_945e_c9e8_c9de_3feb9de2dd15
  dd4f09ce_3fd7_8295_f616_8876cda4555c["loadFixture"]
  b96b90b9_2162_b915_5fc2_ea67d47cb318 --> dd4f09ce_3fd7_8295_f616_8876cda4555c
  e1e2fac7_5a95_7a88_cb1e_0a3b91c4e607["strict"]
  b96b90b9_2162_b915_5fc2_ea67d47cb318 --> e1e2fac7_5a95_7a88_cb1e_0a3b91c4e607
  6b0635f9_51ea_77aa_767b_7857878e98a6["node:test"]
  b96b90b9_2162_b915_5fc2_ea67d47cb318 --> 6b0635f9_51ea_77aa_767b_7857878e98a6
  deb87372_5629_35f8_9a54_e755a08f776a["cheerio"]
  b96b90b9_2162_b915_5fc2_ea67d47cb318 --> deb87372_5629_35f8_9a54_e755a08f776a
  style b96b90b9_2162_b915_5fc2_ea67d47cb318 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('Attributes', async () => {
	let fixture;

	before(async () => {
		fixture = await loadFixture({ root: './fixtures/astro-attrs/' });
		await fixture.build();
	});

	it('Passes attributes to elements as expected', async () => {
		const html = await fixture.readFile('/index.html');
		const $ = cheerio.load(html);

		const attrs = {
			'download-true': { attribute: 'download', value: '' },
			'download-false': { attribute: 'download', value: undefined },
			'download-undefined': { attribute: 'download', value: undefined },
			'download-string-empty': { attribute: 'download', value: '' },
			'download-string': { attribute: 'download', value: 'my-document.pdf' },
			'popover-auto': { attribute: 'popover', value: 'auto' },
			'popover-true': { attribute: 'popover', value: '' },
			'popover-false': { attribute: 'popover', value: undefined },
			'popover-string-empty': { attribute: 'popover', value: '' },
			'boolean-attr-true': { attribute: 'allowfullscreen', value: '' },
			'boolean-attr-false': { attribute: 'allowfullscreen', value: undefined },
			'boolean-attr-string-truthy': { attribute: 'allowfullscreen', value: '' },
			'boolean-attr-string-falsy': { attribute: 'allowfullscreen', value: undefined },
			'boolean-attr-number-truthy': { attribute: 'allowfullscreen', value: '' },
			'boolean-attr-number-falsy': { attribute: 'allowfullscreen', value: undefined },
			'data-attr-true': { attribute: 'data-foobar', value: 'true' },
			'data-attr-false': { attribute: 'data-foobar', value: 'false' },
			'data-attr-string-truthy': { attribute: 'data-foobar', value: 'foo' },
			'data-attr-string-falsy': { attribute: 'data-foobar', value: '' },
			'data-attr-number-truthy': { attribute: 'data-foobar', value: '1' },
			'data-attr-number-falsy': { attribute: 'data-foobar', value: '0' },
			'normal-attr-true': { attribute: 'foobar', value: 'true' },
			'normal-attr-false': { attribute: 'foobar', value: 'false' },
			'normal-attr-string-truthy': { attribute: 'foobar', value: 'foo' },
			'normal-attr-string-falsy': { attribute: 'foobar', value: '' },
			'normal-attr-number-truthy': { attribute: 'foobar', value: '1' },
			'normal-attr-number-falsy': { attribute: 'foobar', value: '0' },
			null: { attribute: 'attr', value: undefined },
			undefined: { attribute: 'attr', value: undefined },
			'html-enum': { attribute: 'draggable', value: 'true' },
			'html-enum-true': { attribute: 'draggable', value: 'true' },
			'html-enum-false': { attribute: 'draggable', value: 'false' },
		};

		assert.ok(!/allowfullscreen=/.test(html), 'boolean attributes should not have values');
		assert.ok(
			!/id="data-attr-string-falsy"\s+data-foobar=/.test(html),
			"data attributes should not have values if it's an empty string",
		);
		assert.ok(
			!/id="normal-attr-string-falsy"\s+data-foobar=/.test(html),
			"normal attributes should not have values if it's an empty string",
		);

		// cheerio will unescape the values, so checking that the url rendered unescaped to begin with has to be done manually
		assert.equal(
			html.includes('https://example.com/api/og?title=hello&description=somedescription'),
			true,
		);

		// cheerio will unescape the values, so checking that the url rendered unescaped to begin with has to be done manually
		assert.equal(
			html.includes('cmd: echo "foo" && echo "bar" > /tmp/hello.txt'),
			true,
		);

		for (const id of Object.keys(attrs)) {
			const { attribute, value } = attrs[id];
			const attr = $(`#${id}`).attr(attribute);
			assert.equal(attr, value, `Expected ${attribute} to be ${value} for #${id}`);
		}
	});

	it('Passes boolean attributes to components as expected', async () => {
		const html = await fixture.readFile('/component/index.html');
		const $ = cheerio.load(html);

		assert.equal($('#true').attr('attr'), 'attr-true');
		assert.equal($('#true').attr('type'), 'boolean');
		assert.equal($('#false').attr('attr'), 'attr-false');
		assert.equal($('#false').attr('type'), 'boolean');
	});

	it('Passes namespaced attributes as expected', async () => {
		const html = await fixture.readFile('/namespaced/index.html');
		const $ = cheerio.load(html);

		assert.equal($('div').attr('xmlns:happy'), 'https://example.com/schemas/happy');
		assert.equal($('img').attr('happy:smile'), 'sweet');
	});

	it('Passes namespaced attributes to components as expected', async () => {
		const html = await fixture.readFile('/namespaced-component/index.html');
		const $ = cheerio.load(html);

		assert.deepEqual($('span').attr('on:click'), '(event) => console.log(event)');
	});
});

Dependencies

Frequently Asked Questions

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