|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import analytics from '@automattic/jetpack-analytics'; |
| 5 | +import { queryClient } from '@jetpack-premium-analytics/data'; |
| 6 | +import { act, render, screen } from '@testing-library/react'; |
| 7 | +import userEvent from '@testing-library/user-event'; |
| 8 | +import apiFetch from '@wordpress/api-fetch'; |
| 9 | +import { RegistryProvider } from '@wordpress/data'; |
| 10 | +/** |
| 11 | + * Internal dependencies |
| 12 | + */ |
| 13 | +import { createNoticesRegistry } from '../../../../tests/js/notice-test-utils'; |
| 14 | +import { DashboardNotices } from '../dashboard-notices'; |
| 15 | +import { resetPlanLimitNoticeForTesting } from '../plan-limit-notice/plan-limit-notice'; |
| 16 | +import { resetPurchaseSuccessNoticeForTesting } from '../purchase-success-notice/purchase-success-notice'; |
| 17 | +import { resetFreeSiteUpgradeNoticeForTesting } from './free-site-upgrade-notice'; |
| 18 | + |
| 19 | +jest.mock( |
| 20 | + '@automattic/jetpack-analytics', |
| 21 | + () => jest.requireActual( '../../../../tests/js/analytics-test-utils' ).mockJetpackAnalytics |
| 22 | +); |
| 23 | +jest.mock( '@wordpress/api-fetch', () => jest.fn() ); |
| 24 | + |
| 25 | +const mockApiFetch = apiFetch as unknown as jest.Mock; |
| 26 | +const recordEvent = jest.mocked( analytics.tracks.recordEvent ); |
| 27 | + |
| 28 | +const UPGRADE_URL = 'https://wordpress.com/checkout/example.com/premium?redirect_to=back'; |
| 29 | + |
| 30 | +type Site = { |
| 31 | + offered?: boolean; |
| 32 | + upgradeUrl?: string; |
| 33 | + hasStatsProduct?: boolean; |
| 34 | + purchaseSuccess?: 'paid' | 'free'; |
| 35 | +}; |
| 36 | + |
| 37 | +let notices = createNoticesRegistry(); |
| 38 | + |
| 39 | +/** |
| 40 | + * Seeds the script data the banner reads. |
| 41 | + * |
| 42 | + * @param site - The site to seed. |
| 43 | + * @param site.offered - Whether the server offers the site a paid plan. |
| 44 | + * @param site.upgradeUrl - Where the server sends the upgrade, empty when there is nowhere to buy it. |
| 45 | + * @param site.hasStatsProduct - Whether the site has a Jetpack Stats product. |
| 46 | + * @param site.purchaseSuccess - The checkout this load returned from, if any. |
| 47 | + */ |
| 48 | +function setSite( { |
| 49 | + offered = true, |
| 50 | + upgradeUrl = UPGRADE_URL, |
| 51 | + hasStatsProduct = false, |
| 52 | + purchaseSuccess, |
| 53 | +}: Site = {} ) { |
| 54 | + window.JetpackScriptData = { |
| 55 | + site: { |
| 56 | + admin_url: 'https://example.com/wp-admin/', |
| 57 | + host: 'jetpack', |
| 58 | + wpcom: { blog_id: 123456789 }, |
| 59 | + }, |
| 60 | + premium_analytics: { |
| 61 | + plan_gates: { |
| 62 | + tier: 'jetpack-free', |
| 63 | + locked_features: {}, |
| 64 | + locked_widget_types: {}, |
| 65 | + free_site_upgrade: offered |
| 66 | + ? { upgrade_url: upgradeUrl, has_stats_product: hasStatsProduct } |
| 67 | + : null, |
| 68 | + }, |
| 69 | + purchase_success: purchaseSuccess ? { type: purchaseSuccess } : undefined, |
| 70 | + }, |
| 71 | + } as unknown as typeof window.JetpackScriptData; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Answers the notices and plan usage requests. |
| 76 | + * |
| 77 | + * @param state - The Stats notice visibility state, over a banner WordPress.com reports visible; a dismissal updates it. |
| 78 | + * @param viewsUsed - This cycle's views against a 10,000 limit. |
| 79 | + */ |
| 80 | +function mockEndpoints( state: Record< string, boolean | undefined > = {}, viewsUsed = 0 ) { |
| 81 | + let saved = { free_site_upgrade: true, ...state }; |
| 82 | + mockApiFetch.mockImplementation( ( { path, method }: { path: string; method?: string } ) => { |
| 83 | + if ( path.includes( 'jetpack-stats/usage' ) ) { |
| 84 | + return Promise.resolve( { |
| 85 | + current_usage: { views_count: viewsUsed }, |
| 86 | + views_limit: 10000, |
| 87 | + } ); |
| 88 | + } |
| 89 | + if ( path.includes( 'notices' ) ) { |
| 90 | + if ( method === 'POST' ) { |
| 91 | + saved = { ...saved, free_site_upgrade: false }; |
| 92 | + } |
| 93 | + return Promise.resolve( saved ); |
| 94 | + } |
| 95 | + return Promise.reject( new Error( `Unexpected request: ${ path }` ) ); |
| 96 | + } ); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Renders the notices and settles their requests. |
| 101 | + * |
| 102 | + * @return The render result. |
| 103 | + */ |
| 104 | +async function renderNotices() { |
| 105 | + const view = render( |
| 106 | + <RegistryProvider value={ notices.registry }> |
| 107 | + <DashboardNotices /> |
| 108 | + </RegistryProvider> |
| 109 | + ); |
| 110 | + await act( () => jest.runAllTimersAsync() ); |
| 111 | + await act( () => jest.runAllTimersAsync() ); |
| 112 | + return view; |
| 113 | +} |
| 114 | + |
| 115 | +describe( 'FreeSiteUpgradeNotice', () => { |
| 116 | + beforeEach( () => { |
| 117 | + jest.useFakeTimers(); |
| 118 | + queryClient.clear(); |
| 119 | + resetFreeSiteUpgradeNoticeForTesting(); |
| 120 | + resetPlanLimitNoticeForTesting(); |
| 121 | + resetPurchaseSuccessNoticeForTesting(); |
| 122 | + notices = createNoticesRegistry(); |
| 123 | + recordEvent.mockClear(); |
| 124 | + mockApiFetch.mockReset(); |
| 125 | + setSite(); |
| 126 | + } ); |
| 127 | + |
| 128 | + afterEach( () => { |
| 129 | + jest.useRealTimers(); |
| 130 | + } ); |
| 131 | + |
| 132 | + it( 'offers the upgrade to a free site 30 days after its first visit', async () => { |
| 133 | + mockEndpoints(); |
| 134 | + |
| 135 | + const { container } = await renderNotices(); |
| 136 | + |
| 137 | + expect( container ).toHaveTextContent( 'Unlock more Stats with a paid plan' ); |
| 138 | + expect( screen.getByRole( 'link', { name: 'Upgrade' } ) ).toHaveAttribute( |
| 139 | + 'href', |
| 140 | + UPGRADE_URL |
| 141 | + ); |
| 142 | + } ); |
| 143 | + |
| 144 | + it( 'keeps only Learn more when the server has nowhere to sell the plan', async () => { |
| 145 | + setSite( { upgradeUrl: '' } ); |
| 146 | + mockEndpoints(); |
| 147 | + |
| 148 | + await renderNotices(); |
| 149 | + |
| 150 | + expect( screen.queryByText( 'Upgrade' ) ).not.toBeInTheDocument(); |
| 151 | + expect( screen.getByRole( 'link', { name: /Learn more/ } ) ).toBeInTheDocument(); |
| 152 | + } ); |
| 153 | + |
| 154 | + it( 'asks a site on the free Stats product whether it wants more', async () => { |
| 155 | + setSite( { hasStatsProduct: true } ); |
| 156 | + mockEndpoints(); |
| 157 | + |
| 158 | + const { container } = await renderNotices(); |
| 159 | + |
| 160 | + expect( container ).toHaveTextContent( 'Want to get the most out of Jetpack Stats?' ); |
| 161 | + } ); |
| 162 | + |
| 163 | + it( 'says nothing to a site the server does not offer a paid plan', async () => { |
| 164 | + setSite( { offered: false } ); |
| 165 | + mockEndpoints(); |
| 166 | + |
| 167 | + const { container } = await renderNotices(); |
| 168 | + |
| 169 | + expect( container ).toBeEmptyDOMElement(); |
| 170 | + } ); |
| 171 | + |
| 172 | + it( 'stays away when the notices omit its state, as they do when WordPress.com fails', async () => { |
| 173 | + mockEndpoints( { free_site_upgrade: undefined } ); |
| 174 | + |
| 175 | + const { container } = await renderNotices(); |
| 176 | + |
| 177 | + expect( container ).toBeEmptyDOMElement(); |
| 178 | + } ); |
| 179 | + |
| 180 | + it.each( [ 'free_site_upgrade', 'do_you_love_jetpack_stats', 'commercial_site_upgrade' ] )( |
| 181 | + 'stays away once Stats has %s dismissed', |
| 182 | + async id => { |
| 183 | + mockEndpoints( { [ id ]: false } ); |
| 184 | + |
| 185 | + const { container } = await renderNotices(); |
| 186 | + |
| 187 | + expect( container ).toBeEmptyDOMElement(); |
| 188 | + } |
| 189 | + ); |
| 190 | + |
| 191 | + it( 'postpones the banner for ten years when dismissed', async () => { |
| 192 | + mockEndpoints(); |
| 193 | + const user = userEvent.setup( { advanceTimers: jest.advanceTimersByTime } ); |
| 194 | + const { container } = await renderNotices(); |
| 195 | + |
| 196 | + await user.click( screen.getByRole( 'button', { name: 'Dismiss' } ) ); |
| 197 | + await act( () => jest.runAllTimersAsync() ); |
| 198 | + |
| 199 | + expect( container ).toBeEmptyDOMElement(); |
| 200 | + expect( mockApiFetch ).toHaveBeenCalledWith( |
| 201 | + expect.objectContaining( { |
| 202 | + method: 'POST', |
| 203 | + data: { id: 'free_site_upgrade', status: 'postponed', postponed_for: 315360000 }, |
| 204 | + } ) |
| 205 | + ); |
| 206 | + } ); |
| 207 | + |
| 208 | + it( 'stays in place after a failed dismissal, though the refetch fails too', async () => { |
| 209 | + mockEndpoints(); |
| 210 | + const user = userEvent.setup( { advanceTimers: jest.advanceTimersByTime } ); |
| 211 | + const { container } = await renderNotices(); |
| 212 | + mockApiFetch.mockRejectedValue( new Error( 'offline' ) ); |
| 213 | + |
| 214 | + await user.click( screen.getByRole( 'button', { name: 'Dismiss' } ) ); |
| 215 | + await act( () => jest.runAllTimersAsync() ); |
| 216 | + |
| 217 | + expect( container ).toHaveTextContent( 'Unlock more Stats with a paid plan' ); |
| 218 | + } ); |
| 219 | + |
| 220 | + it( 'shows instead of the plan limit notice when both apply', async () => { |
| 221 | + mockEndpoints( {}, 9500 ); |
| 222 | + |
| 223 | + const { container } = await renderNotices(); |
| 224 | + |
| 225 | + expect( container ).toHaveTextContent( 'Unlock more Stats with a paid plan' ); |
| 226 | + expect( container ).not.toHaveTextContent( 'You are nearing your monthly limit' ); |
| 227 | + } ); |
| 228 | + |
| 229 | + it( 'gives way to the thank-you notice when a checkout returns', async () => { |
| 230 | + setSite( { purchaseSuccess: 'free' } ); |
| 231 | + mockEndpoints( {}, 9500 ); |
| 232 | + |
| 233 | + const { container } = await renderNotices(); |
| 234 | + |
| 235 | + expect( container ).toHaveTextContent( 'Thank you for using Jetpack Stats!' ); |
| 236 | + expect( container ).not.toHaveTextContent( 'Unlock more Stats with a paid plan' ); |
| 237 | + expect( container ).not.toHaveTextContent( 'You are nearing your monthly limit' ); |
| 238 | + } ); |
| 239 | + |
| 240 | + it( 'leaves the plan limit notice in place once dismissed', async () => { |
| 241 | + mockEndpoints( { free_site_upgrade: false }, 9500 ); |
| 242 | + |
| 243 | + const { container } = await renderNotices(); |
| 244 | + |
| 245 | + expect( container ).toHaveTextContent( 'You are nearing your monthly limit' ); |
| 246 | + } ); |
| 247 | + |
| 248 | + it( 'records one view per page load, though every section tab mounts the notices', async () => { |
| 249 | + mockEndpoints(); |
| 250 | + |
| 251 | + const { unmount } = await renderNotices(); |
| 252 | + unmount(); |
| 253 | + await renderNotices(); |
| 254 | + |
| 255 | + const views = recordEvent.mock.calls.filter( |
| 256 | + ( [ name ] ) => name === 'jetpack_premium_analytics_free_site_upgrade_notice_view' |
| 257 | + ); |
| 258 | + expect( views ).toHaveLength( 1 ); |
| 259 | + } ); |
| 260 | +} ); |
0 commit comments