Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

mobile.spec.ts

205 lines6.3 KB

1import { expect, test } from "@playwright/test"
2import { openGame } from "./helpers"
3​
4// Test mobile layouts on actual device viewports
5test.describe(`Mobile Layout`, () => {
6 test.use({ viewport: { width: 390, height: 844 } }) // iPhone 13 size
7​
8 test.beforeEach(async ({ page }) => {
9 await openGame(page)
10 })
11​
12 test(`mobile layout displays correctly`, async ({ page }) => {
13 // Core UI elements should be visible
14 await expect(page.getByTestId(`team-badge`).filter({ visible: true })).toBeVisible()
15 await expect(page.getByTestId(`quota-meter`)).toBeVisible()
16​
17 // World view should be visible
18 const worldView = page.getByTestId(`world-view`)
19 await expect(worldView).toBeVisible()
20​
21 // Check that world view takes appropriate space
22 const box = await worldView.boundingBox()
23 expect(box).toBeTruthy()
24 if (box) {
25 // Minimap should fit alongside the playable board.
26 expect(box.width).toBeGreaterThan(80)
27 }
28 })
29​
30 test(`touch targets are at least 44px`, async ({ page }) => {
31 // Apple's Human Interface Guidelines recommend 44px minimum
32 const MIN_TOUCH_TARGET = 44
33​
34 // Check zoom buttons
35 const zoomIn = page.getByRole(`button`, { name: /zoom in/i })
36 const zoomOut = page.getByRole(`button`, { name: /zoom out/i })
37​
38 for (const button of [zoomIn, zoomOut]) {
39 const box = await button.boundingBox()
40 expect(box).toBeTruthy()
41 if (box) {
42 expect(box.width).toBeGreaterThanOrEqual(MIN_TOUCH_TARGET)
43 expect(box.height).toBeGreaterThanOrEqual(MIN_TOUCH_TARGET)
44 }
45 }
46 })
47​
48 test(`world view is visible on mobile`, async ({ page }) => {
49 const worldView = page.getByTestId(`world-view`)
50 await expect(worldView).toBeVisible()
51​
52 // Get viewport size
53 const viewportSize = page.viewportSize()
54 expect(viewportSize).toBeTruthy()
55​
56 const box = await worldView.boundingBox()
57 expect(box).toBeTruthy()
58​
59 if (box && viewportSize) {
60 // World view should be within viewport bounds
61 expect(box.x).toBeGreaterThanOrEqual(0)
62 expect(box.y).toBeGreaterThanOrEqual(0)
63 expect(box.x + box.width).toBeLessThanOrEqual(viewportSize.width + 1)
64 }
65 })
66​
67 test(`no horizontal overflow on mobile`, async ({ page }) => {
68 // Check that body doesn't have horizontal scroll
69 const hasHorizontalScroll = await page.evaluate(() => {
70 return document.body.scrollWidth > window.innerWidth
71 })
72​
73 expect(hasHorizontalScroll).toBe(false)
74 })
75​
76 test(`quota meter is accessible on mobile`, async ({ page }) => {
77 const quotaMeter = page.getByTestId(`quota-meter`)
78 await expect(quotaMeter).toBeVisible()
79​
80 const box = await quotaMeter.boundingBox()
81 expect(box).toBeTruthy()
82​
83 if (box) {
84 // Should have reasonable size for mobile viewing
85 expect(box.width).toBeGreaterThan(50)
86 expect(box.height).toBeGreaterThanOrEqual(12)
87 }
88 })
89​
90 test(`team badge is readable on mobile`, async ({ page }) => {
91 const teamBadge = page.getByTestId(`team-badge`).filter({ visible: true })
92 await expect(teamBadge).toBeVisible()
93​
94 const teamName = page.getByTestId(`team-name`).filter({ visible: true })
95 await expect(teamName).toBeVisible()
96​
97 // Check that text is actually visible (has some size)
98 const box = await teamName.boundingBox()
99 expect(box).toBeTruthy()
100 if (box) {
101 expect(box.width).toBeGreaterThan(0)
102 expect(box.height).toBeGreaterThan(0)
103 }
104 })
105})
106​
107// Test on small viewport (iPhone SE size)
108test.describe(`Small Mobile Layout`, () => {
109 test.use({ viewport: { width: 375, height: 667 } }) // iPhone SE size
110​
111 test(`layout adapts to small screen`, async ({ page }) => {
112 await openGame(page)
113​
114 // UI should still be visible
115 await expect(page.getByTestId(`team-badge`).filter({ visible: true })).toBeVisible()
116 await expect(page.getByTestId(`quota-meter`)).toBeVisible()
117​
118 const worldView = page.getByTestId(`world-view`)
119 await expect(worldView).toBeVisible()
120 })
121​
122 test(`controls remain accessible`, async ({ page }) => {
123 await openGame(page)
124​
125 const zoomIn = page.getByRole(`button`, { name: /zoom in/i })
126 const zoomOut = page.getByRole(`button`, { name: /zoom out/i })
127​
128 await expect(zoomIn).toBeVisible()
129 await expect(zoomOut).toBeVisible()
130​
131 // Should be clickable
132 await zoomIn.click()
133 await zoomOut.click()
134 })
135})
136​
137// Test landscape orientation
138test.describe(`Landscape Orientation`, () => {
139 test.use({ viewport: { width: 844, height: 390 } }) // iPhone 13 landscape
140​
141 test(`landscape layout works`, async ({ page }) => {
142 await openGame(page)
143​
144 // Core elements should be visible
145 await expect(page.getByTestId(`team-badge`).filter({ visible: true })).toBeVisible()
146 await expect(page.getByTestId(`quota-meter`)).toBeVisible()
147​
148 const worldView = page.getByTestId(`world-view`)
149 await expect(worldView).toBeVisible()
150 })
151​
152 test(`world view uses available width in landscape`, async ({ page }) => {
153 await openGame(page)
154​
155 const worldView = page.getByTestId(`game-canvas`)
156 const box = await worldView.boundingBox()
157​
158 expect(box).toBeTruthy()
159 if (box) {
160 // In landscape, world view should have substantial width
161 expect(box.width).toBeGreaterThan(400)
162 }
163 })
164})
165​
166// Test touch interactions
167test.describe(`Touch Interactions`, () => {
168 test.use({ viewport: { width: 390, height: 844 }, hasTouch: true })
169​
170 test(`tap on world view is handled`, async ({ page }) => {
171 await openGame(page)
172​
173 const worldView = page.getByTestId(`world-view`)
174 await expect(worldView).toBeVisible()
175​
176 const box = await worldView.boundingBox()
177 expect(box).toBeTruthy()
178​
179 if (box) {
180 // Tap in the center
181 await page.touchscreen.tap(box.x + box.width / 2, box.y + box.height / 2)
182​
183 // Verify no errors occurred (page should still be functional)
184 await expect(page.getByTestId(`quota-meter`)).toBeVisible()
185 }
186 })
187​
188 test(`zoom buttons respond to touch`, async ({ page }) => {
189 await openGame(page)
190​
191 const zoomIn = page.getByRole(`button`, { name: /zoom in/i })
192 await expect(zoomIn).toBeVisible()
193​
194 const box = await zoomIn.boundingBox()
195 expect(box).toBeTruthy()
196​
197 if (box) {
198 // Touch the zoom button
199 await page.touchscreen.tap(box.x + box.width / 2, box.y + box.height / 2)
200​
201 // Button should still be visible and functional
202 await expect(zoomIn).toBeVisible()
203 }
204 })
205})