Skip to content

Commit

Permalink
Newsletters: individual-subscriber-stats and newsletter-categories fe…
Browse files Browse the repository at this point in the history
…atures in jpcloud (#97089)
  • Loading branch information
lezama authored Dec 5, 2024
1 parent ce42481 commit f392ac3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
Expand Down Expand Up @@ -69,6 +73,7 @@ describe( 'useSubscribedNewsletterCategories', () => {
await waitFor( () => expect( result.current.isSuccess ).toBe( true ) );

expect( result.current.data ).toEqual( {
enabled: true,
newsletterCategories: [
{
id: 1,
Expand All @@ -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: [],
} );

Expand All @@ -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',
} );
} );
} );
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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`;
Expand All @@ -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,
} );
Expand Down
2 changes: 2 additions & 0 deletions config/jetpack-cloud-development.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions config/jetpack-cloud-horizon.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions config/jetpack-cloud-production.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions config/jetpack-cloud-stage.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f392ac3

Please sign in to comment.