@@ -2,41 +2,35 @@ import React from 'react';
22import type { RenderResult } from '@testing-library/react' ;
33import { render , screen , waitFor , within } from '@testing-library/react' ;
44import { QueryClient } from '@tanstack/react-query' ;
5+ import { GrowthBook } from '@growthbook/growthbook-react' ;
56import ad from '../../../../__tests__/fixture/ad' ;
67import { AdGrid } from './AdGrid' ;
78import { AdList } from './AdList' ;
89import { SignalAdList } from './SignalAdList' ;
910import type { AdCardProps } from './common/common' ;
1011import { TestBootProvider } from '../../../../__tests__/helpers/boot' ;
1112import { ActiveFeedContext } from '../../../contexts' ;
13+ import { businessWebsiteUrl } from '../../../lib/constants' ;
1214
1315const defaultProps : AdCardProps = {
1416 ad,
17+ index : 0 ,
18+ feedIndex : 0 ,
1519 onLinkClick : jest . fn ( ) ,
1620} ;
1721
1822beforeEach ( ( ) => {
1923 jest . clearAllMocks ( ) ;
2024} ) ;
2125
22- const renderComponent = ( props : Partial < AdCardProps > = { } ) : RenderResult => {
23- const client = new QueryClient ( ) ;
24- return render (
25- < TestBootProvider client = { client } >
26- < ActiveFeedContext . Provider value = { { queryKey : 'test' } } >
27- < AdGrid { ...defaultProps } { ...props } />
28- </ ActiveFeedContext . Provider >
29- </ TestBootProvider > ,
30- ) ;
31- } ;
32-
3326const renderListComponent = (
3427 props : Partial < AdCardProps > = { } ,
28+ gb = new GrowthBook ( ) ,
3529) : RenderResult => {
3630 const client = new QueryClient ( ) ;
3731 return render (
38- < TestBootProvider client = { client } >
39- < ActiveFeedContext . Provider value = { { queryKey : 'test' } } >
32+ < TestBootProvider client = { client } gb = { gb } >
33+ < ActiveFeedContext . Provider value = { { items : [ ] , queryKey : [ 'test' ] } } >
4034 < AdList { ...defaultProps } { ...props } />
4135 </ ActiveFeedContext . Provider >
4236 </ TestBootProvider > ,
@@ -49,26 +43,53 @@ const renderSignalListComponent = (
4943 const client = new QueryClient ( ) ;
5044 return render (
5145 < TestBootProvider client = { client } >
52- < ActiveFeedContext . Provider value = { { queryKey : 'test' } } >
46+ < ActiveFeedContext . Provider value = { { items : [ ] , queryKey : [ 'test' ] } } >
5347 < SignalAdList { ...defaultProps } { ...props } />
5448 </ ActiveFeedContext . Provider >
5549 </ TestBootProvider > ,
5650 ) ;
5751} ;
5852
53+ const getGrowthBook = ( isAdReferralCtaEnabled = false ) : GrowthBook => {
54+ const gb = new GrowthBook ( ) ;
55+
56+ gb . setFeatures ( {
57+ ad_referral_cta : {
58+ defaultValue : isAdReferralCtaEnabled ,
59+ } ,
60+ } ) ;
61+
62+ return gb ;
63+ } ;
64+
5965const getNormalizedText = ( element ?: Element | null ) : string =>
6066 element ?. textContent ?. replace ( / \u200B / g, '' ) . trim ( ) ?? '' ;
6167
68+ const renderGridComponent = (
69+ props : Partial < AdCardProps > = { } ,
70+ gb = new GrowthBook ( ) ,
71+ ) : RenderResult => {
72+ const client = new QueryClient ( ) ;
73+
74+ return render (
75+ < TestBootProvider client = { client } gb = { gb } >
76+ < ActiveFeedContext . Provider value = { { items : [ ] , queryKey : [ 'test' ] } } >
77+ < AdGrid { ...defaultProps } { ...props } />
78+ </ ActiveFeedContext . Provider >
79+ </ TestBootProvider > ,
80+ ) ;
81+ } ;
82+
6283it ( 'should call on click on component left click' , async ( ) => {
63- renderComponent ( ) ;
84+ renderGridComponent ( ) ;
6485 const el = await screen . findByTestId ( 'adItem' ) ;
6586 const links = await within ( el ) . findAllByRole ( 'link' ) ;
6687 links [ 0 ] . click ( ) ;
6788 await waitFor ( ( ) => expect ( defaultProps . onLinkClick ) . toBeCalledWith ( ad ) ) ;
6889} ) ;
6990
7091it ( 'should call on click on component middle mouse up' , async ( ) => {
71- renderComponent ( ) ;
92+ renderGridComponent ( ) ;
7293 const el = await screen . findByTestId ( 'adItem' ) ;
7394 const links = await within ( el ) . findAllByRole ( 'link' ) ;
7495 links [ 0 ] . dispatchEvent (
@@ -78,29 +99,46 @@ it('should call on click on component middle mouse up', async () => {
7899} ) ;
79100
80101it ( 'should show a single image by default' , async ( ) => {
81- renderComponent ( ) ;
102+ renderGridComponent ( ) ;
82103 const img = await screen . findByAltText ( 'Ad image' ) ;
83104 const background = screen . queryByAltText ( 'Ad image background' ) ;
84105 expect ( img ) . toBeInTheDocument ( ) ;
85106 expect ( background ) . not . toBeInTheDocument ( ) ;
86107} ) ;
87108
88109it ( 'should show blurred image for carbon' , async ( ) => {
89- renderComponent ( { ad : { ...ad , source : 'Carbon' } } ) ;
110+ renderGridComponent ( { ad : { ...ad , source : 'Carbon' } } ) ;
90111 const img = await screen . findByAltText ( 'Ad image' ) ;
91112 const background = screen . queryByAltText ( 'Ad image background' ) ;
92113 expect ( img ) . toHaveClass ( 'absolute' ) ;
93114 expect ( background ) . toBeInTheDocument ( ) ;
94115} ) ;
95116
96117it ( 'should show pixel images' , async ( ) => {
97- renderComponent ( {
118+ renderGridComponent ( {
98119 ad : { ...ad , pixel : [ 'https://daily.dev/pixel' ] } ,
99120 } ) ;
100121 const el = await screen . findByTestId ( 'pixel' ) ;
101122 expect ( el ) . toHaveAttribute ( 'src' , 'https://daily.dev/pixel' ) ;
102123} ) ;
103124
125+ it ( 'should not render advertise link by default' , ( ) => {
126+ renderGridComponent ( ) ;
127+
128+ expect (
129+ screen . queryByRole ( 'link' , { name : 'Advertise here' } ) ,
130+ ) . not . toBeInTheDocument ( ) ;
131+ } ) ;
132+
133+ it ( 'should render advertise link on grid ad when feature is enabled' , ( ) => {
134+ renderGridComponent ( { } , getGrowthBook ( true ) ) ;
135+
136+ expect ( screen . getByRole ( 'link' , { name : 'Advertise here' } ) ) . toHaveAttribute (
137+ 'href' ,
138+ businessWebsiteUrl ,
139+ ) ;
140+ } ) ;
141+
104142it ( 'should render promoted attribution outside of list title clamp' , async ( ) => {
105143 const promotedMatcher = ( _ : string , element ?: Element | null ) : boolean => {
106144 const text = getNormalizedText ( element ) ;
@@ -114,6 +152,15 @@ it('should render promoted attribution outside of list title clamp', async () =>
114152 expect ( await screen . findByText ( promotedMatcher ) ) . toBeInTheDocument ( ) ;
115153} ) ;
116154
155+ it ( 'should render advertise link on list ad when feature is enabled' , ( ) => {
156+ renderListComponent ( { } , getGrowthBook ( true ) ) ;
157+
158+ expect ( screen . getByRole ( 'link' , { name : 'Advertise here' } ) ) . toHaveAttribute (
159+ 'href' ,
160+ businessWebsiteUrl ,
161+ ) ;
162+ } ) ;
163+
117164it ( 'should render company logo and company name in signal ad header' , async ( ) => {
118165 const companyLogo = 'https://daily.dev/company-logo.png' ;
119166 const companyName = 'daily.dev' ;
0 commit comments