File size: 2,897 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
/**
 * @group gutenberg
 * @group calypso-pr
 * @group calypso-release
 */

import {
	DataHelper,
	ElementHelper,
	PublishedPostPage,
	TestAccount,
	envVariables,
	getTestAccountByFeature,
	envToFeatureKey,
	RestAPIClient,
	PostResponse,
} from '@automattic/calypso-e2e';
import { Page, Browser } from 'playwright';

declare const browser: Browser;

describe( 'Likes: Post', function () {
	const features = envToFeatureKey( envVariables );
	// @todo Does it make sense to create a `simpleSitePersonalPlanUserEdge` with GB edge?
	// for now, it will pick up the default `gutenbergAtomicSiteEdgeUser` if edge is set.
	const accountName = getTestAccountByFeature( features, [
		{
			gutenberg: 'stable',
			siteType: 'simple',
			accountName: 'simpleSitePersonalPlanUser',
		},
	] );

	const postingUser = new TestAccount( accountName );
	const otherUser = new TestAccount( 'defaultUser' );
	let page: Page;
	let restAPIClient: RestAPIClient;

	let newPost: PostResponse;

	beforeAll( async () => {
		page = await browser.newPage();
		await postingUser.authenticate( page );
	} );

	it( 'Setup the test', async function () {
		restAPIClient = new RestAPIClient( postingUser.credentials );
		newPost = await restAPIClient.createPost(
			postingUser.credentials.testSites?.primary.id as number,
			{
				title: DataHelper.getRandomPhrase(),
			}
		);
	} );

	describe( 'While authenticated', function () {
		let publishedPostPage: PublishedPostPage;

		it( 'View post', async function () {
			await ElementHelper.reloadAndRetry( page, async () => {
				await page.goto( newPost.URL, { timeout: 20 * 1000 } );
			} );
		} );

		it( 'Like post', async function () {
			await ElementHelper.reloadAndRetry( page, async () => {
				publishedPostPage = new PublishedPostPage( page );
				await publishedPostPage.likePost();
			} );
		} );

		it( 'Unlike post', async function () {
			await ElementHelper.reloadAndRetry( page, async () => {
				publishedPostPage = new PublishedPostPage( page );
				await publishedPostPage.unlikePost();
			} );
		} );
	} );

	describe( 'While unauthenticated', function () {
		let newPage: Page;
		let publishedPostPage: PublishedPostPage;

		beforeAll( async () => {
			newPage = await browser.newPage();
		} );

		it( 'Go to the published post page', async () => {
			await ElementHelper.reloadAndRetry( newPage, async () => {
				await newPage.goto( newPost.URL, { timeout: 20 * 1000 } );
			} );
		} );

		it( 'Login via popup to like the post', async function () {
			newPage.on( 'popup', async ( popup ) => {
				await otherUser.logInViaPopupPage( popup );
			} );

			publishedPostPage = new PublishedPostPage( newPage );
			await publishedPostPage.likePost();
		} );
	} );

	afterAll( async function () {
		if ( ! newPost ) {
			return;
		}

		await restAPIClient.deletePost(
			postingUser.credentials.testSites?.primary.id as number,
			newPost.ID
		);
	} );
} );