Skip to content

Commit

Permalink
chore: update test environment to React18 (#891)
Browse files Browse the repository at this point in the history
test in React17 and React18
  • Loading branch information
ph-fritsche committed Mar 31, 2022
1 parent 886780c commit 810c0d8
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 102 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/validate.yml
Expand Up @@ -32,6 +32,7 @@ jobs:
uses: bahmutov/npm-install@v1
with:
useLockFile: false
install-command: yarn --silent
env:
HUSKY_SKIP_INSTALL: true

Expand Down Expand Up @@ -61,6 +62,7 @@ jobs:
uses: bahmutov/npm-install@v1
with:
useLockFile: false
install-command: yarn --silent
env:
HUSKY_SKIP_INSTALL: true

Expand Down
5 changes: 4 additions & 1 deletion jest.config.js
Expand Up @@ -10,7 +10,10 @@ config.moduleNameMapper = {

config.testEnvironment = 'jsdom'

config.setupFilesAfterEnv = ['<rootDir>/tests/_setup-env.js']
config.setupFilesAfterEnv = [
'<rootDir>/tests/_setup-env.js',
'<rootDir>/tests/react/_env/setup-env.js',
]

config.testMatch.push('<rootDir>/tests/**/*.+(js|jsx|ts|tsx)')

Expand Down
11 changes: 7 additions & 4 deletions package.json
Expand Up @@ -44,11 +44,10 @@
"validate": "kcd-scripts validate",
"typecheck": "kcd-scripts typecheck"
},
"dependencies": {},
"devDependencies": {
"@testing-library/dom": "^8.11.4",
"@testing-library/jest-dom": "^5.16.3",
"@testing-library/react": "^12.1.4",
"@testing-library/react": "^13.0.0",
"@types/jest-in-case": "^1.0.3",
"@types/react": "^17.0.42",
"esbuild": "^0.14.27",
Expand All @@ -57,8 +56,12 @@
"jest-in-case": "^1.0.2",
"jest-serializer-ansi": "^1.0.3",
"kcd-scripts": "^12.1.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react17": "npm:react@^17.0.2",
"reactDom17": "npm:react-dom@^17.0.2",
"reactIs17": "npm:react-is@^17.0.2",
"reactTesting17": "npm:@testing-library/react@^12.1.3",
"typescript": "^4.1.2"
},
"peerDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions tests/react/17.tsx
@@ -0,0 +1,5 @@
/**
* @jest-environment ./tests/react/_env/react17.js
*/

import './index'
9 changes: 9 additions & 0 deletions tests/react/_env/react17.js
@@ -0,0 +1,9 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const jsdomEnv = require('jest-environment-jsdom')

module.exports = class React17 extends jsdomEnv {
async setup() {
await super.setup()
this.global.REACT_VERSION = 17
}
}
12 changes: 12 additions & 0 deletions tests/react/_env/setup-env.js
@@ -0,0 +1,12 @@
if (global.REACT_VERSION) {
jest.mock('@testing-library/react', () =>
jest.requireActual(`reactTesting${global.REACT_VERSION}`),
)
jest.mock('react', () => jest.requireActual(`react${global.REACT_VERSION}`))
jest.mock('react-dom', () =>
jest.requireActual(`reactDom${global.REACT_VERSION}`),
)
jest.mock('react-is', () =>
jest.requireActual(`reactIs${global.REACT_VERSION}`),
)
}
103 changes: 6 additions & 97 deletions tests/react/index.tsx
Expand Up @@ -131,43 +131,15 @@ describe('typing in a formatted input', () => {
}

test('typing in empty formatted input', async () => {
const {element, getEventSnapshot, user} = setupDollarInput()
const {element, user} = setupDollarInput()

await user.type(element, '23')

expect(element).toHaveValue('$23')
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value="$23"]
input[value=""] - pointerover
input[value=""] - pointerenter
input[value=""] - mouseover
input[value=""] - mouseenter
input[value=""] - pointermove
input[value=""] - mousemove
input[value=""] - pointerdown
input[value=""] - mousedown: primary
input[value=""] - focus
input[value=""] - focusin
input[value=""] - pointerup
input[value=""] - mouseup: primary
input[value=""] - click: primary
input[value=""] - keydown: 2
input[value=""] - keypress: 2
input[value=""] - beforeinput
input[value="2"] - input
"2{CURSOR}" -> "$2{CURSOR}"
input[value="$2"] - keyup: 2
input[value="$2"] - keydown: 3
input[value="$2"] - keypress: 3
input[value="$2"] - beforeinput
input[value="$23"] - input
input[value="$23"] - keyup: 3
`)
})

test('typing in the middle of a formatted input', async () => {
const {element, getEventSnapshot, user} = setupDollarInput({
const {element, user} = setupDollarInput({
initialValue: '$23',
})

Expand All @@ -176,37 +148,10 @@ describe('typing in a formatted input', () => {
expect(element).toHaveValue('$213')
expect(element).toHaveProperty('selectionStart', 3)
expect(element).toHaveProperty('selectionEnd', 3)

expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value="$213"]
input[value="$23"] - pointerover
input[value="$23"] - pointerenter
input[value="$23"] - mouseover
input[value="$23"] - mouseenter
input[value="$23"] - pointermove
input[value="$23"] - mousemove
input[value="$23"] - pointerdown
input[value="$23"] - mousedown: primary
input[value="$23"] - focus
input[value="$23"] - focusin
input[value="$23"] - pointerup
input[value="$23"] - mouseup: primary
input[value="$23"] - click: primary
input[value="$23"] - select
input[value="$23"] - keydown: 1
input[value="$23"] - keypress: 1
input[value="$23"] - beforeinput
input[value="$213"] - select
input[value="$213"] - input
"$21{CURSOR}3" -> "$213{CURSOR}"
input[value="$213"] - select
input[value="$213"] - keyup: 1
`)
})

