-
Notifications
You must be signed in to change notification settings - Fork 6
/
cards.spec.ts
110 lines (95 loc) · 4.25 KB
/
cards.spec.ts
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
import { test, expect, Page } from "@playwright/test";
import type { SwipeType } from "types";
const elements = {
activeCard: 'div[data-testid="active-card"]',
likeCount: 'div[data-testid="like-count"]',
nopeCount: 'div[data-testid="nope-count"]',
superlikeCount: 'div[data-testid="superlike-count"]',
undoBtn: 'button[data-testid="undo-btn"]',
};
const swipeCard = async (
page: Page,
swipeType: SwipeType,
point: { x: number; y: number }
) => {
const { x, y } = point;
await page.mouse.move(x, y);
await page.mouse.click(x, y);
await page.mouse.down();
if (swipeType === "superlike") {
await page.mouse.move(x, y - 2000);
} else {
await page.mouse.move(x + (swipeType === "like" ? +1000 : -1000), y);
}
await page.mouse.up();
};
test("should have active card & default state", async ({ page }) => {
// Start from the index page (the baseURL is set via the webServer in the playwright.config.ts)
await page.goto("http://localhost:3000/");
await expect(page.locator(elements.activeCard)).toBeVisible();
await expect(page.locator(elements.undoBtn)).toBeVisible();
await expect(page.locator(elements.likeCount)).toHaveText("0");
await expect(page.locator(elements.nopeCount)).toHaveText("0");
});
test("should swipe and change count", async ({ page }) => {
await page.goto("http://localhost:3000/");
// get the bounding box of active card
const bb = await page.locator(elements.activeCard).boundingBox();
// get the center position of active card
if (!bb) return;
const center = { x: bb.x + bb.width / 2, y: bb.y + bb.height / 2 };
await swipeCard(page, "nope", center);
await expect(page.locator(elements.nopeCount)).toHaveText("1");
await swipeCard(page, "like", center);
await expect(page.locator(elements.likeCount)).toHaveText("1");
await swipeCard(page, "nope", center);
await expect(page.locator(elements.nopeCount)).toHaveText("2");
await swipeCard(page, "like", center);
await expect(page.locator(elements.likeCount)).toHaveText("2");
});
test("should swipe & undo + change count", async ({ page }) => {
await page.goto("http://localhost:3000/");
// get the bounding box of active card
const bb = await page.locator(elements.activeCard).boundingBox();
// get the center position of active card
if (!bb) return;
const center = { x: bb.x + bb.width / 2, y: bb.y + bb.height / 2 };
await swipeCard(page, "nope", center);
await expect(page.locator(elements.nopeCount)).toHaveText("1");
await swipeCard(page, "like", center);
await expect(page.locator(elements.likeCount)).toHaveText("1");
await swipeCard(page, "nope", center);
await expect(page.locator(elements.nopeCount)).toHaveText("2");
await swipeCard(page, "like", center);
await expect(page.locator(elements.likeCount)).toHaveText("2");
await swipeCard(page, "superlike", center);
await expect(page.locator(elements.superlikeCount)).toHaveText("1");
// Undo
await page.locator(elements.undoBtn).click();
await expect(page.locator(elements.superlikeCount)).toHaveText("0");
await page.locator(elements.undoBtn).click();
await expect(page.locator(elements.likeCount)).toHaveText("1");
await page.locator(elements.undoBtn).click();
await expect(page.locator(elements.nopeCount)).toHaveText("1");
});
test("should superlike & undo + change count", async ({ page }) => {
await page.goto("http://localhost:3000/");
// get the bounding box of active card
const bb = await page.locator(elements.activeCard).boundingBox();
// get the center position of active card
if (!bb) return;
const center = { x: bb.x + bb.width / 2, y: bb.y + bb.height / 2 };
await swipeCard(page, "superlike", center);
await expect(page.locator(elements.superlikeCount)).toHaveText("1");
await swipeCard(page, "like", center);
await expect(page.locator(elements.likeCount)).toHaveText("1");
await swipeCard(page, "nope", center);
await expect(page.locator(elements.nopeCount)).toHaveText("1");
// Undo
await page.locator(elements.undoBtn).click();
await expect(page.locator(elements.nopeCount)).toHaveText("0");
await page.locator(elements.undoBtn).click();
await expect(page.locator(elements.likeCount)).toHaveText("0");
await page.locator(elements.undoBtn).click();
await expect(page.locator(elements.superlikeCount)).toHaveText("0");
});