File size: 9,371 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
 * @group calypso-pr
 * @group jetpack-wpcom-integration
 */

import {
	DataHelper,
	EditorPage,
	envToFeatureKey,
	envVariables,
	getTestAccountByFeature,
	SocialConnectionsManager,
	TestAccount,
	TestAccountName,
} from '@automattic/calypso-e2e';
import { Browser, Page } from 'playwright';
import { skipDescribeIf } from '../../jest-helpers';

declare const browser: Browser;

const features4SimpleSites = {
	resharing: false,
	manualSharing: true,
	mediaSharing: false,
	socialImageGenerator: false,
};

const features4BusinessPlan = {
	resharing: true,
	manualSharing: true,
	mediaSharing: true,
	socialImageGenerator: true,
};

const testCases: Array< {
	plan: string;
	platform: 'Simple' | 'Atomic';
	testAccountName: TestAccountName;
	features: Record<
		'resharing' | 'manualSharing' | 'mediaSharing' | 'socialImageGenerator',
		boolean
	>;
	isPrivate?: boolean;
} > = [];

if ( envVariables.JETPACK_TARGET === 'wpcom-deployment' ) {
	testCases.push( {
		plan: envVariables.TEST_ON_ATOMIC ? 'Paid' : 'Business',
		platform: envVariables.TEST_ON_ATOMIC ? 'Atomic' : 'Simple',
		testAccountName: getTestAccountByFeature( envToFeatureKey( envVariables ) ),
		features: features4BusinessPlan,
		isPrivate: envVariables.TEST_ON_ATOMIC && envVariables.ATOMIC_VARIATION === 'private',
	} );
} else {
	testCases.push(
		{
			plan: 'Free',
			platform: 'Simple',
			testAccountName: 'simpleSiteFreePlanUser',
			features: features4SimpleSites,
		},
		{
			plan: 'Personal',
			platform: 'Simple',
			testAccountName: 'simpleSitePersonalPlanUser',
			features: features4SimpleSites,
		}
	);
}

/**
 * Tests features offered by Jetpack Social on a Simple site with Free plan.
 *
 * Keywords: Social, Jetpack, Publicize, Editor
 */
