Passed
Pull Request — master (#5)
by Jesús
01:51
created

test/e2e/disabled-boxes-ui.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 109
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 97
mnd 1
bc 1
fnc 0
dl 0
loc 109
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { test, expect } from '@playwright/test';
2
3
test.describe('Disabled Boxes - UI Interactions', () => {
4
  test.beforeEach(async ({ page }) => {
5
    await page.goto('/');
6
    await page.waitForSelector('.grid');
7
  });
8
9
  test('should not allow clicking disabled 2048 box', async ({ page }) => {
10
    const boxes = page.locator('.box');
11
    const box2048 = boxes.nth(0);
12
    const box1024 = boxes.nth(1);
13
    const indexDisplay = page.locator('#index');
14
15
    await box1024.click();
16
    await expect(indexDisplay).toHaveText('1024');
17
18
    await box2048.click({ force: true });
19
20
    await expect(indexDisplay).toHaveText('1024');
21
    await expect(box2048).not.toHaveClass(/active/);
22
  });
23
24
  test('should not allow clicking other disabled boxes when 2048 is active', async ({ page }) => {
25
    const boxes = page.locator('.box');
26
    const box2048 = boxes.nth(0);
27
    const box1024 = boxes.nth(1);
28
    const wordDisplay = page.locator('#word-input');
29
30
    await box2048.click();
31
    
32
    await expect(wordDisplay).not.toHaveClass(/error/);
33
    
34
    await box1024.click({ force: true });
35
    
36
    await expect(box1024).not.toHaveClass(/active/);
37
    await expect(box2048).toHaveClass(/active/);
38
  });
39
40
  test('should enable all boxes after reset', async ({ page }) => {
41
    const boxes = page.locator('.box');
42
    const resetButton = page.locator('#reset');
43
44
    await boxes.nth(0).click();
45
    
46
    await expect(boxes.nth(1)).toHaveClass(/disabled/);
47
    
48
    await resetButton.click();
49
    
50
    for (let i = 0; i < 12; i++) {
51
      await expect(boxes.nth(i)).not.toHaveClass(/disabled/);
52
    }
53
  });
54
55
  test('should not allow keyboard navigation to trigger disabled boxes', async ({ page }) => {
56
    const boxes = page.locator('.box');
57
    const box2048 = boxes.nth(0);
58
    const box1024 = boxes.nth(1);
59
60
    await box1024.click();
61
    
62
    await box2048.focus();
63
    await page.keyboard.press('Enter');
64
    
65
    await expect(box2048).not.toHaveClass(/active/);
66
    await expect(box1024).toHaveClass(/active/); // 1024 should still be active
67
  });
68
69
  test('should allow keyboard navigation through enabled boxes only', async ({ page }) => {
70
    const boxes = page.locator('.box');
71
72
    await boxes.nth(0).click();
73
    
74
    await boxes.nth(0).focus();
75
    await page.keyboard.press('ArrowRight');
76
77
    await page.keyboard.press('Enter');
78
    
79
    await expect(boxes.nth(1)).not.toHaveClass(/active/);
80
  });
81
82
  test('should have visual disabled state (disabled class)', async ({ page }) => {
83
    const boxes = page.locator('.box');
84
    const box2048 = boxes.nth(0);
85
    const box1024 = boxes.nth(1);
86
87
    await box1024.click();
88
    
89
    await expect(box2048).toHaveClass(/disabled/);
90
    
91
    await expect(box2048).toHaveAttribute('aria-disabled', 'true');
92
  });
93
94
  test('should show not-allowed cursor on disabled boxes', async ({ page }) => {
95
    const box2048 = page.locator('.box').nth(0);
96
    const box1024 = page.locator('.box').nth(1);
97
98
    await box1024.click();
99
    
100
    await expect(box2048).toHaveClass(/disabled/);
101
    
102
    const cursor = await box2048.evaluate((el) => 
103
      window.getComputedStyle(el).cursor
104
    );
105
    
106
    expect(cursor).toBe('not-allowed');
107
  });
108
});
109