-
Notifications
You must be signed in to change notification settings - Fork 392
/
NumberInput.test.tsx
99 lines (88 loc) · 3.22 KB
/
NumberInput.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// eslint-disable-next-line import/no-unassigned-import
import '@testing-library/jest-dom/extend-expect'
import React from 'react'
import {renderNumberInput} from '../../../../test/form'
import {NumberInput} from './NumberInput'
describe('number-input', () => {
it('renders the number input field', async () => {
const {result} = await renderNumberInput({
fieldDefinition: {
name: 'defaultNumber',
title: 'Integer',
type: 'number',
},
render: (inputProps) => <NumberInput {...inputProps} />,
})
const input = result.container.querySelector('input')
expect(input).toBeDefined()
expect(input).toHaveAttribute('type', 'number')
})
it('accepts decimals by default', async () => {
const {result} = await renderNumberInput({
fieldDefinition: {
name: 'defaultNumber',
title: 'Integer',
type: 'number',
},
render: (inputProps) => <NumberInput {...inputProps} />,
})
const input = result.container.querySelector('input')
input!.value = '1.2'
expect(input!.valueAsNumber).toBe(1.2)
expect(input!.checkValidity()).toBe(true)
})
it('renders inputMode equals text if there is no min rule', async () => {
// Note: we want "text" because devices may or may not show a minus key.
// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode#values
const {result} = await renderNumberInput({
fieldDefinition: {
name: 'defaultNumber',
title: 'Integer',
type: 'number',
},
render: (inputProps) => <NumberInput {...inputProps} />,
})
const input = result.container.querySelector('input')!
expect(input.inputMode).toBe('text')
})
it('renders inputMode equals "decimal" if there is a min rule', async () => {
const {result} = await renderNumberInput({
fieldDefinition: {
name: 'positiveNumber',
title: 'A positive number',
type: 'number',
validation: (Rule) => Rule.positive(),
},
render: (inputProps) => <NumberInput {...inputProps} />,
})
const input = result.container.querySelector('input')!
expect(input.inputMode).toBe('decimal')
})
it('renders inputMode equals "numeric" if there is a min rule and integer rule', async () => {
const {result} = await renderNumberInput({
fieldDefinition: {
name: 'positiveInteger',
title: 'Integer',
type: 'number',
validation: (Rule) => Rule.integer().positive(),
},
render: (inputProps) => <NumberInput {...inputProps} />,
})
const input = result.container.querySelector('input')!
expect(input.inputMode).toBe('numeric')
})
it('renders inputMode equals "numeric" if there is a min rule and zero precision rule', async () => {
const {result} = await renderNumberInput({
fieldDefinition: {
// should be handled the same way as an integer
name: 'positiveZeroPrecisionNumber',
title: 'Integer',
type: 'number',
validation: (Rule) => Rule.precision(0).positive(),
},
render: (inputProps) => <NumberInput {...inputProps} />,
})
const input = result.container.querySelector('input')!
expect(input.inputMode).toBe('numeric')
})
})