-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathinjectWebviewOverlay.test.ts
More file actions
124 lines (103 loc) · 3.86 KB
/
injectWebviewOverlay.test.ts
File metadata and controls
124 lines (103 loc) · 3.86 KB
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
119
120
121
122
123
124
import { describe, it, expect, beforeEach } from 'vitest'
import { JSDOM } from 'jsdom'
import { injectWebviewOverlay } from './injectWebviewOverlay.js'
describe('injectWebviewOverlay', () => {
beforeEach(() => {
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>', {
pretendToBeVisual: true,
runScripts: 'dangerously',
})
global.window = dom.window as unknown as Window & typeof globalThis
global.document = dom.window.document
Object.defineProperty(document.documentElement, 'clientHeight', {
value: 800,
configurable: true,
})
Object.defineProperty(window, 'innerWidth', {
value: 400,
configurable: true,
})
Object.defineProperty(window, 'devicePixelRatio', {
value: 2,
configurable: true,
})
})
it('should inject an overlay if not already present', () => {
expect(document.querySelector('[data-test="ics-overlay"]')).toBeNull()
injectWebviewOverlay(true)
const overlay = document.querySelector('[data-test="ics-overlay"]') as HTMLElement
expect(overlay).toBeTruthy()
expect(overlay?.tagName).toBe('DIV')
expect(overlay?.style.position).toBe('fixed')
expect(overlay?.style.height).toContain('800px')
})
it('should not inject a second overlay if one already exists', () => {
injectWebviewOverlay(true)
injectWebviewOverlay(true)
const overlays = document.querySelectorAll('[data-test="ics-overlay"]')
expect(overlays.length).toBe(1)
})
it('should store click position and dimensions in dataset on click (Android DPR)', () => {
injectWebviewOverlay(true)
const overlay = document.querySelector('[data-test="ics-overlay"]') as HTMLDivElement
const event = new window.MouseEvent('click', {
clientX: 50,
clientY: 100,
bubbles: true,
})
overlay.dispatchEvent(event)
const parsedData = JSON.parse(overlay.dataset.icsWebviewData!)
expect(parsedData).toEqual({
x: 100,
y: 200,
width: 800,
height: 1600,
})
})
it('should round values to integers with non-integer DPR (Android)', () => {
Object.defineProperty(window, 'devicePixelRatio', {
value: 2.625,
configurable: true,
})
Object.defineProperty(window, 'innerWidth', {
value: 412,
configurable: true,
})
Object.defineProperty(document.documentElement, 'clientHeight', {
value: 363,
configurable: true,
})
injectWebviewOverlay(true)
const overlay = document.querySelector('[data-test="ics-overlay"]') as HTMLDivElement
const event = new window.MouseEvent('click', {
clientX: 206,
clientY: 181,
bubbles: true,
})
overlay.dispatchEvent(event)
const parsedData = JSON.parse(overlay.dataset.icsWebviewData!)
expect(parsedData).toEqual({
x: Math.round(206 * 2.625),
y: Math.round(181 * 2.625),
width: Math.round(412 * 2.625),
height: Math.round(363 * 2.625),
})
})
it('should use DPR = 1 for iOS (isAndroid = false)', () => {
injectWebviewOverlay(false)
const overlay = document.querySelector('[data-test="ics-overlay"]') as HTMLDivElement
const event = new window.MouseEvent('click', {
clientX: 50,
clientY: 100,
bubbles: true,
})
overlay.dispatchEvent(event)
const parsedData = JSON.parse(overlay.dataset.icsWebviewData!)
expect(parsedData).toEqual({
x: 50,
y: 100,
width: 400,
height: 800,
})
})
})