@@ -33,6 +33,8 @@ describe('script component lifecycle', () => {
3333afterEach ( ( ) => {
3434for ( const wrapper of wrappers . splice ( 0 ) )
3535wrapper . unmount ( )
36+ for ( const el of document . querySelectorAll ( '#_carbonads_js' ) )
37+ el . remove ( )
3638if ( lemonSqueezyDescriptor )
3739Object . defineProperty ( window , 'LemonSqueezy' , lemonSqueezyDescriptor )
3840else
@@ -51,6 +53,37 @@ describe('script component lifecycle', () => {
5153expect ( removeRoot ) . not . toHaveBeenCalled ( )
5254} )
5355
56+ // carbon.js reads `document.getElementById('_carbonads_js').src` from its own
57+ // async ad callback, unguarded, so the id must outlive the component.
58+ it ( 'keeps the carbon script resolvable by id after unmount' , ( ) => {
59+ const wrapper = mount ( ScriptCarbonAds , {
60+ attachTo : document . body ,
61+ props : { serve : 'CW7DTKJL' , placement : 'unlighthousedev' , format : 'cover' } ,
62+ } )
63+ wrappers . push ( wrapper )
64+
65+ wrapper . unmount ( )
66+
67+ const readServe = ( ) => new URL ( document . getElementById ( '_carbonads_js' ) ! . getAttribute ( 'src' ) ! , 'https://x.test' ) . searchParams . get ( 'serve' )
68+ expect ( readServe ( ) ) . toBe ( 'CW7DTKJL' )
69+ } )
70+
71+ it ( 'replaces the parked script when a new instance mounts' , ( ) => {
72+ const first = mount ( ScriptCarbonAds , {
73+ attachTo : document . body ,
74+ props : { serve : 'CW7DTKJL' , placement : 'first' , format : 'cover' } ,
75+ } )
76+ first . unmount ( )
77+ const second = mount ( ScriptCarbonAds , {
78+ attachTo : document . body ,
79+ props : { serve : 'CW7DTKJL' , placement : 'second' , format : 'cover' } ,
80+ } )
81+ wrappers . push ( second )
82+
83+ expect ( document . querySelectorAll ( '#_carbonads_js' ) ) . toHaveLength ( 1 )
84+ expect ( document . getElementById ( '_carbonads_js' ) ! . getAttribute ( 'src' ) ) . toContain ( 'placement=second' )
85+ } )
86+
5487it ( 'fans Lemon Squeezy events out to every live component' , ( ) => {
5588const firstEvent = vi . fn ( )
5689const secondEvent = vi . fn ( )
0 commit comments