Skip to content

Commit 16cb738

Browse files
authored
Merge pull request #38 from michaeldzjap/fix/#37-source-map
Only include source maps in dev build
2 parents 0c18dbe + 774cf67 commit 16cb738

File tree

10 files changed

+2235
-1853
lines changed

10 files changed

+2235
-1853
lines changed

__tests__/SignaturePad.test.tsx

Lines changed: 193 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,222 @@
11
import * as React from 'react';
22
import { mount } from 'enzyme';
3+
import SigPad from 'signature_pad';
34

4-
import './helpers/resizeWindow';
55
import signature from './helpers/signature';
66
import SignaturePad from '../src/SignaturePad';
77

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+
826
describe('Component', () => {
927
describe('SignaturePad', () => {
10-
const signaturePad = mount<SignaturePad>(<SignaturePad />);
11-
const instance = signaturePad.instance();
28+
beforeEach(() => {
29+
jest.restoreAllMocks();
30+
scaleCanvas(1024, 768);
31+
});
1232

1333
it('renders the component', () => {
34+
const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />);
35+
1436
expect(signaturePad.exists()).toBeTruthy();
1537
});
1638

1739
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+
1945
expect(instance.isEmpty()).toBeTruthy();
2046

21-
// Load a signature
2247
instance.fromDataURL(signature);
48+
2349
expect(instance.isEmpty()).toBeFalsy();
2450
});
2551

2652
it('clears the signature pad', () => {
27-
// Load a signature
53+
const signaturePad = mount<SignaturePad>(<SignaturePad redrawOnResize />);
54+
const instance = signaturePad.instance();
55+
2856
instance.fromDataURL(signature);
57+
2958
expect(instance.isEmpty()).toBeFalsy();
3059

3160
instance.clear();
61+
3262
expect(instance.isEmpty()).toBeTruthy();
3363
});
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+
});
34221
});
35222
});

__tests__/helpers/resizeWindow.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

__tests__/shim.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)