test('ignored {backspace} in formatted input', async () => {
const {element, getEventSnapshot, user} = setupDollarInput({
const {element, user} = setupDollarInput({
initialValue: '$23',
})

Expand All @@ -220,47 +165,11 @@ describe('typing in a formatted input', () => {
// before. When the value is set programmatically to something different
// from what was expected based on the input event, the browser sets
// the selection start and end to the end of the input
expect(element.selectionStart).toBe(element.value.length)
expect(element.selectionEnd).toBe(element.value.length)
expect(element.selectionStart).toBe(3)
expect(element.selectionEnd).toBe(3)

await user.type(element, '4')

expect(element).toHaveValue('$234')
// the backslash in the inline snapshot is to escape the $ before {CURSOR}
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value="$234"]
input[value="$23"] - pointerover
input[value="$23"] - pointerenter
input[value="$23"] - mouseover
input[value="$23"] - mouseenter
input[value="$23"] - pointermove
input[value="$23"] - mousemove
input[value="$23"] - pointerdown
input[value="$23"] - mousedown: primary
input[value="$23"] - focus
input[value="$23"] - focusin
input[value="$23"] - pointerup
input[value="$23"] - mouseup: primary
input[value="$23"] - click: primary
input[value="$23"] - select
input[value="$23"] - keydown: Backspace
input[value="$23"] - beforeinput
input[value="23"] - select
input[value="23"] - input
"{CURSOR}23" -> "$23{CURSOR}"
input[value="$23"] - keyup: Backspace
input[value="$23"] - pointermove
input[value="$23"] - mousemove
input[value="$23"] - pointerdown
input[value="$23"] - mousedown: primary
input[value="$23"] - pointerup
input[value="$23"] - mouseup: primary
input[value="$23"] - click: primary
input[value="$23"] - keydown: 4
input[value="$23"] - keypress: 4
input[value="$23"] - beforeinput
input[value="$234"] - input
input[value="$234"] - keyup: 4
`)
})
})

0 comments on commit 810c0d8

Please sign in to comment.