Skip to content

Commit c361f46

Browse files
committed
add pal() tests
1 parent 111a732 commit c361f46

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

tests/_preload/happy-dom.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import { GlobalRegistrator } from '@happy-dom/global-registrator'
77
* @returns {Promise<Function>}
88
*/
99
global.onLitecanvas = function (instance, event, callback) {
10-
return new Promise((resolve) => {
10+
return new Promise((resolve, reject) => {
1111
const removeListener = instance.listen(event, (...args) => {
12-
const res = callback(...args)
13-
if (false !== res) {
14-
removeListener()
15-
resolve()
12+
try {
13+
const res = callback(...args)
14+
if (false !== res) {
15+
removeListener()
16+
resolve()
17+
}
18+
} catch (err) {
19+
reject(err)
1620
}
1721
})
1822
})

tests/pal.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import test from 'ava'
2+
import litecanvas from '../src/index.js'
3+
import { defaultPalette } from '../src/palette.js'
4+
import * as sinon from 'sinon'
5+
6+
let /** @type {LitecanvasInstance} */
7+
local,
8+
/** @type {sinon.SinonSpiedInstance<CanvasRenderingContext2D>} */
9+
contextSpy
10+
11+
test.before(() => {
12+
sinon.stub(console) // silent console
13+
14+
local = litecanvas({
15+
global: false,
16+
})
17+
18+
contextSpy = sinon.spy(local.ctx())
19+
})
20+
21+
test.after(() => {
22+
local.quit()
23+
})
24+
25+
test('change the color palette', async (t) => {
26+
await onLitecanvas(local, 'draw', () => {
27+
const customPalette = ['#123', '#456']
28+
local.pal(customPalette)
29+
30+
const colors = local.stat(5)
31+
32+
t.is(colors[0], customPalette[0])
33+
})
34+
})
35+
36+
test('reset the color palette', async (t) => {
37+
await onLitecanvas(local, 'draw', () => {
38+
local.pal()
39+
40+
const colors = local.stat(5)
41+
42+
t.is(colors[0], defaultPalette[0])
43+
})
44+
})

0 commit comments

Comments
 (0)