|
| 1 | +import { test } from 'node:test' |
| 2 | +import { mockDOM, mockListen } from '#test/utils' |
| 3 | +import { StrokeMotion } from '../index.js' |
| 4 | + |
| 5 | +test('StrokeMotion', async t => { |
| 6 | + t.beforeEach(t => Object.assign(t, { mockDOM, mockListen }).mockDOM()) |
| 7 | + |
| 8 | + await t.test('points with non-array input', async t => { |
| 9 | + await t.test('throws TypeError', t => { |
| 10 | + const el = document.createElement('div') |
| 11 | + |
| 12 | + t.assert.throws( |
| 13 | + () => new StrokeMotion(el, null), |
| 14 | + { name: 'TypeError', message: /StrokeMotion\.points/i } |
| 15 | + ) |
| 16 | + }) |
| 17 | + }) |
| 18 | + |
| 19 | + await t.test('points with negative timestamps', async t => { |
| 20 | + await t.test('throws RangeError', t => { |
| 21 | + const el = document.createElement('div') |
| 22 | + |
| 23 | + t.assert.throws( |
| 24 | + () => new StrokeMotion(el, [[10, 10, -1]]), |
| 25 | + { name: 'RangeError', message: />= 0/i } |
| 26 | + ) |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + await t.test('points with decreasing timestamps', async t => { |
| 31 | + await t.test('throws RangeError', t => { |
| 32 | + const el = document.createElement('div') |
| 33 | + |
| 34 | + t.assert.throws( |
| 35 | + () => new StrokeMotion(el, [ |
| 36 | + [10, 10, 1], |
| 37 | + [11, 11, 0], |
| 38 | + ]), |
| 39 | + { name: 'RangeError', message: /points\[1\]\[2\]/i } |
| 40 | + ) |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + await t.test('pen stroke within element', async t => { |
| 45 | + t.beforeEach(t => Object.assign(t, { |
| 46 | + stage: document.body.appendChild( |
| 47 | + Object.assign(document.createElement('div'), { id: 'stage' }) |
| 48 | + ), |
| 49 | + a: document.createElement('div'), |
| 50 | + })) |
| 51 | + |
| 52 | + t.beforeEach(t => { |
| 53 | + t.a.id = 'a' |
| 54 | + t.stage.append(t.a) |
| 55 | + document.elementFromPoint = () => t.a |
| 56 | + }) |
| 57 | + |
| 58 | + await t.test('dispatches expected event sequence', async t => { |
| 59 | + const dispatched = t.mockListen([ |
| 60 | + 'pointerover', |
| 61 | + 'pointerenter', |
| 62 | + 'pointerdown', |
| 63 | + 'touchstart', |
| 64 | + 'gotpointercapture', |
| 65 | + 'pointermove', |
| 66 | + 'touchmove', |
| 67 | + 'pointerup', |
| 68 | + 'lostpointercapture', |
| 69 | + 'pointerout', |
| 70 | + 'pointerleave', |
| 71 | + 'touchend', |
| 72 | + ]) |
| 73 | + |
| 74 | + await new StrokeMotion(t.stage, [ |
| 75 | + [10, 10, 0], |
| 76 | + [11, 11, 0], |
| 77 | + ]).perform() |
| 78 | + |
| 79 | + t.assert.eventSequence(dispatched, [ |
| 80 | + 'pointerover@a', |
| 81 | + 'pointerenter@HTML', |
| 82 | + 'pointerenter@BODY', |
| 83 | + 'pointerenter@stage', |
| 84 | + 'pointerenter@a', |
| 85 | + 'pointerdown@a', |
| 86 | + 'touchstart@a', |
| 87 | + 'pointermove@a', |
| 88 | + 'touchmove@a', |
| 89 | + 'pointerup@a', |
| 90 | + 'pointerout@a', |
| 91 | + 'pointerleave@a', |
| 92 | + 'pointerleave@stage', |
| 93 | + 'pointerleave@BODY', |
| 94 | + 'pointerleave@HTML', |
| 95 | + 'touchend@a', |
| 96 | + ]) |
| 97 | + }) |
| 98 | + |
| 99 | + await t.test('dispatches pointermove for each point after first', async t => { |
| 100 | + const dispatched = t.mockListen(['pointermove']) |
| 101 | + |
| 102 | + await new StrokeMotion(t.stage, [ |
| 103 | + [10, 10, 0], |
| 104 | + [11, 11, 0], |
| 105 | + [12, 12, 0], |
| 106 | + ]).perform() |
| 107 | + |
| 108 | + t.assert.strictEqual(dispatched.length, 2) |
| 109 | + }) |
| 110 | + |
| 111 | + await t.test('uses pointerType pen', async t => { |
| 112 | + const dispatched = t.mockListen([ |
| 113 | + 'pointerover', |
| 114 | + 'pointerenter', |
| 115 | + 'pointerdown', |
| 116 | + 'pointermove', |
| 117 | + 'pointerup', |
| 118 | + 'pointerout', |
| 119 | + 'pointerleave', |
| 120 | + ]) |
| 121 | + |
| 122 | + await new StrokeMotion(t.stage, [ |
| 123 | + [10, 10, 0], |
| 124 | + [11, 11, 0], |
| 125 | + ]).perform() |
| 126 | + |
| 127 | + t.assert.everyPartialEqual(dispatched, { pointerType: 'pen' }) |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + await t.test('hit-test miss mid-motion', async t => { |
| 132 | + t.beforeEach(t => Object.assign(t, { |
| 133 | + stage: document.body.appendChild( |
| 134 | + Object.assign(document.createElement('div'), { id: 'stage' }) |
| 135 | + ), |
| 136 | + a: document.createElement('div'), |
| 137 | + })) |
| 138 | + |
| 139 | + t.beforeEach(t => { |
| 140 | + t.a.id = 'a' |
| 141 | + t.stage.append(t.a) |
| 142 | + document.elementFromPoint = x => (x < 20 ? t.a : null) |
| 143 | + }) |
| 144 | + |
| 145 | + await t.test('rejects with hit-test missed error', async t => { |
| 146 | + await t.assert.rejects( |
| 147 | + () => new StrokeMotion(t.stage, [ |
| 148 | + [10, 10, 0], |
| 149 | + [30, 10, 0], |
| 150 | + ]).perform(), |
| 151 | + { name: 'Error', message: /hit-test missed/i } |
| 152 | + ) |
| 153 | + }) |
| 154 | + |
| 155 | + await t.test('dispatches pointercancel then leave then touchcancel', async t => { |
| 156 | + const dispatched = t.mockListen([ |
| 157 | + 'pointerover', |
| 158 | + 'pointerenter', |
| 159 | + 'pointerdown', |
| 160 | + 'touchstart', |
| 161 | + 'pointercancel', |
| 162 | + 'pointerout', |
| 163 | + 'pointerleave', |
| 164 | + 'touchcancel', |
| 165 | + ]) |
| 166 | + |
| 167 | + await new StrokeMotion(t.stage, [ |
| 168 | + [10, 10, 0], |
| 169 | + [30, 10, 0], |
| 170 | + ]).perform().catch(() => null) |
| 171 | + |
| 172 | + t.assert.eventSequence(dispatched, [ |
| 173 | + 'pointerover@a', |
| 174 | + 'pointerenter@HTML', |
| 175 | + 'pointerenter@BODY', |
| 176 | + 'pointerenter@stage', |
| 177 | + 'pointerenter@a', |
| 178 | + 'pointerdown@a', |
| 179 | + 'touchstart@a', |
| 180 | + 'pointercancel@a', |
| 181 | + 'pointerout@a', |
| 182 | + 'pointerleave@a', |
| 183 | + 'pointerleave@stage', |
| 184 | + 'pointerleave@BODY', |
| 185 | + 'pointerleave@HTML', |
| 186 | + 'touchcancel@a', |
| 187 | + ]) |
| 188 | + }) |
| 189 | + }) |
| 190 | +}) |
| 191 | + |
0 commit comments