Skip to content

Commit 5452535

Browse files
manzoorwanijkmatticbot
authored andcommitted
Social | Clean up Social admin page unit tests (#42064)
Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/13560814808 Upstream-Ref: Automattic/jetpack@b578911
1 parent 57b157a commit 5452535

7 files changed

+186
-227
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ This is an alpha version! The changes listed here are not final.
1919
- Social | Improve connect URL generation
2020
- Update package dependencies.
2121

22+
### Fixed
23+
- Clean up Social admin page unit tests
24+
2225
## [0.79.0] - 2025-02-24
2326
### Added
2427
- Add support for Bluesky video selection. [#41669]

src/components/admin-page/test/index.test.jsx

+9-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { render, renderHook, screen } from '@testing-library/react';
2-
import { useSelect, createReduxStore, register } from '@wordpress/data';
3-
import React from 'react';
1+
import { render, screen } from '@testing-library/react';
2+
import { createReduxStore, register } from '@wordpress/data';
43
import { SOCIAL_STORE_CONFIG, SOCIAL_STORE_ID } from '../../../social-store';
4+
import { clearMockedScriptData, mockScriptData } from '../../../utils/test-utils';
55
import { SocialAdminPage } from '../index';
66

77
const store = createReduxStore( SOCIAL_STORE_ID, SOCIAL_STORE_CONFIG );
@@ -11,32 +11,22 @@ describe( 'load the app', () => {
1111
const version = '99.9';
1212

1313
beforeEach( () => {
14-
window.JetpackScriptData = {
15-
site: {
16-
host: 'unknown',
17-
},
14+
mockScriptData( {
1815
social: {
19-
api_paths: {},
2016
plugin_info: {
2117
social: {
2218
version,
2319
},
2420
},
2521
},
26-
user: {
27-
current_user: {
28-
capabilities: {},
29-
},
30-
},
31-
};
22+
} );
23+
} );
24+
25+
afterEach( () => {
26+
clearMockedScriptData();
3227
} );
3328

3429
test( 'container renders', () => {
35-
let storeSelect;
36-
renderHook( () => useSelect( select => ( storeSelect = select( SOCIAL_STORE_ID ) ) ) );
37-
jest.spyOn( storeSelect, 'getSocialSettings' ).mockReset().mockReturnValue( {
38-
showPricingPage: true,
39-
} );
4030
render( <SocialAdminPage /> );
4131
expect( screen.getByText( `Jetpack Social ${ version }` ) ).toBeInTheDocument();
4232
} );
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,58 @@
1-
// eslint-disable-next-line import/order -- This is a test file and we need to import the mocks first
2-
import { mockStore } from '../../../utils/test-mocks.js';
3-
import { isJetpackSelfHostedSite } from '@automattic/jetpack-script-data';
41
import { render, screen } from '@testing-library/react';
5-
import { hasSocialPaidFeatures } from '../../../utils';
2+
import { clearMockedScriptData, mockScriptData } from '../../../utils/test-utils';
63
import AdminPageHeader from '../page-header';
74

85
describe( 'AdminPageHeader', () => {
96
beforeEach( () => {
10-
jest.clearAllMocks();
11-
mockStore();
7+
mockScriptData();
128
} );
139

10+
afterEach( () => {
11+
clearMockedScriptData();
12+
} );
1413
it( 'should show license text when no paid features and is Jetpack site', () => {
15-
hasSocialPaidFeatures.mockReturnValue( false );
16-
isJetpackSelfHostedSite.mockReturnValue( true );
17-
1814
render( <AdminPageHeader /> );
1915
expect(
2016
screen.getByText( /Already have an existing plan or license key\?/i )
2117
).toBeInTheDocument();
2218
expect( screen.getByRole( 'link' ) ).toHaveAttribute(
2319
'href',
24-
'https://example.com/add-license'
20+
expect.stringContaining( 'admin.php?page=my-jetpack#/add-license' )
2521
);
2622
} );
2723

2824
it( 'should not show license text when has paid features', () => {
29-
hasSocialPaidFeatures.mockReturnValue( true );
30-
isJetpackSelfHostedSite.mockReturnValue( true );
31-
25+
mockScriptData( {
26+
site: {
27+
plan: {
28+
features: {
29+
active: [ 'social-enhanced-publishing' ],
30+
},
31+
},
32+
},
33+
} );
3234
render( <AdminPageHeader /> );
3335
expect(
3436
screen.queryByText( /Already have an existing plan or license key\?/i )
3537
).not.toBeInTheDocument();
38+
clearMockedScriptData();
3639
} );
3740

3841
it( 'should not show license text when not a Jetpack site', () => {
39-
hasSocialPaidFeatures.mockReturnValue( false );
40-
isJetpackSelfHostedSite.mockReturnValue( false );
41-
42+
mockScriptData( {
43+
site: {
44+
host: 'wpcom',
45+
plan: {
46+
features: {
47+
active: [ 'social-enhanced-publishing' ],
48+
},
49+
},
50+
},
51+
} );
4252
render( <AdminPageHeader /> );
4353
expect(
4454
screen.queryByText( /Already have an existing plan or license key\?/i )
4555
).not.toBeInTheDocument();
56+
clearMockedScriptData();
4657
} );
4758
} );

src/components/admin-page/test/social-admin-page.test.jsx

+71-59
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,25 @@
1-
// eslint-disable-next-line import/order -- This is a test file and we need to import the mocks first
2-
import { mockStore } from '../../../utils/test-mocks';
1+
jest.mock( '@automattic/jetpack-connection', () => ( {
2+
useConnection: jest.fn(),
3+
useConnectionErrorNotice: jest.fn( () => ( { hasConnectionError: false } ) ),
4+
} ) );
5+
36
import { useConnection } from '@automattic/jetpack-connection';
4-
import { isJetpackSelfHostedSite, siteHasFeature } from '@automattic/jetpack-script-data';
57
import { render, screen } from '@testing-library/react';
68
import { SocialAdminPage } from '../';
7-
import { getSocialScriptData } from '../../../utils';
8-
9-
// Mock child components to simplify testing - We only test the SocialAdminPage component here
10-
jest.mock( '../connection-screen', () => () => <div data-testid="connection-screen" /> );
11-
jest.mock( '../header', () => () => <div data-testid="header" /> );
12-
jest.mock( '../info-section', () => () => <div data-testid="info-section" /> );
13-
jest.mock( '../page-header', () => () => <div data-testid="page-header" /> );
14-
jest.mock( '../pricing-page', () => ( { onDismiss } ) => (
15-
<button data-testid="pricing-page" onClick={ onDismiss } onKeyDown={ onDismiss } />
16-
) );
17-
jest.mock( '../support-section', () => () => <div data-testid="support-section" /> );
18-
jest.mock( '../toggles/social-image-generator-toggle', () => () => (
19-
<div data-testid="social-image-generator-toggle" />
20-
) );
21-
jest.mock( '../toggles/social-module-toggle', () => () => (
22-
<div data-testid="social-module-toggle" />
23-
) );
24-
jest.mock( '../toggles/social-notes-toggle', () => () => (
25-
<div data-testid="social-notes-toggle" />
26-
) );
27-
jest.mock( '../toggles/utm-toggle', () => () => <div data-testid="utm-toggle" /> );
9+
import { clearMockedScriptData, mockScriptData } from '../../../utils/test-utils';
2810

2911
describe( 'SocialAdminPage', () => {
3012
beforeEach( () => {
3113
jest.clearAllMocks();
32-
mockStore();
3314
useConnection.mockReturnValue( {
3415
isUserConnected: true,
3516
isRegistered: true,
3617
} );
37-
isJetpackSelfHostedSite.mockReturnValue( true );
38-
siteHasFeature.mockReturnValue( true );
39-
getSocialScriptData.mockReturnValue( {
40-
plugin_info: {
41-
social: { version: '1.0.0' },
42-
jetpack: { version: '1.0.0' },
43-
},
44-
} );
18+
mockScriptData();
19+
} );
20+
21+
afterEach( () => {
22+
clearMockedScriptData();
4523
} );
4624

4725
describe( 'Page rendering', () => {
@@ -52,88 +30,122 @@ describe( 'SocialAdminPage', () => {
5230
} );
5331

5432
render( <SocialAdminPage /> );
55-
expect( screen.getByTestId( 'connection-screen' ) ).toBeInTheDocument();
33+
expect( screen.getByRole( 'button', { name: 'Get Started' } ) ).toBeInTheDocument();
5634
} );
5735

5836
it( 'should render main admin page when connected', () => {
5937
render( <SocialAdminPage /> );
6038

61-
expect( screen.getByTestId( 'page-header' ) ).toBeInTheDocument();
62-
expect( screen.getByTestId( 'header' ) ).toBeInTheDocument();
63-
expect( screen.getByTestId( 'info-section' ) ).toBeInTheDocument();
64-
expect( screen.getByTestId( 'support-section' ) ).toBeInTheDocument();
39+
expect( screen.getByText( 'Write once, post everywhere' ) ).toBeInTheDocument();
40+
expect( screen.getByText( 'Did you know?' ) ).toBeInTheDocument();
6541
} );
6642

6743
it( 'should render pricing page when showPricingPage is true and no paid features', () => {
68-
mockStore( {
69-
getSocialSettings: () => ( { showPricingPage: true } ),
44+
mockScriptData( {
45+
social: {
46+
settings: {
47+
showPricingPage: true,
48+
},
49+
},
7050
} );
7151

7252
render( <SocialAdminPage /> );
73-
expect( screen.getByTestId( 'pricing-page' ) ).toBeInTheDocument();
53+
54+
expect( screen.getByText( 'Start for free' ) ).toBeInTheDocument();
55+
56+
clearMockedScriptData();
7457
} );
7558
} );
7659

7760
describe( 'Toggle visibility', () => {
7861
describe( 'UTM toggle', () => {
7962
it( 'should show when module is enabled', () => {
8063
render( <SocialAdminPage /> );
81-
expect( screen.getByTestId( 'utm-toggle' ) ).toBeInTheDocument();
64+
expect( screen.getByText( 'Append UTM parameters to shared URLs' ) ).toBeInTheDocument();
8265
} );
8366

8467
it( 'should not show when module is disabled', () => {
85-
mockStore( {
86-
getSocialModuleSettings: () => ( { publicize: false } ),
68+
mockScriptData( {
69+
social: {
70+
is_publicize_enabled: false,
71+
},
8772
} );
8873
render( <SocialAdminPage /> );
89-
expect( screen.queryByTestId( 'utm-toggle' ) ).not.toBeInTheDocument();
74+
expect(
75+
screen.queryByText( 'Append UTM parameters to shared URLs' )
76+
).not.toBeInTheDocument();
77+
78+
clearMockedScriptData();
9079
} );
9180
} );
9281

9382
describe( 'Social Notes toggle', () => {
9483
it( 'should show when plugin is active and module is enabled', () => {
9584
render( <SocialAdminPage /> );
96-
expect( screen.getByTestId( 'social-notes-toggle' ) ).toBeInTheDocument();
85+
expect( screen.getByText( 'Enable Social Notes' ) ).toBeInTheDocument();
9786
} );
9887

9988
it( 'should not show when plugin is not active', () => {
100-
getSocialScriptData.mockReturnValue( {
101-
plugin_info: {
102-
social: { version: null },
103-
jetpack: { version: '1.0.0' },
89+
mockScriptData( {
90+
social: {
91+
plugin_info: {
92+
social: { version: null },
93+
jetpack: { version: '1.0.0' },
94+
},
10495
},
10596
} );
10697
render( <SocialAdminPage /> );
107-
expect( screen.queryByTestId( 'social-notes-toggle' ) ).not.toBeInTheDocument();
98+
expect( screen.queryByText( 'Enable Social Notes' ) ).not.toBeInTheDocument();
99+
100+
clearMockedScriptData();
108101
} );
109102

110103
it( 'should not show when module is disabled', () => {
111-
mockStore( {
112-
getSocialModuleSettings: () => ( { publicize: false } ),
104+
mockScriptData( {
105+
social: {
106+
is_publicize_enabled: false,
107+
},
113108
} );
114109
render( <SocialAdminPage /> );
115110
expect( screen.queryByTestId( 'social-notes-toggle' ) ).not.toBeInTheDocument();
111+
clearMockedScriptData();
116112
} );
117113
} );
118114

119115
describe( 'Social Image Generator toggle', () => {
120116
it( 'should show when feature is available and module is enabled', () => {
117+
mockScriptData( {
118+
site: {
119+
plan: {
120+
features: {
121+
active: [ 'social-image-generator' ],
122+
},
123+
},
124+
},
125+
} );
121126
render( <SocialAdminPage /> );
122-
expect( screen.getByTestId( 'social-image-generator-toggle' ) ).toBeInTheDocument();
127+
128+
expect( screen.getByText( 'Enable Social Image Generator' ) ).toBeInTheDocument();
129+
130+
clearMockedScriptData();
123131
} );
124132

125133
it( 'should not show when feature is not available', () => {
126-
siteHasFeature.mockReturnValue( false );
134+
mockScriptData();
127135
render( <SocialAdminPage /> );
128-
expect( screen.queryByTestId( 'social-image-generator-toggle' ) ).not.toBeInTheDocument();
136+
expect( screen.queryByText( 'Enable Social Image Generator' ) ).not.toBeInTheDocument();
137+
clearMockedScriptData();
129138
} );
130139

131140
it( 'should not show when module is disabled', () => {
132-
mockStore( {
133-
getSocialModuleSettings: () => ( { publicize: false } ),
141+
mockScriptData( {
142+
social: {
143+
is_publicize_enabled: false,
144+
},
134145
} );
135146
render( <SocialAdminPage /> );
136-
expect( screen.queryByTestId( 'social-image-generator-toggle' ) ).not.toBeInTheDocument();
147+
expect( screen.queryByText( 'Enable Social Image Generator' ) ).not.toBeInTheDocument();
148+
clearMockedScriptData();
137149
} );
138150
} );
139151
} );

0 commit comments

Comments
 (0)