Skip to content

Commit 3985dc1

Browse files
committed
(refactor/types): rewrite tests in TypeScript
- not much to change except for some code to cast things to correct types - remove file extensions from TS imports as TS errors on them - configure .ts for ts-jest, and remove testMatch as we only have .spec.tsx? extensions now - leave out test/config/ set-up files as I'd like to split those out into separate packages anyway and their types aren't really necessary for test correctness (deps): add @types/enzyme for more explicit typings of tests
1 parent 3075293 commit 3985dc1

File tree

5 files changed

+42
-18
lines changed

5 files changed

+42
-18
lines changed

jest.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ module.exports = {
1010
transform: {
1111
// support babel-jest. TSDX defaults to just ts-jest. see https://github.com/jaredpalmer/tsdx/pull/486
1212
'\\.js$': 'babel-jest',
13-
'\\.tsx$': 'ts-jest'
13+
'\\.tsx?$': 'ts-jest'
1414
},
15-
// support JS + JSX. TSDX defaults to just TS + TSX. see https://github.com/jaredpalmer/tsdx/pull/486
16-
testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
1715
coveragePathIgnorePatterns: [
1816
'/node_modules/', // default
1917
'<rootDir>/test/' // ignore any test helper files

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@agilgur5/changelog-maker": "^3.0.0",
6464
"@babel/core": "^7.8.4",
6565
"@babel/preset-react": "^7.8.3",
66+
"@types/enzyme": "^3.10.4",
6667
"@types/prop-types": "^15.7.3",
6768
"@types/react": "^17.0.44",
6869
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.6",

test/fixtures.js renamed to test/fixtures.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import SignaturePad from 'signature_pad'
2+
13
// signature_pad options
24
const sigPadOptions = {
35
velocityFilterWeight: 0.8,
@@ -21,7 +23,7 @@ export const propsF = { sigPadOptions, all: props }
2123

2224
const dotData = [
2325
[{ x: 466.59375, y: 189, time: 1564339579755, color: 'black' }]
24-
]
26+
] as SignaturePad.Point[][]
2527
const canvasProps = { width: 1011, height: 326 }
2628
const trimmedSize = { width: 4, height: 4 }
2729
export const dotF = { data: dotData, canvasProps, trimmedSize }

test/index.spec.js renamed to test/index.spec.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import { jest, describe, it, test, expect } from 'jest-without-globals'
2-
import { mount } from 'enzyme'
2+
import { mount, CommonWrapper } from 'enzyme'
33
import React from 'react'
44

5-
import SignatureCanvas from '../src/index.tsx'
6-
import { propsF, dotF } from './fixtures.js'
5+
import SignatureCanvas from '../src/index'
6+
import { propsF, dotF } from './fixtures'
7+
8+
function rSCInstance (wrapper: CommonWrapper): SignatureCanvas {
9+
return wrapper.instance() as SignatureCanvas
10+
}
711

812
test('mounts canvas and instance properly', () => {
913
const wrapper = mount(<SignatureCanvas />)
1014
expect(wrapper.exists('canvas')).toBe(true)
11-
const instance = wrapper.instance()
15+
const instance = rSCInstance(wrapper)
1216
expect(instance.isEmpty()).toBe(true)
1317
})
1418

1519
describe('setting and updating props', () => {
1620
it('should set default props', () => {
17-
const instance = mount(<SignatureCanvas />).instance()
21+
const instance = rSCInstance(mount(<SignatureCanvas />))
1822
expect(instance.props).toStrictEqual(SignatureCanvas.defaultProps)
1923
})
2024

2125
it('should set initial mount props and SigPad options', () => {
22-
const instance = mount(<SignatureCanvas {...propsF.all} />).instance()
26+
const instance = rSCInstance(mount(<SignatureCanvas {...propsF.all} />))
2327
const sigPad = instance.getSignaturePad()
2428

2529
expect(instance.props).toMatchObject(propsF.all)
@@ -28,7 +32,7 @@ describe('setting and updating props', () => {
2832

2933
it('should update props and SigPad options', () => {
3034
const wrapper = mount(<SignatureCanvas />)
31-
const instance = wrapper.instance()
35+
const instance = rSCInstance(wrapper)
3236
const sigPad = instance.getSignaturePad()
3337

3438
// default props and options should not match new ones
@@ -43,7 +47,7 @@ describe('setting and updating props', () => {
4347
})
4448

4549
describe('SigCanvas wrapper methods return equivalent to SigPad', () => {
46-
const rSigPad = mount(<SignatureCanvas />).instance()
50+
const rSigPad = rSCInstance(mount(<SignatureCanvas />))
4751
const sigPad = rSigPad.getSignaturePad()
4852

4953
test('toData should be equivalent', () => {
@@ -118,9 +122,9 @@ describe('SigCanvas wrapper methods return equivalent to SigPad', () => {
118122

119123
// comes after props and wrapper methods as it uses both
120124
describe('get methods', () => {
121-
const instance = mount(
125+
const instance = rSCInstance(mount(
122126
<SignatureCanvas canvasProps={dotF.canvasProps} />
123-
).instance()
127+
))
124128
instance.fromData(dotF.data)
125129

126130
test('getCanvas should return the same underlying canvas', () => {
@@ -138,7 +142,7 @@ describe('get methods', () => {
138142
// comes after props, wrappers, and gets as it uses them all
139143
describe('canvas resizing', () => {
140144
const wrapper = mount(<SignatureCanvas />)
141-
const instance = wrapper.instance()
145+
const instance = rSCInstance(wrapper)
142146
const canvas = instance.getCanvas()
143147

144148
it('should clear on resize', () => {
@@ -196,7 +200,7 @@ describe('canvas resizing', () => {
196200
// comes after wrappers and resizing as it uses both
197201
describe('on & off methods', () => {
198202
const wrapper = mount(<SignatureCanvas />)
199-
const instance = wrapper.instance()
203+
const instance = rSCInstance(wrapper)
200204

201205
it('should not clear when off, should clear when back on', () => {
202206
instance.fromData(dotF.data)
@@ -214,8 +218,8 @@ describe('on & off methods', () => {
214218
it('should no longer fire after unmount', () => {
215219
// monkey-patch on with a mock to tell if it were called, as there's no way
216220
// to check what event listeners are attached to window
217-
instance._on = instance.on
218-
instance.on = jest.fn(instance._on)
221+
const origOn = instance.on
222+
instance.on = jest.fn(origOn)
219223

220224
wrapper.unmount()
221225
window.resizeTo(500, 500)

0 commit comments

Comments
 (0)