Home / File/ conditional-rendering.test.js — astro Source File

conditional-rendering.test.js — astro Source File

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

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

/**
 * @see https://github.com/withastro/astro/issues/14252
 *
 * Svelte components that are conditionally rendered (inside {#if} blocks)
 * should have their styles included in the production build, even when
 * the condition is initially false during SSR.
 */

let fixture;

describe('Conditional rendering styles', () => {
	before(async () => {
		fixture = await loadFixture({
			root: new URL('./fixtures/conditional-rendering/', import.meta.url),
		});
	});

	describe('build', () => {
		before(async () => {
			await fixture.build();
		});

		it('includes styles for conditionally rendered Svelte components', async () => {
			const html = await fixture.readFile('/index.html');
			const $ = cheerioLoad(html);

			// Get all CSS - either inline styles or linked stylesheets
			let allCss = '';

			// Check inline styles
			$('style').each((_, el) => {
				allCss += $(el).text();
			});

			// Check linked stylesheets
			const cssLinks = $('link[rel="stylesheet"]');
			for (const link of cssLinks.toArray()) {
				const href = $(link).attr('href');
				if (href) {
					const cssContent = await fixture.readFile(href);
					allCss += cssContent;
				}
			}

			// Verify that styles from the Child component are included
			// The Child has: background-color: red
			// Even though the child is not rendered during SSR (showChild starts as false),
			// its styles should still be included in the build output
			const hasChildStyles = allCss.includes('red');
			assert.ok(
				hasChildStyles,
				`Child component styles (background-color: red) should be included in build output even when conditionally rendered. CSS found: ${allCss.substring(0, 500)}`,
			);
		});
	});

	describe('dev', () => {
		let devServer;

		before(async () => {
			devServer = await fixture.startDevServer();
		});

		after(async () => {
			await devServer.stop();
		});

		it('includes styles for conditionally rendered Svelte components', async () => {
			const html = await fixture.fetch('/').then((res) => res.text());

			// In dev mode, styles are typically injected via JS
			// The component should be present and work correctly
			const hasParentComponent = html.includes('parent');

			assert.ok(hasParentComponent, 'Parent component should be present in dev mode');
		});
	});
});

Domain

Dependencies

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free