Skip to content

Commit 58da3e5

Browse files
committed
(test): ensure resizing works correctly
- clearOnResize and fixed/unfixed width & height should affect resizing as expected - also yea the tests run in parallel, but readability-wise it makes more sense I think(?) - add a window.resizeTo polyfill as jsdom doesn't implement it o.o - and there doesn't seem to be an NPM package for this(?)
1 parent 1627a60 commit 58da3e5

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

jest.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ module.exports = {
55
},
66
setupFilesAfterEnv: [
77
// configure enzyme w/ react adapter
8-
'<rootDir>/test-utils/configure-enzyme.js'
8+
'<rootDir>/test-utils/configure-enzyme.js',
9+
// polyfill window.resizeTo
10+
'<rootDir>/test-utils/window-resizeTo.js'
911
],
1012
transform: {
1113
// use babel-jest@23 for babel@6 support (https://github.com/facebook/jest/issues/8230#issuecomment-479470547)

src/index.spec.js

+58
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ describe('SigCanvas wrapper methods return equivalent to SigPad', () => {
116116
})
117117
})
118118

119+
// comes after props and wrapper methods as it uses both
119120
describe('get methods return correct canvases', () => {
120121
const instance = mount(
121122
<SignatureCanvas canvasProps={dotF.canvasProps} />
@@ -133,3 +134,60 @@ describe('get methods return correct canvases', () => {
133134
expect(trimmed.height).toBe(dotF.trimmedSize.height)
134135
})
135136
})
137+
138+
// comes after props, wrappers, and gets as it uses them all
139+
describe('resizing works correctly', () => {
140+
const wrapper = mount(<SignatureCanvas />)
141+
const instance = wrapper.instance()
142+
const canvas = instance.getCanvas()
143+
144+
test('canvas should clear on resize', () => {
145+
instance.fromData(dotF.data)
146+
expect(instance.isEmpty()).toBe(false)
147+
148+
window.resizeTo(500, 500)
149+
expect(instance.isEmpty()).toBe(true)
150+
})
151+
152+
test('canvas should not clear when clearOnResize is false', () => {
153+
wrapper.setProps({ clearOnResize: false })
154+
155+
instance.fromData(dotF.data)
156+
expect(instance.isEmpty()).toBe(false)
157+
158+
window.resizeTo(500, 500)
159+
expect(instance.isEmpty()).toBe(false)
160+
})
161+
162+
const size = { width: 100, height: 100 }
163+
test('canvas should not change size if fixed width & height', () => {
164+
wrapper.setProps({ canvasProps: size })
165+
window.resizeTo(500, 500)
166+
167+
expect(canvas.width).toBe(size.width)
168+
expect(canvas.height).toBe(size.height)
169+
})
170+
171+
test('canvas should change size if no width or height', () => {
172+
wrapper.setProps({ canvasProps: {} })
173+
window.resizeTo(500, 500)
174+
175+
expect(canvas.width).not.toBe(size.width)
176+
expect(canvas.height).not.toBe(size.height)
177+
})
178+
179+
test('canvas should partially change size if one of width or height', () => {
180+
wrapper.setProps({ canvasProps: { width: size.width } })
181+
window.resizeTo(500, 500)
182+
183+
expect(canvas.width).toBe(size.width)
184+
expect(canvas.height).not.toBe(size.height)
185+
186+
// now do height instead
187+
wrapper.setProps({ canvasProps: { height: size.height } })
188+
window.resizeTo(500, 500)
189+
190+
expect(canvas.width).not.toBe(size.width)
191+
expect(canvas.height).toBe(size.height)
192+
})
193+
})

test-utils/window-resizeTo.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// polyfill window.resizeTo in jsdom environment
2+
// https://spectrum.chat/testing-library/help-react/how-to-set-window-innerwidth-to-test-mobile~70aa9572-b7cc-4397-92f5-a09d75ed24b8?m=MTU1OTU5MTI2MTI0MQ==
3+
window.resizeTo = function resizeTo (width, height) {
4+
Object.assign(window, {
5+
innerWidth: width,
6+
innerHeight: height,
7+
outerWidth: width,
8+
outerHeight: height
9+
}).dispatchEvent(new window.Event('resize'))
10+
}

0 commit comments

Comments
 (0)