|
1 | 1 | import * as React from 'react'; |
2 | 2 | import { mount } from 'enzyme'; |
| 3 | +import SigPad from 'signature_pad'; |
3 | 4 |
|
4 | | -import './helpers/resizeWindow'; |
5 | 5 | import signature from './helpers/signature'; |
6 | 6 | import SignaturePad from '../src/SignaturePad'; |
7 | 7 |
|
| 8 | +/** |
| 9 | + * Set the dimension of the HTML canvas element to the given width and height. |
| 10 | + * |
| 11 | + * @param {number} width |
| 12 | + * @param {number} height |
| 13 | + * @return {void} |
| 14 | + */ |
| 15 | +const scaleCanvas = (width: number, height: number): void => { |
| 16 | + Object.defineProperty(HTMLCanvasElement.prototype, 'offsetWidth', { |
| 17 | + configurable: true, |
| 18 | + value: width, |
| 19 | + }); |
| 20 | + Object.defineProperty(HTMLCanvasElement.prototype, 'offsetHeight', { |
| 21 | + configurable: true, |
| 22 | + value: height, |
| 23 | + }); |
| 24 | +}; |
| 25 | + |
8 | 26 | describe('Component', () => { |
9 | 27 | describe('SignaturePad', () => { |
10 | | - const signaturePad = mount<SignaturePad>(<SignaturePad />); |
11 | | - const instance = signaturePad.instance(); |
| 28 | + beforeEach(() => { |
| 29 | + jest.restoreAllMocks(); |
| 30 | + scaleCanvas(1024, 768); |
| 31 | + }); |
12 | 32 |
|
13 | 33 | it('renders the component', () => { |
| 34 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 35 | + |
14 | 36 | expect(signaturePad.exists()).toBeTruthy(); |
15 | 37 | }); |
16 | 38 |
|
17 | 39 | it('loads a signature', () => { |
18 | | - // Signature pad should be empty initially |
| 40 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 41 | + const instance = signaturePad.instance(); |
| 42 | + |
| 43 | + instance.clear(); |
| 44 | + |
19 | 45 | expect(instance.isEmpty()).toBeTruthy(); |
20 | 46 |
|
21 | | - // Load a signature |
22 | 47 | instance.fromDataURL(signature); |
| 48 | + |
23 | 49 | expect(instance.isEmpty()).toBeFalsy(); |
24 | 50 | }); |
25 | 51 |
|
26 | 52 | it('clears the signature pad', () => { |
27 | | - // Load a signature |
| 53 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 54 | + const instance = signaturePad.instance(); |
| 55 | + |
28 | 56 | instance.fromDataURL(signature); |
| 57 | + |
29 | 58 | expect(instance.isEmpty()).toBeFalsy(); |
30 | 59 |
|
31 | 60 | instance.clear(); |
| 61 | + |
32 | 62 | expect(instance.isEmpty()).toBeTruthy(); |
33 | 63 | }); |
| 64 | + |
| 65 | + it('returns the underlying signature pad instance', () => { |
| 66 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 67 | + const instance = signaturePad.instance(); |
| 68 | + |
| 69 | + expect(instance.instance).toBeInstanceOf(SigPad); |
| 70 | + }); |
| 71 | + |
| 72 | + it('returns a ref to the underlying HTML canvas element', () => { |
| 73 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 74 | + const instance = signaturePad.instance(); |
| 75 | + |
| 76 | + expect(instance.canvas).toBeInstanceOf(Object); |
| 77 | + }); |
| 78 | + |
| 79 | + [ |
| 80 | + { name: 'dot size', option: 'dotSize', expected: 3 }, |
| 81 | + { name: 'min width', option: 'minWidth', expected: 1 }, |
| 82 | + { name: 'max width', option: 'maxWidth', expected: 3 }, |
| 83 | + { name: 'throttle', option: 'throttle', expected: 20 }, |
| 84 | + { name: 'background color', option: 'backgroundColor', expected: 'rgba(255,255,255)' }, |
| 85 | + { name: 'pen color', option: 'penColor', expected: 'red' }, |
| 86 | + { name: 'velocity filter weight', option: 'velocityFilterWeight', expected: 1 }, |
| 87 | + ].forEach(({ name, option, expected }) => { |
| 88 | + it(`sets and gets the ${name}`, () => { |
| 89 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 90 | + const instance = signaturePad.instance(); |
| 91 | + |
| 92 | + Reflect.set(instance, option, expected); |
| 93 | + |
| 94 | + expect(Reflect.get(instance, option)).toBe(expected); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns a signature as a data URL', () => { |
| 99 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 100 | + const instance = signaturePad.instance(); |
| 101 | + |
| 102 | + expect(instance.toDataURL()).toContain('data:image/png;base64'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('returns a signature as an array of data points', () => { |
| 106 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 107 | + const instance = signaturePad.instance(); |
| 108 | + |
| 109 | + expect(instance.toData()).toHaveLength(0); |
| 110 | + }); |
| 111 | + |
| 112 | + it('draws a signature from an array of data points', () => { |
| 113 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 114 | + const instance = signaturePad.instance(); |
| 115 | + |
| 116 | + const data = [ |
| 117 | + { |
| 118 | + dotSize: 0, |
| 119 | + maxWidth: 2.5, |
| 120 | + minWidth: 0.5, |
| 121 | + penColor: 'black', |
| 122 | + points: [{ pressure: 0.5, time: 1641476147709, x: 100, y: 100 }], |
| 123 | + }, |
| 124 | + ]; |
| 125 | + |
| 126 | + instance.fromData(data); |
| 127 | + |
| 128 | + expect(instance.toData()).toBe(data); |
| 129 | + }); |
| 130 | + |
| 131 | + it('unbinds all event handlers', () => { |
| 132 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 133 | + const instance = signaturePad.instance(); |
| 134 | + const spy = jest.spyOn(instance.instance, 'off'); |
| 135 | + |
| 136 | + instance.off(); |
| 137 | + |
| 138 | + expect(spy).toHaveBeenCalled(); |
| 139 | + |
| 140 | + spy.mockRestore(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('rebinds all event handlers', () => { |
| 144 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 145 | + const instance = signaturePad.instance(); |
| 146 | + const spy = jest.spyOn(instance.instance, 'on'); |
| 147 | + |
| 148 | + instance.on(); |
| 149 | + |
| 150 | + expect(spy).toHaveBeenCalled(); |
| 151 | + |
| 152 | + spy.mockRestore(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('does not redraw a signature by default when the viewport dimensions change', () => { |
| 156 | + const signaturePad = mount<SignaturePad>(<SignaturePad />); |
| 157 | + const instance = signaturePad.instance(); |
| 158 | + const spy = jest.spyOn(instance.instance, 'clear'); |
| 159 | + |
| 160 | + scaleCanvas(768, 768); |
| 161 | + instance.handleResize(); |
| 162 | + |
| 163 | + expect(spy).toHaveBeenCalled(); |
| 164 | + }); |
| 165 | + |
| 166 | + it('redraws a signature when the viewport dimensions change', () => { |
| 167 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 168 | + const instance = signaturePad.instance(); |
| 169 | + const spy = jest.spyOn(instance.instance, 'toDataURL'); |
| 170 | + |
| 171 | + scaleCanvas(768, 768); |
| 172 | + instance.handleResize(); |
| 173 | + |
| 174 | + expect(spy).toHaveBeenCalled(); |
| 175 | + }); |
| 176 | + |
| 177 | + it('does not redraw a signature when the viewport dimensions have not changed', () => { |
| 178 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 179 | + const instance = signaturePad.instance(); |
| 180 | + const spy = jest.spyOn(instance.instance, 'toDataURL'); |
| 181 | + |
| 182 | + instance.handleResize(); |
| 183 | + |
| 184 | + expect(spy).not.toHaveBeenCalled(); |
| 185 | + }); |
| 186 | + |
| 187 | + it('does not add the resize event listener on mount', () => { |
| 188 | + const spy = jest.spyOn(window, 'addEventListener'); |
| 189 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize width={512} height={512} />); |
| 190 | + |
| 191 | + expect(spy).not.toHaveBeenCalledWith('resize', Reflect.get(signaturePad.instance(), 'callResizeHandler')); |
| 192 | + }); |
| 193 | + |
| 194 | + it('adds the resize event listener on mount', () => { |
| 195 | + const spy = jest.spyOn(window, 'addEventListener'); |
| 196 | + |
| 197 | + const signaturePad = mount<SignaturePad>(<SignaturePad />); |
| 198 | + |
| 199 | + expect(spy).toHaveBeenCalledWith('resize', Reflect.get(signaturePad.instance(), 'callResizeHandler')); |
| 200 | + }); |
| 201 | + |
| 202 | + it('removes the resize event listener on unmount', () => { |
| 203 | + const spy = jest.spyOn(window, 'removeEventListener'); |
| 204 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />); |
| 205 | + const handler = Reflect.get(signaturePad.instance(), 'callResizeHandler'); |
| 206 | + |
| 207 | + signaturePad.unmount(); |
| 208 | + |
| 209 | + expect(spy).toHaveBeenCalledWith('resize', handler); |
| 210 | + }); |
| 211 | + |
| 212 | + it('does not remove the resize event listener on unmount', () => { |
| 213 | + const spy = jest.spyOn(window, 'removeEventListener'); |
| 214 | + const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize width={512} height={512} />); |
| 215 | + const handler = Reflect.get(signaturePad.instance(), 'callResizeHandler'); |
| 216 | + |
| 217 | + signaturePad.unmount(); |
| 218 | + |
| 219 | + expect(spy).not.toHaveBeenCalledWith('resize', handler); |
| 220 | + }); |
34 | 221 | }); |
35 | 222 | }); |
0 commit comments