mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-17 18:11:20 -08:00
test: ENG-03 share behavior fixtures across source and releases
This commit is contained in:
1 parent
b53461da1c
commit
458bc3215d
15 files changed
+260
-74
No files matched your search
+12
-49
@@ -1,58 +1,21 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { Buffer } from 'node:buffer'
|
||||
// eslint-disable-next-line test/no-import-node-test -- Use the built-in runner without adding a test framework.
|
||||
import { mock, test } from 'node:test'
|
||||
import { setImmediate } from 'node:timers/promises'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { build } from 'esbuild'
|
||||
import { loadModules } from './helpers/load.js'
|
||||
import { playbackFactory } from './helpers/playback.js'
|
||||
|
||||
// Bundle the source modules so tests also work with Node 20's ESM resolution.
|
||||
const { outputFiles } = await build({
|
||||
stdin: {
|
||||
contents: `
|
||||
export { silencePromise } from './packages/artplayer/src/utils/error.js';
|
||||
export { default as Emitter } from './packages/artplayer/src/utils/emitter.js';
|
||||
export { default as Hotkey } from './packages/artplayer/src/hotkey.js';
|
||||
export { default as playMix } from './packages/artplayer/src/player/playMix.js';
|
||||
export { default as pauseMix } from './packages/artplayer/src/player/pauseMix.js';
|
||||
export { default as toggleMix } from './packages/artplayer/src/player/toggleMix.js';
|
||||
export { default as switchMix } from './packages/artplayer/src/player/switchMix.js';
|
||||
`,
|
||||
resolveDir: fileURLToPath(new URL('../', import.meta.url)),
|
||||
},
|
||||
bundle: true,
|
||||
write: false,
|
||||
platform: 'node',
|
||||
format: 'esm',
|
||||
const modules = await loadModules({
|
||||
silencePromise: { file: 'packages/artplayer/src/utils/error', name: 'silencePromise' },
|
||||
Emitter: 'packages/artplayer/src/utils/emitter',
|
||||
Hotkey: 'packages/artplayer/src/hotkey',
|
||||
playMix: 'packages/artplayer/src/player/playMix',
|
||||
pauseMix: 'packages/artplayer/src/player/pauseMix',
|
||||
toggleMix: 'packages/artplayer/src/player/toggleMix',
|
||||
switchMix: 'packages/artplayer/src/player/switchMix',
|
||||
})
|
||||
const { silencePromise, Emitter, Hotkey, playMix, pauseMix, toggleMix, switchMix } = await import(
|
||||
`data:text/javascript;base64,${Buffer.from(outputFiles[0].contents).toString('base64')}`,
|
||||
)
|
||||
|
||||
function createArt(play = () => Promise.resolve(), playing = false) {
|
||||
const art = Object.assign(new Emitter(), {
|
||||
url: 'old.mp4',
|
||||
currentTime: 37,
|
||||
aspectRatio: '16:9',
|
||||
playbackRate: 1.5,
|
||||
notice: { show: '' },
|
||||
i18n: { get: key => key },
|
||||
option: { hotkey: true, mutex: false },
|
||||
constructor: { instances: [] },
|
||||
template: {
|
||||
$video: {
|
||||
paused: !playing,
|
||||
play: mock.fn(play),
|
||||
pause() { this.paused = true },
|
||||
},
|
||||
},
|
||||
})
|
||||
Object.defineProperty(art, 'playing', { get: () => !art.template.$video.paused })
|
||||
for (const mix of [playMix, pauseMix, toggleMix, switchMix]) {
|
||||
mix(art)
|
||||
}
|
||||
return art
|
||||
}
|
||||
const { silencePromise, Hotkey } = modules
|
||||
const createArt = playbackFactory(modules)
|
||||
|
||||
test('silencePromise preserves fulfilled values and handles rejected promises', async () => {
|
||||
assert.equal(await silencePromise(Promise.resolve(42)), 42)
|
||||
|
||||
Reference in new issue
Block a user