diff --git a/client/data/newsletter-categories/test/use-subscribed-newsletter-categories-query.test.tsx b/client/data/newsletter-categories/test/use-subscribed-newsletter-categories-query.test.tsx index 6b13ca164f90b..3a97847e916a3 100644 --- a/client/data/newsletter-categories/test/use-subscribed-newsletter-categories-query.test.tsx +++ b/client/data/newsletter-categories/test/use-subscribed-newsletter-categories-query.test.tsx @@ -5,21 +5,26 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { renderHook, waitFor } from '@testing-library/react'; import React from 'react'; -import requestWithSubkeyFallback from 'calypso/lib/request-with-subkey-fallback/request-with-subkey-fallback'; import useSubscribedNewsletterCategories from '../use-subscribed-newsletter-categories-query'; -jest.mock( 'calypso/lib/request-with-subkey-fallback/request-with-subkey-fallback', () => - jest.fn() -); +const mockGet = jest.fn(); +jest.mock( 'calypso/lib/wp', () => { + return { + __esModule: true, + default: { + req: { + get: ( ...args: unknown[] ) => mockGet( ...args ), + }, + }, + }; +} ); describe( 'useSubscribedNewsletterCategories', () => { let queryClient: QueryClient; let wrapper: any; beforeEach( () => { - ( - requestWithSubkeyFallback as jest.MockedFunction< typeof requestWithSubkeyFallback > - ).mockReset(); + mockGet.mockReset(); queryClient = new QueryClient( { defaultOptions: { @@ -39,9 +44,8 @@ describe( 'useSubscribedNewsletterCategories', () => { } ); it( 'should return expected data when successful', async () => { - ( - requestWithSubkeyFallback as jest.MockedFunction< typeof requestWithSubkeyFallback > - ).mockResolvedValue( { + mockGet.mockResolvedValue( { + enabled: true, newsletter_categories: [ { id: 1, @@ -69,6 +73,7 @@ describe( 'useSubscribedNewsletterCategories', () => { await waitFor( () => expect( result.current.isSuccess ).toBe( true ) ); expect( result.current.data ).toEqual( { + enabled: true, newsletterCategories: [ { id: 1, @@ -91,9 +96,8 @@ describe( 'useSubscribedNewsletterCategories', () => { } ); it( 'should handle empty response', async () => { - ( - requestWithSubkeyFallback as jest.MockedFunction< typeof requestWithSubkeyFallback > - ).mockResolvedValue( { + mockGet.mockResolvedValue( { + enabled: false, newsletter_categories: [], } ); @@ -103,63 +107,60 @@ describe( 'useSubscribedNewsletterCategories', () => { await waitFor( () => expect( result.current.isSuccess ).toBe( true ) ); - expect( result.current.data ).toEqual( { newsletterCategories: [] } ); + expect( result.current.data ).toEqual( { enabled: false, newsletterCategories: [] } ); } ); it( 'should call request with correct arguments', async () => { - ( - requestWithSubkeyFallback as jest.MockedFunction< typeof requestWithSubkeyFallback > - ).mockResolvedValue( { - success: true, + mockGet.mockResolvedValue( { + enabled: true, + newsletter_categories: [], } ); renderHook( () => useSubscribedNewsletterCategories( { siteId: 123 } ), { wrapper, } ); - await waitFor( () => expect( requestWithSubkeyFallback ).toHaveBeenCalled() ); + await waitFor( () => expect( mockGet ).toHaveBeenCalled() ); - expect( requestWithSubkeyFallback ).toHaveBeenCalledWith( - false, - `/sites/123/newsletter-categories/subscriptions` - ); + expect( mockGet ).toHaveBeenCalledWith( { + path: `/sites/123/newsletter-categories/subscriptions`, + apiNamespace: 'wpcom/v2', + } ); } ); it( 'should include the subscriptionId when being called with one', async () => { - ( - requestWithSubkeyFallback as jest.MockedFunction< typeof requestWithSubkeyFallback > - ).mockResolvedValue( { - success: true, + mockGet.mockResolvedValue( { + enabled: true, + newsletter_categories: [], } ); renderHook( () => useSubscribedNewsletterCategories( { siteId: 123, subscriptionId: 456 } ), { wrapper, } ); - await waitFor( () => expect( requestWithSubkeyFallback ).toHaveBeenCalled() ); + await waitFor( () => expect( mockGet ).toHaveBeenCalled() ); - expect( requestWithSubkeyFallback ).toHaveBeenCalledWith( - false, - `/sites/123/newsletter-categories/subscriptions/456` - ); + expect( mockGet ).toHaveBeenCalledWith( { + path: `/sites/123/newsletter-categories/subscriptions/456`, + apiNamespace: 'wpcom/v2', + } ); } ); it( 'should call with ?type=wpcom when being passed a user id', async () => { - ( - requestWithSubkeyFallback as jest.MockedFunction< typeof requestWithSubkeyFallback > - ).mockResolvedValue( { - success: true, + mockGet.mockResolvedValue( { + enabled: true, + newsletter_categories: [], } ); renderHook( () => useSubscribedNewsletterCategories( { siteId: 123, userId: 456 } ), { wrapper, } ); - await waitFor( () => expect( requestWithSubkeyFallback ).toHaveBeenCalled() ); + await waitFor( () => expect( mockGet ).toHaveBeenCalled() ); - expect( requestWithSubkeyFallback ).toHaveBeenCalledWith( - false, - `/sites/123/newsletter-categories/subscriptions/456?type=wpcom` - ); + expect( mockGet ).toHaveBeenCalledWith( { + path: `/sites/123/newsletter-categories/subscriptions/456?type=wpcom`, + apiNamespace: 'wpcom/v2', + } ); } ); } ); diff --git a/client/data/newsletter-categories/use-subscribed-newsletter-categories-query.tsx b/client/data/newsletter-categories/use-subscribed-newsletter-categories-query.tsx index db71f6c41aeb8..1357a709b0630 100644 --- a/client/data/newsletter-categories/use-subscribed-newsletter-categories-query.tsx +++ b/client/data/newsletter-categories/use-subscribed-newsletter-categories-query.tsx @@ -1,5 +1,5 @@ import { useQuery, UseQueryResult } from '@tanstack/react-query'; -import { useIsLoggedIn, requestWithSubkeyFallback } from 'calypso/lib/request-with-subkey-fallback'; +import wpcom from 'calypso/lib/wp'; import { NewsletterCategories, NewsletterCategory } from './types'; type NewsletterCategoryQueryProps = { @@ -31,8 +31,6 @@ const useSubscribedNewsletterCategories = ( { subscriptionId, userId, }: NewsletterCategoryQueryProps ): UseQueryResult< NewsletterCategories > => { - const { isLoggedIn } = useIsLoggedIn(); - let path = `/sites/${ siteId }/newsletter-categories/subscriptions`; if ( userId ) { path += `/${ userId }?type=wpcom`; @@ -42,12 +40,20 @@ const useSubscribedNewsletterCategories = ( { return useQuery( { queryKey: getSubscribedNewsletterCategoriesKey( siteId, subscriptionId, userId ), - queryFn: () => { + queryFn: async () => { try { - return requestWithSubkeyFallback< NewsletterCategoryResponse >( isLoggedIn, path ).then( - convertNewsletterCategoryResponse - ); - } catch ( e ) {} + const response = await wpcom.req.get< NewsletterCategoryResponse >( { + path, + apiNamespace: 'wpcom/v2', + } ); + return convertNewsletterCategoryResponse( response ); + } catch ( e ) { + return { + enabled: false, + newsletterCategories: [], + error: e instanceof Error ? e.message : 'Unknown error', + }; + } }, enabled: !! siteId, } ); diff --git a/config/jetpack-cloud-development.json b/config/jetpack-cloud-development.json index 0c03f80690439..43df2d93fab2e 100644 --- a/config/jetpack-cloud-development.json +++ b/config/jetpack-cloud-development.json @@ -42,6 +42,7 @@ "current-site/stale-cart-notice": false, "dev/auth-helper": true, "dev/preferences-helper": true, + "individual-subscriber-stats": true, "jetpack-cloud": true, "jetpack/activity-log-sharing": true, "jetpack/agency-dashboard": true, @@ -75,6 +76,7 @@ "layout/support-article-dialog": false, "marketplace-domain-bundle": false, "marketplace-test": false, + "newsletter-categories": true, "oauth": true, "p2-enabled": false, "purchases/new-payment-methods": true, diff --git a/config/jetpack-cloud-horizon.json b/config/jetpack-cloud-horizon.json index a678d7a353f3f..026a2a43abd66 100644 --- a/config/jetpack-cloud-horizon.json +++ b/config/jetpack-cloud-horizon.json @@ -38,6 +38,7 @@ "current-site/notice": false, "current-site/stale-cart-notice": false, "jetpack-cloud": true, + "individual-subscriber-stats": true, "jetpack/agency-dashboard": true, "jetpack/backup-messaging-i3": true, "jetpack/backup-restore-preflight-checks": true, @@ -68,6 +69,7 @@ "layout/support-article-dialog": false, "marketplace-domain-bundle": false, "marketplace-test": false, + "newsletter-categories": true, "oauth": false, "p2-enabled": false, "purchases/new-payment-methods": true, diff --git a/config/jetpack-cloud-production.json b/config/jetpack-cloud-production.json index b50502400eb25..e75b8c532670e 100644 --- a/config/jetpack-cloud-production.json +++ b/config/jetpack-cloud-production.json @@ -41,6 +41,7 @@ "current-site/stale-cart-notice": false, "i18n/community-translator": false, "importers/newsletter": true, + "individual-subscriber-stats": true, "jetpack-cloud": true, "jetpack/agency-dashboard": true, "jetpack/backup-messaging-i3": true, @@ -72,6 +73,7 @@ "logrocket": false, "marketplace-domain-bundle": false, "marketplace-test": false, + "newsletter-categories": true, "oauth": true, "p2-enabled": false, "purchases/new-payment-methods": true, diff --git a/config/jetpack-cloud-stage.json b/config/jetpack-cloud-stage.json index c3a6054ef0f12..466d587acc4f8 100644 --- a/config/jetpack-cloud-stage.json +++ b/config/jetpack-cloud-stage.json @@ -39,6 +39,7 @@ "current-site/domain-warning": false, "current-site/notice": false, "current-site/stale-cart-notice": false, + "individual-subscriber-stats": true, "jetpack-cloud": true, "jetpack/activity-log-sharing": true, "jetpack/agency-dashboard": true, @@ -72,6 +73,7 @@ "layout/support-article-dialog": false, "marketplace-domain-bundle": false, "marketplace-test": false, + "newsletter-categories": true, "oauth": true, "p2-enabled": false, "purchases/new-payment-methods": true,