|
1 |
| -import { test, expect } from 'jest' |
| 1 | +import { describe, test, expect } from 'jest' |
2 | 2 | import { mount } from 'enzyme'
|
3 | 3 | import React from 'react'
|
4 | 4 |
|
5 | 5 | import SignatureCanvas from './index.js'
|
| 6 | +import { dotData } from '../test-utils/fixtures.js' |
6 | 7 |
|
7 | 8 | test('mounts canvas and instance properly', () => {
|
8 | 9 | const wrapper = mount(<SignatureCanvas />)
|
9 | 10 | expect(wrapper.exists('canvas')).toBe(true)
|
10 | 11 | const instance = wrapper.instance()
|
11 | 12 | expect(instance.isEmpty()).toBe(true)
|
12 | 13 | })
|
| 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 | +}) |
0 commit comments