@@ -11,6 +11,8 @@ const { default: useCarbonAd } = await import('#site/hooks/useCarbonAd');
1111
1212const ad = {
1313 company : 'Company' ,
14+ description : 'Description' ,
15+ callToAction : 'Learn more' ,
1416 statlink : 'https://srv.buysellads.com/ads/click/x/1' ,
1517 statview : 'https://srv.buysellads.com/ads/view/x/1' ,
1618} ;
@@ -141,6 +143,52 @@ describe('useCarbonAd', () => {
141143 } ) ;
142144 } ) ;
143145
146+ describe ( 'validation' , ( ) => {
147+ const ValidationProbe = ( ) => {
148+ const { ad } = useCarbonAd ( 'CWBI6KJY' ) ;
149+
150+ return < div > { ad ? 'ad' : 'no ad' } </ div > ;
151+ } ;
152+
153+ const renderWith = async creative => {
154+ globalThis . fetch . mock . mockImplementation ( async ( ) => ( {
155+ ok : true ,
156+ json : async ( ) => ( { ads : [ creative ] } ) ,
157+ } ) ) ;
158+
159+ const { container } = render ( < ValidationProbe /> ) ;
160+ await flush ( ) ;
161+
162+ return container . textContent ;
163+ } ;
164+
165+ it ( 'accepts an ad without a title' , async ( ) => {
166+ assert . equal ( await renderWith ( ad ) , 'ad' ) ;
167+ } ) ;
168+
169+ it ( 'accepts an ad with a title' , async ( ) => {
170+ assert . equal ( await renderWith ( { ...ad , title : 'Title' } ) , 'ad' ) ;
171+ } ) ;
172+
173+ for ( const field of [ 'statlink' , 'description' , 'callToAction' ] ) {
174+ it ( `rejects a missing ${ field } ` , async ( ) => {
175+ assert . equal ( await renderWith ( { ...ad , [ field ] : undefined } ) , 'no ad' ) ;
176+ } ) ;
177+
178+ it ( `rejects a non-string ${ field } ` , async ( ) => {
179+ assert . equal ( await renderWith ( { ...ad , [ field ] : { a : 1 } } ) , 'no ad' ) ;
180+ } ) ;
181+ }
182+
183+ it ( 'rejects a non-string title' , async ( ) => {
184+ assert . equal ( await renderWith ( { ...ad , title : { a : 1 } } ) , 'no ad' ) ;
185+ } ) ;
186+
187+ it ( 'rejects an empty creative' , async ( ) => {
188+ assert . equal ( await renderWith ( { } ) , 'no ad' ) ;
189+ } ) ;
190+ } ) ;
191+
144192 it ( 'sends the view beacon after the ad was at least 50% visible for one second' , async t => {
145193 await renderAd ( t ) ;
146194
0 commit comments