File size: 3,240 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
/**
 * @group gutenberg
 */

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

declare const browser: Browser;

describe( 'Likes: Comment', 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 postContent =
		'The foolish man seeks happiness in the distance. The wise grows it under his feet.\n— James Oppenheim';
	let page: Page;
	let commentsComponent: CommentsComponent;
	let testAccount: TestAccount;
	let newPost: PostResponse;
	let commentToBeLiked: NewCommentResponse;
	let commentToBeUnliked: NewCommentResponse;
	let restAPIClient: RestAPIClient;

	it( 'Setup the test', async function () {
		page = await browser.newPage();

		testAccount = new TestAccount( accountName );

		restAPIClient = new RestAPIClient( testAccount.credentials );

		newPost = await restAPIClient.createPost(
			testAccount.credentials.testSites?.primary.id as number,
			{
				title: DataHelper.getTimestamp() as string,
				content: postContent,
			}
		);

		commentToBeLiked = await restAPIClient.createComment(
			testAccount.credentials.testSites?.primary.id as number,
			newPost.ID,
			DataHelper.getRandomPhrase()
		);
		commentToBeUnliked = await restAPIClient.createComment(
			testAccount.credentials.testSites?.primary.id as number,
			newPost.ID,
			DataHelper.getRandomPhrase()
		);

		// The comment takes some time to settle. If we request the like
		// immediately we might be getting the `unknown_comment` error. Let's do
		// a few retries to make sure the like is getting through.
		const likeRetryCount = 10;
		for ( let i = 0; i <= likeRetryCount; i++ ) {
			try {
				await restAPIClient.commentAction(
					'like',
					testAccount.credentials.testSites?.primary.id as number,
					commentToBeUnliked.ID
				);
				break;
			} catch ( error ) {
				if ( i === likeRetryCount ) {
					throw error;
				}
				await page.waitForTimeout( 1000 );
			}
		}

		await testAccount.authenticate( page );
	} );

	it( 'View the post', async function () {
		await page.goto( newPost.URL );
	} );

	it( 'Like the comment', async function () {
		commentsComponent = new CommentsComponent( page );
		await commentsComponent.like( commentToBeLiked.raw_content );
	} );

	it( 'Unlike the comment', async function () {
		if ( envVariables.TEST_ON_ATOMIC ) {
			// AT comments appear unable to respond to `scrollIntoViewIfNeeded`
			// unless the focus is "unstuck" by shiting the page.
			await page.mouse.wheel( 0, 120 );
		}
		await commentsComponent.unlike( commentToBeUnliked.raw_content );
	} );

	afterAll( async function () {
		if ( ! newPost ) {
			return;
		}
		await restAPIClient.deletePost(
			testAccount.credentials.testSites?.primary.id as number,
			newPost.ID
		);
	} );
} );