describe( DataHelper.createSuiteTitle( 'Social: Editor features' ), function () {
	for ( const { plan, platform, testAccountName, features, isPrivate } of testCases ) {
		const title = `For ${ platform } sites with ${ plan } plan`;

		skipDescribeIf( isPrivate ?? false )( DataHelper.createSuiteTitle( title ), function () {
			let page: Page;
			let editorPage: EditorPage;
			let socialConnectionsManager: SocialConnectionsManager;

			let siteSlug: string;
			let siteId: number;

			beforeAll( async () => {
				page = await browser.newPage();
				editorPage = new EditorPage( page );

				const testAccount = new TestAccount( testAccountName );
				siteId = testAccount.credentials.testSites?.primary?.id || 0;
				siteSlug = testAccount.getSiteURL( { protocol: false } );
				socialConnectionsManager = new SocialConnectionsManager( page, siteId! );
				await testAccount.authenticate( page );
			} );

			beforeEach( async () => {
				await editorPage.visit( 'post', { siteSlug } );
			} );

			afterEach( async () => {
				await socialConnectionsManager.clearIntercepts();
			} );

			test( 'Should verify that auto-sharing is available for new posts', async function () {
				await socialConnectionsManager.interceptRequests();

				await Promise.all( [
					// Open the Jetpack sidebar.
					editorPage.openSettings( 'Jetpack' ),
					// Wait for the connection test results to finish
					socialConnectionsManager.waitForConnectionTests(),
				] );

				// Expand the Publicize panel.
				const section = await editorPage.expandSection( 'Share this post' );

				// Verify that the toggle is enabled.
				const toggle = section.getByLabel( 'Share when publishing' );
				expect( await toggle.isChecked() ).toBe( true );

				// Verify that the message box is editable.
				const messageBox = section.getByRole( 'textbox', { name: 'Message' } );
				expect( await messageBox.isEditable() ).toBe( true );

				// Verify whether the media button is available.
				const mediaButton = section.getByRole( 'button', { name: 'Choose Media' } );
				expect( await mediaButton.isVisible() ).toBe( features.mediaSharing );
			} );

			test( `Should verify that resharing ${
				features.resharing ? 'IS' : 'is NOT'
			} available`, async function () {
				let connectionTestPromise = Promise.resolve();
				await socialConnectionsManager.interceptRequests();

				connectionTestPromise = socialConnectionsManager.waitForConnectionTests();

				// Open the Jetpack sidebar.
				await editorPage.openSettings( 'Jetpack' );

				await connectionTestPromise;

				// Expand the Publicize panel.
				let section = await editorPage.expandSection( 'Share this post' );

				// Verify that resharing button is not visible on new posts in the share modal
				let sharePostModalButton = section.getByRole( 'button', {
					name: 'Preview social posts',
					exact: true,
				} );
				await sharePostModalButton.click();

				const shareModal = ( await editorPage.getEditorParent() ).getByRole( 'dialog' ).filter( {
					hasText: 'Social Preview',
				} );

				await shareModal.waitFor();
				let reshareButton = shareModal.getByRole( 'button', { name: 'Share', exact: true } );

				expect( await reshareButton.isVisible() ).toBe( false );

				let closeButton = shareModal.getByRole( 'button', { name: 'Close' } );
				await closeButton.click();

				// Set a title for the post
				await editorPage.enterTitle( 'Resharing: ' + DataHelper.getRandomPhrase() );
				// Publish the post.
				await editorPage.publish();
				connectionTestPromise = socialConnectionsManager.waitForConnectionTests();
				await editorPage.closeAllPanels();

				// Open the Jetpack sidebar.
				await editorPage.openSettings( 'Jetpack' );

				await connectionTestPromise;

				// Expand the Publicize panel.
				section = await editorPage.expandSection( 'Share this post' );

				// Verify whether the auto-share toggle is no longer visible.
				const toggle = section.getByLabel( 'Share when publishing' );
				expect( await toggle.isVisible() ).toBe( false );

				// Check if the Preview & Share button is visible based on resharing feature
				sharePostModalButton = section.getByRole( 'button', {
					name: 'Preview & Share',
					exact: true,
				} );

				expect( await sharePostModalButton.isVisible() ).toBe( features.resharing );

				let isReshareButtonVisible = false;

				if ( features.resharing ) {
					await sharePostModalButton.click();

					const shareModal = ( await editorPage.getEditorParent() ).getByRole( 'dialog' ).filter( {
						hasText: 'Share Post',
					} );
					await shareModal.waitFor();

					// Look for the Share button within the modal dialog
					reshareButton = shareModal.getByRole( 'button', { name: 'Share', exact: true } );
					isReshareButtonVisible = await reshareButton.isVisible();

					// Close the share post modal by clicking the Close button within the modal
					closeButton = shareModal.getByRole( 'button', { name: 'Close' } );
					await closeButton.click();
				}
				expect( isReshareButtonVisible ).toBe( features.resharing );

				// Verify whether the upgrade nudge/link is visible.
				if ( ! features.resharing ) {
					const upgradeLink = section.getByRole( 'link', { name: 'Upgrade now' } );
					// The upgrade CTA is a button instead of a link until some API call finishes to get the checkout URL, which makes it a link.
					// So, we need to wait for the link to appear.
					await upgradeLink.waitFor();
				}

				const content = await section.textContent();

				const message = 'To re-share a post, you need to upgrade to the WordPress.com Premium plan';

				expect( content?.includes( message ) ).toBe( ! features.resharing );
			} );

			test( `Should verify that manual sharing ${
				features.manualSharing ? 'IS' : 'is NOT'
			} available`, async function () {
				// Open the Jetpack sidebar.
				await editorPage.openSettings( 'Jetpack' );

				// Expand the Publicize panel.
				let section = await editorPage.expandSection( 'Share this post' );

				// Verify that manual sharing is NOT available before publishing.
				let manualSharing = section.getByRole( 'paragraph', { name: 'Manual sharing' } );
				expect( await manualSharing.isVisible() ).toBe( false );

				// Set a title for the post
				await editorPage.enterTitle( 'Manual sharing: ' + DataHelper.getRandomPhrase() );
				// Publish the post.
				await editorPage.publish();

				// Verify whether the manual sharing is available on post publish panel
				manualSharing = ( await editorPage.getPublishPanelRoot() ).getByRole( 'button', {
					name: 'Manual sharing',
				} );

				if ( features.manualSharing ) {
					await manualSharing.waitFor();
				}

				// Close the post publish panel.
				await editorPage.closeAllPanels();

				// Open the Jetpack sidebar.
				await editorPage.openSettings( 'Jetpack' );

				// Expand the Publicize panel.
				section = await editorPage.expandSection( 'Share this post' );

				// Verify whether manual sharing is available in the Jetpack sidebar.
				manualSharing = section.getByText( 'Manual sharing' );
				expect( await manualSharing.isVisible() ).toBe( features.manualSharing );
			} );

			test( `Should verify that Social Image Generator ${
				features.socialImageGenerator ? 'IS' : 'is NOT'
			} available`, async function () {
				// Open the Jetpack sidebar.
				await editorPage.openSettings( 'Jetpack' );

				const section = await editorPage.getSettingsSection( 'Social Image Generator' );

				// Verify whether the Social Image Generator panel exists.
				expect( await section.isVisible() ).toBe( features.socialImageGenerator );
			} );
		} );
	}
} );