|
1 | | -import { expect, test, type Locator, type Page } from "@playwright/test" |
2 | | -import { a11y, controls, part, testid } from "./_utils" |
| 1 | +import { test } from "@playwright/test" |
| 2 | +import { controls } from "./_utils" |
| 3 | +import { RatingGroupModel } from "./models/rating-group.model" |
3 | 4 |
|
4 | | -const control = part("control") |
5 | | -const hiddenInput = testid("hidden-input") |
6 | | -const rating = part("item") |
7 | | - |
8 | | -const getRating = (page: Page, value: number): Locator => { |
9 | | - return page.locator(rating).nth(value - 1) |
10 | | -} |
11 | | - |
12 | | -const expectToBeHighlighted = (el: Locator) => { |
13 | | - return expect(el).toHaveAttribute("data-highlighted", "") |
14 | | -} |
15 | | - |
16 | | -const expectToBeDisabled = (el: Locator) => { |
17 | | - return expect(el).toHaveAttribute("data-disabled", "") |
18 | | -} |
19 | | - |
20 | | -const pointerover = (el: Locator) => { |
21 | | - el.hover() |
22 | | - return el.dispatchEvent("pointermove", { button: 0 }) |
23 | | -} |
| 5 | +let I: RatingGroupModel |
24 | 6 |
|
25 | 7 | test.describe("rating / pointer", () => { |
26 | 8 | test.beforeEach(async ({ page }) => { |
27 | | - await page.goto("/rating-group") |
| 9 | + I = new RatingGroupModel(page) |
| 10 | + await I.goto() |
28 | 11 | }) |
29 | 12 |
|
30 | | - test("should be accessible", async ({ page }) => { |
31 | | - await a11y(page) |
| 13 | + test("should be accessible", async () => { |
| 14 | + await I.checkAccessibility() |
32 | 15 | }) |
33 | 16 |
|
34 | | - test("should set value when item is clicked", async ({ page }) => { |
35 | | - await getRating(page, 4).click() |
36 | | - await expect(page.locator(hiddenInput)).toHaveValue("4") |
| 17 | + test("should set value when item is clicked", async () => { |
| 18 | + await I.clickRating(4) |
| 19 | + await I.seeInputHasValue("4") |
37 | 20 | }) |
38 | 21 |
|
39 | | - test.skip("highlight when hovered", async ({ page }) => { |
40 | | - const el = getRating(page, 4) |
| 22 | + test.skip("highlight when hovered", async () => { |
| 23 | + await I.hoverRating(4) |
| 24 | + await I.seeRatingIsHighlighted(4) |
41 | 25 |
|
42 | | - await pointerover(el) |
43 | | - await expectToBeHighlighted(el) |
| 26 | + await I.hoverOut() |
| 27 | + await I.seeRatingIsHighlighted(3) |
44 | 28 |
|
45 | | - // hover out |
46 | | - page.hover("main") |
47 | | - await expectToBeHighlighted(getRating(page, 3)) |
| 29 | + await I.hoverRating(4) |
| 30 | + await I.clickRating(4) |
| 31 | + await I.seeInputHasValue("4") |
| 32 | + }) |
48 | 33 |
|
49 | | - await pointerover(el) |
50 | | - el.click() |
51 | | - await expect(page.locator(hiddenInput)).toHaveValue("4") |
| 34 | + test("clicking label should focus 3rd item", async () => { |
| 35 | + await I.clickLabel() |
| 36 | + await I.seeRatingIsFocused(3) |
52 | 37 | }) |
53 | 38 | }) |
54 | 39 |
|
55 | 40 | test.describe("rating / properties", () => { |
56 | 41 | test.beforeEach(async ({ page }) => { |
57 | | - await page.goto("/rating-group") |
| 42 | + I = new RatingGroupModel(page) |
| 43 | + await I.goto() |
58 | 44 | }) |
59 | 45 |
|
60 | | - test("should not be selectable when disabled", async ({ page }) => { |
61 | | - await controls(page).bool("disabled") |
62 | | - await expectToBeDisabled(page.locator(control)) |
63 | | - const items = page.locator(rating) |
64 | | - const isAllItemsDisabled = await items.evaluateAll((items) => items.every((item) => item.dataset.disabled === "")) |
65 | | - expect(isAllItemsDisabled).toBeTruthy() |
| 46 | + test("should not be selectable when disabled", async () => { |
| 47 | + await controls(I.page).bool("disabled") |
| 48 | + await I.seeControlIsDisabled() |
| 49 | + await I.seeAllRatingsAreDisabled() |
66 | 50 | }) |
67 | 51 |
|
68 | | - test("should not be selectable when is readonly", async ({ page }) => { |
69 | | - await controls(page).bool("readOnly") |
70 | | - const items = page.locator(rating) |
71 | | - const isAllItemsReadonly = await items.evaluateAll((items) => items.every((item) => item.dataset.readonly === "")) |
72 | | - expect(isAllItemsReadonly).toBeTruthy() |
| 52 | + test("should not be selectable when is readonly", async () => { |
| 53 | + await controls(I.page).bool("readOnly") |
| 54 | + await I.seeAllRatingsAreReadonly() |
73 | 55 | }) |
74 | 56 | }) |
75 | 57 |
|
76 | 58 | test.describe("rating / keyboard", () => { |
77 | 59 | test.beforeEach(async ({ page }) => { |
78 | | - await page.goto("/rating-group") |
| 60 | + I = new RatingGroupModel(page) |
| 61 | + await I.goto() |
79 | 62 | }) |
80 | 63 |
|
81 | | - test("should select value on arrow left/right", async ({ page }) => { |
82 | | - await getRating(page, 3).focus() |
| 64 | + test("should select value on arrow left/right", async () => { |
| 65 | + await I.focusRating(3) |
83 | 66 |
|
84 | | - await page.keyboard.press("ArrowRight") |
85 | | - await page.keyboard.press("ArrowRight") |
86 | | - await expect(getRating(page, 4)).toBeFocused() |
| 67 | + await I.pressKey("ArrowRight") |
| 68 | + await I.pressKey("ArrowRight") |
| 69 | + await I.seeRatingIsFocused(4) |
87 | 70 |
|
88 | | - await page.keyboard.press("ArrowLeft") |
89 | | - await page.keyboard.press("ArrowLeft") |
90 | | - await expect(getRating(page, 3)).toBeFocused() |
| 71 | + await I.pressKey("ArrowLeft") |
| 72 | + await I.pressKey("ArrowLeft") |
| 73 | + await I.seeRatingIsFocused(3) |
91 | 74 | }) |
92 | 75 |
|
93 | | - test("should select value on arrow home/end", async ({ page }) => { |
94 | | - await getRating(page, 3).focus() |
| 76 | + test("should select value on arrow home/end", async () => { |
| 77 | + await I.focusRating(3) |
95 | 78 |
|
96 | | - await page.keyboard.press("Home") |
97 | | - await expect(getRating(page, 1)).toBeFocused() |
98 | | - await expectToBeHighlighted(getRating(page, 1)) |
| 79 | + await I.pressKey("Home") |
| 80 | + await I.seeRatingIsFocused(1) |
| 81 | + await I.seeRatingIsHighlighted(1) |
99 | 82 |
|
100 | | - await page.keyboard.press("End") |
101 | | - await expect(getRating(page, 5)).toBeFocused() |
102 | | - await expectToBeHighlighted(getRating(page, 5)) |
| 83 | + await I.pressKey("End") |
| 84 | + await I.seeRatingIsFocused(5) |
| 85 | + await I.seeRatingIsHighlighted(5) |
103 | 86 | }) |
104 | 87 | }) |
0 commit comments