1+ import { beforeEach , describe , expect , test } from 'vitest'
2+ import { experimental_AstroContainer as AstroContainer } from 'astro/container'
3+ import { withJsdomEnvironment } from '@test/unit/helpers/litRuntime'
4+
5+ describe ( 'Inset (Astro)' , ( ) => {
6+ let container : AstroContainer
7+
8+ beforeEach ( async ( ) => {
9+ container = await AstroContainer . create ( )
10+ } )
11+
12+ test ( 'renders a figure caption below the inset body when provided' , async ( ) => {
13+ const Inset = ( await import ( '@components/Inset/index.astro' ) ) . default
14+
15+ const renderedHtml = await container . renderToString ( Inset , {
16+ props : {
17+ color : 'warning-inverse' ,
18+ figure : 'Retry budget pseudocode' ,
19+ variant : 'default' ,
20+ } ,
21+ slots : {
22+ default : 'if (!budget.canRetry()) return false;' ,
23+ } ,
24+ } )
25+
26+ await withJsdomEnvironment ( async ( { window } ) => {
27+ window . document . body . innerHTML = renderedHtml
28+
29+ const figure = window . document . querySelector ( 'figure' )
30+ expect ( figure ) . toBeTruthy ( )
31+ expect ( figure ?. className ) . toContain ( 'mb-8' )
32+
33+ const figcaption = figure ?. querySelector ( 'figcaption' )
34+ expect ( figcaption ) . toBeTruthy ( )
35+ expect ( figcaption ?. textContent ) . toContain ( 'Retry budget pseudocode' )
36+ expect ( figcaption ?. className ) . toContain ( 'italic' )
37+ expect ( figcaption ?. className ) . toContain ( 'text-center' )
38+
39+ const insetBody = figure ?. querySelector ( 'div' )
40+ expect ( insetBody ) . toBeTruthy ( )
41+ expect ( insetBody ?. className ) . toContain ( 'w-full' )
42+ expect ( insetBody ?. textContent ) . toContain ( 'if (!budget.canRetry()) return false;' )
43+ } )
44+ } )
45+
46+ test ( 'renders a fit-width inset when fullWidth is false' , async ( ) => {
47+ const Inset = ( await import ( '@components/Inset/index.astro' ) ) . default
48+
49+ const renderedHtml = await container . renderToString ( Inset , {
50+ props : {
51+ fullWidth : false ,
52+ variant : 'default' ,
53+ } ,
54+ slots : {
55+ default : 'const fallback = queueForRetry(request)' ,
56+ } ,
57+ } )
58+
59+ await withJsdomEnvironment ( async ( { window } ) => {
60+ window . document . body . innerHTML = renderedHtml
61+
62+ const figure = window . document . querySelector ( 'figure' )
63+ expect ( figure ) . toBeTruthy ( )
64+ const insetBody = figure ?. querySelector ( 'div' )
65+ expect ( insetBody ) . toBeTruthy ( )
66+ expect ( insetBody ?. className ) . toContain ( 'w-fit' )
67+ expect ( insetBody ?. className ) . toContain ( 'max-w-full' )
68+ expect ( insetBody ?. className ) . toContain ( 'mx-auto' )
69+ } )
70+ } )
71+
72+ test ( 'omits the figcaption when no figure text is provided' , async ( ) => {
73+ const Inset = ( await import ( '@components/Inset/index.astro' ) ) . default
74+
75+ const renderedHtml = await container . renderToString ( Inset , {
76+ props : {
77+ variant : 'default' ,
78+ } ,
79+ slots : {
80+ default : 'const circuit = new CircuitBreaker(service, config)' ,
81+ } ,
82+ } )
83+
84+ await withJsdomEnvironment ( async ( { window } ) => {
85+ window . document . body . innerHTML = renderedHtml
86+
87+ expect ( window . document . querySelector ( 'figcaption' ) ) . toBeNull ( )
88+ expect ( window . document . querySelector ( 'figure div' ) ?. textContent ) . toContain (
89+ 'const circuit = new CircuitBreaker(service, config)'
90+ )
91+ } )
92+ } )
93+ } )
0 commit comments