generated from napi-rs/package-template
/
index.spec.ts
177 lines (150 loc) · 4.27 KB
/
index.spec.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import ava, { TestFn } from 'ava'
import { createCanvas, Path2D, Canvas, SKRSContext2D, DOMMatrix } from '../index'
import { snapshotImage } from './image-snapshot'
const test = ava as TestFn<{
canvas: Canvas
ctx: SKRSContext2D
}>
test.beforeEach((t) => {
const canvas = createCanvas(1024, 768)
t.context.canvas = canvas
t.context.ctx = canvas.getContext('2d')!
})
test('should be able to create Path2D', (t) => {
t.notThrows(() => new Path2D())
t.notThrows(
() =>
new Path2D(
'M108.956,403.826c0,0,0.178,3.344-1.276,3.311 c-1.455-0.033-30.507-84.917-66.752-80.957C40.928,326.18,72.326,313.197,108.956,403.826z',
),
)
t.notThrows(() => new Path2D(new Path2D()))
})
test('miterLimit state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.miterLimit, 10)
ctx.miterLimit = 20
t.is(ctx.miterLimit, 20)
})
test('globalAlpha state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.globalAlpha, 1)
ctx.globalAlpha = 0.2
t.is(ctx.globalAlpha, 0.2)
})
test('globalCompositeOperation state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.globalCompositeOperation, 'source-over')
ctx.globalCompositeOperation = 'xor'
t.is(ctx.globalCompositeOperation, 'xor')
})
test('imageSmoothingEnabled state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.imageSmoothingEnabled, true)
ctx.imageSmoothingEnabled = false
t.is(ctx.imageSmoothingEnabled, false)
})
test('imageSmoothingQuality state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.imageSmoothingQuality, 'low')
ctx.imageSmoothingQuality = 'high'
t.is(ctx.imageSmoothingQuality, 'high')
})
test('lineCap state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.lineCap, 'butt')
ctx.lineCap = 'round'
t.is(ctx.lineCap, 'round')
})
test('lineDashOffset state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.lineDashOffset, 0)
ctx.lineDashOffset = 10
t.is(ctx.lineDashOffset, 10)
})
test('lineJoin state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.lineJoin, 'miter')
ctx.lineJoin = 'round'
t.is(ctx.lineJoin, 'round')
})
test('lineWidth state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.lineWidth, 1)
ctx.lineWidth = 10
t.is(ctx.lineWidth, 10)
})
test('fillStyle state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.fillStyle, '#000')
ctx.fillStyle = 'hotpink'
t.is(ctx.fillStyle, 'hotpink')
})
test('strokeStyle state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.strokeStyle, '#000')
ctx.strokeStyle = 'hotpink'
t.is(ctx.strokeStyle, 'hotpink')
})
test('shadowBlur state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.shadowBlur, 0)
ctx.shadowBlur = 10
t.is(ctx.shadowBlur, 10)
})
test('shadowColor state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.shadowColor, '#000000')
ctx.shadowColor = 'hotpink'
t.is(ctx.shadowColor, 'hotpink')
})
test('shadowOffsetX state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.shadowOffsetX, 0)
ctx.shadowOffsetX = 10
t.is(ctx.shadowOffsetX, 10)
})
test('shadowOffsetY state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.shadowOffsetY, 0)
ctx.shadowOffsetY = 10
t.is(ctx.shadowOffsetY, 10)
})
test('lineDash state should be ok', (t) => {
const { ctx } = t.context
const lineDash = [1, 2, 4.5, 7]
ctx.setLineDash(lineDash)
t.deepEqual(ctx.getLineDash(), lineDash)
})
test('textAlign state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.textAlign, 'start')
ctx.textAlign = 'center'
t.is(ctx.textAlign, 'center')
})
test('textBaseline state should be ok', (t) => {
const { ctx } = t.context
t.is(ctx.textBaseline, 'alphabetic')
ctx.textBaseline = 'hanging'
t.is(ctx.textBaseline, 'hanging')
})
test('getTransform', (t) => {
const { ctx } = t.context
t.deepEqual(ctx.getTransform(), new DOMMatrix([1, 0, 0, 1, 0, 0]))
})
test('stroke-and-filling-jpeg', async (t) => {
const { ctx } = t.context
ctx.lineWidth = 16
ctx.strokeStyle = 'red'
// Stroke on top of fill
ctx.beginPath()
ctx.rect(25, 25, 100, 100)
ctx.fill()
ctx.stroke()
// Fill on top of stroke
ctx.beginPath()
ctx.rect(175, 25, 100, 100)
ctx.stroke()
ctx.fill()
await snapshotImage(t, t.context, 'jpeg')
})