Skip to content

Commit 29dd1bb

Browse files
committed
(test): ensure wrapper methods have equivalent output
- add describe as this is several tests in one - add a fixture of just a dot - add tests for toData, fromData, toDataURL, fromDataURL, isEmpty, and clear
1 parent a0dae66 commit 29dd1bb

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

src/index.spec.js

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,87 @@
1-
import { test, expect } from 'jest'
1+
import { describe, test, expect } from 'jest'
22
import { mount } from 'enzyme'
33
import React from 'react'
44

55
import SignatureCanvas from './index.js'
6+
import { dotData } from '../test-utils/fixtures.js'
67

78
test('mounts canvas and instance properly', () => {
89
const wrapper = mount(<SignatureCanvas />)
910
expect(wrapper.exists('canvas')).toBe(true)
1011
const instance = wrapper.instance()
1112
expect(instance.isEmpty()).toBe(true)
1213
})
14+
15+
describe('SigCanvas wrapper methods return equivalent to SigPad', () => {
16+
const rSigPad = mount(<SignatureCanvas />).instance()
17+
const sigPad = rSigPad.getSignaturePad()
18+
19+
test('toData should be equivalent', () => {
20+
const rData = rSigPad.toData()
21+
expect(rData).toStrictEqual([])
22+
expect(rData).toBe(sigPad.toData())
23+
})
24+
25+
test('fromData should be equivalent', () => {
26+
rSigPad.fromData(dotData)
27+
const rData = rSigPad.toData()
28+
expect(rData).toBe(dotData)
29+
expect(rData).toBe(sigPad.toData())
30+
31+
// test reverse as both froms should be equivalent
32+
sigPad.fromData(dotData)
33+
const data = sigPad.toData()
34+
expect(rData).toBe(data)
35+
expect(rSigPad.toData()).toBe(data)
36+
})
37+
38+
test('toDataURL should be equivalent', () => {
39+
rSigPad.fromData(dotData)
40+
expect(rSigPad.toDataURL()).toBe(sigPad.toDataURL())
41+
expect(rSigPad.toDataURL('image/jpg')).toBe(sigPad.toDataURL('image/jpg'))
42+
expect(rSigPad.toDataURL('image/jpg', 0.7)).toBe(sigPad.toDataURL('image/jpg', 0.7))
43+
expect(rSigPad.toDataURL('image/svg+xml')).toBe(sigPad.toDataURL('image/svg+xml'))
44+
})
45+
46+
test('fromDataURL should be equivalent', () => {
47+
// convert data fixture to dataURL
48+
rSigPad.fromData(dotData)
49+
const dotDataURL = rSigPad.toDataURL()
50+
51+
rSigPad.fromDataURL(dotDataURL)
52+
const rDataURL = rSigPad.toDataURL()
53+
expect(rDataURL).toBe(dotDataURL)
54+
expect(rDataURL).toBe(sigPad.toDataURL())
55+
56+
// test reverse as both froms should be equivalent
57+
sigPad.fromDataURL(dotDataURL)
58+
const dataURL = sigPad.toDataURL()
59+
expect(rDataURL).toBe(dataURL)
60+
expect(rSigPad.toDataURL()).toBe(dataURL)
61+
})
62+
63+
test('isEmpty & clear should be equivalent', () => {
64+
rSigPad.fromData(dotData)
65+
let isEmpty = rSigPad.isEmpty()
66+
expect(isEmpty).toBe(false)
67+
expect(isEmpty).toBe(sigPad.isEmpty())
68+
69+
// both empty after clear
70+
rSigPad.clear()
71+
isEmpty = rSigPad.isEmpty()
72+
expect(isEmpty).toBe(true)
73+
expect(isEmpty).toBe(sigPad.isEmpty())
74+
75+
// test reverse
76+
sigPad.fromData(dotData)
77+
isEmpty = rSigPad.isEmpty()
78+
expect(isEmpty).toBe(false)
79+
expect(isEmpty).toBe(sigPad.isEmpty())
80+
81+
// both empty after internal sigPad clear
82+
sigPad.clear()
83+
isEmpty = rSigPad.isEmpty()
84+
expect(isEmpty).toBe(true)
85+
expect(isEmpty).toBe(sigPad.isEmpty())
86+
})
87+
})

test-utils/fixtures.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const dotData = [
2+
[{ x: 466.59375, y: 189, time: 1564339579755, color: 'black' }]
3+
]

test-utils/jest-export.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// export Jest as an ES Module (https://github.com/facebook/jest/pull/7571#issuecomment-498634094)
22
/* global jest */
33
export default jest
4-
export const { expect, test } = global
4+
export const { expect, test, describe } = global

0 commit comments

Comments
 (0)