@@ -7,9 +7,16 @@ import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest'
77import Zmage from '../Zmage'
88import { resolvePreset } from '../types/default'
99import { pageIsCover } from '../components/Browser/Browser.utils'
10+ import type { HotKey } from '../types/global'
1011
1112const SRC = 'https://example.com/test.jpg'
1213
14+ function clickById ( id : string ) {
15+ const el = document . getElementById ( id )
16+ if ( ! el ) throw new Error ( `expected #${ id } to be in the DOM at this point` )
17+ fireEvent . click ( el )
18+ }
19+
1320describe ( 'Zmage 基础组件' , ( ) => {
1421 it ( '封面 img 可被渲染并附加 zoom-in cursor' , ( ) => {
1522 render ( < Zmage src = { SRC } alt = "t" /> )
@@ -235,9 +242,9 @@ describe('Zmage caption 渲染', () => {
235242 expect ( document . getElementById ( 'zmageCaption' ) ?. textContent ) . toBe ( 'first' )
236243 // 触发右翻按钮 (DOM 事件比 window keydown 在 JSDOM 下更可靠)
237244 const flipRight = document . getElementById ( 'zmageControlFlipRight' )
238- expect ( flipRight ) . toBeTruthy ( )
245+ if ( ! flipRight ) throw new Error ( 'expected #zmageControlFlipRight in DOM' )
239246 await act ( async ( ) => {
240- fireEvent . click ( flipRight ! )
247+ fireEvent . click ( flipRight )
241248 await new Promise ( r => setTimeout ( r , 50 ) )
242249 } )
243250 expect ( document . getElementById ( 'zmageCaption' ) ?. textContent ) . toBe ( 'second' )
@@ -255,17 +262,17 @@ describe('Zmage 动画行为', () => {
255262 await wait ( 50 )
256263
257264 const rotateRight = document . getElementById ( 'zmageControlRotateRight' )
258- expect ( rotateRight ) . toBeTruthy ( )
265+ if ( ! rotateRight ) throw new Error ( 'expected #zmageControlRotateRight in DOM' )
259266 await act ( async ( ) => {
260267 for ( let i = 0 ; i < 5 ; i ++ ) {
261- fireEvent . click ( rotateRight ! )
268+ fireEvent . click ( rotateRight )
262269 }
263270 await new Promise ( r => setTimeout ( r , 80 ) )
264271 } )
265272
266273 expect ( ( document . getElementById ( 'zmageImage' ) as HTMLImageElement ) . style . transform ) . toContain ( '450deg' )
267274
268- fireEvent . click ( document . getElementById ( 'zmageControlClose' ) ! )
275+ clickById ( 'zmageControlClose' )
269276 await wait ( 80 )
270277
271278 const closingImage = document . getElementById ( 'zmageImage' ) as HTMLImageElement
@@ -282,7 +289,7 @@ describe('Zmage 动画行为', () => {
282289 expect ( document . getElementById ( 'zmageControl' ) ?. style . transition ) . toBe ( 'none' )
283290 expect ( document . getElementById ( 'zmageCaption' ) ?. style . transition ) . toBe ( 'none' )
284291
285- fireEvent . click ( document . getElementById ( 'zmageControlClose' ) ! )
292+ clickById ( 'zmageControlClose' )
286293 await wait ( 0 )
287294 expect ( document . getElementById ( 'zmage' ) ) . toBeNull ( )
288295 } )
@@ -302,7 +309,7 @@ describe('Zmage 动画行为', () => {
302309 fireEvent . click ( screen . getByAltText ( 'cover' ) )
303310 await wait ( 50 )
304311
305- fireEvent . click ( document . getElementById ( 'zmageControlFlipRight' ) ! )
312+ clickById ( 'zmageControlFlipRight' )
306313 await wait ( 50 )
307314
308315 const centerImage = document . getElementById ( 'zmageImage' ) as HTMLImageElement
@@ -333,7 +340,7 @@ describe('Zmage 动画行为', () => {
333340
334341 expect ( nextSideImage ) . toBeTruthy ( )
335342
336- fireEvent . click ( document . getElementById ( 'zmageControlFlipRight' ) ! )
343+ clickById ( 'zmageControlFlipRight' )
337344 await wait ( 50 )
338345
339346 const centerImage = document . getElementById ( 'zmageImage' ) as HTMLImageElement
@@ -391,7 +398,7 @@ describe('Zmage 动画行为', () => {
391398 fireEvent . click ( screen . getByAltText ( 'control-zoom' ) )
392399 await wait ( 50 )
393400
394- fireEvent . click ( document . getElementById ( 'zmageControlZoom' ) ! )
401+ clickById ( 'zmageControlZoom' )
395402 await wait ( 80 )
396403
397404 const centerImage = document . getElementById ( 'zmageImage' ) as HTMLImageElement
@@ -471,7 +478,7 @@ describe('Zmage hotKey 翻页 (umbrella + 单边)', () => {
471478 } )
472479 }
473480
474- const renderTwoPageSet = ( hotKey : any ) =>
481+ const renderTwoPageSet = ( hotKey : boolean | HotKey ) =>
475482 render (
476483 < Zmage
477484 src = { SRC }
0 commit comments