From 8b84b7afa9aa1e756d2fdc626c95aa87a9480ec5 Mon Sep 17 00:00:00 2001 From: okazari Date: Wed, 12 May 2021 10:52:03 +0200 Subject: [PATCH] fix: .toHaveValue()/.toHaveFormValues() on input type range now expect a number --- README.md | 7 +++++-- src/__tests__/to-have-form-values.js | 15 +++++++++++++++ src/__tests__/to-have-value.js | 23 +++++++++++++++++++++++ src/utils.js | 1 + 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 94b8a5af..b3c0fce9 100644 --- a/README.md +++ b/README.md @@ -636,8 +636,8 @@ is obtained depending on the type of form control. For instance, `` elements have a `value` attribute, but `` elements return the value as a **number**, instead of - a string. +- `` and `` elements return the value + as a **number**, instead of a string. - `` elements: - if there's a single one with the given `name` attribute, it is treated as a **boolean**, returning `true` if the checkbox is checked, `false` if @@ -796,6 +796,7 @@ For all other form elements, the value is matched using the same algorithm as in ```html + values correctly', () => { + const renderInputRange = value => { + const {container} = render(` +
+ +
+ `) + return container.querySelector('form') + } + + expect(renderInputRange('10')).toHaveFormValues({ + volume: 10, + }) + }) + describe('edge cases', () => { // This is also to ensure 100% code coverage for edge cases it('detects multiple elements with the same name but different type', () => { diff --git a/src/__tests__/to-have-value.js b/src/__tests__/to-have-value.js index d0159361..f4c71720 100644 --- a/src/__tests__/to-have-value.js +++ b/src/__tests__/to-have-value.js @@ -47,6 +47,29 @@ describe('.toHaveValue', () => { expect(queryByTestId('without')).toHaveValue(10) }) + test('handles value of range input', () => { + const {queryByTestId} = render(` + + + + `) + + expect(queryByTestId('range')).toHaveValue(5) + expect(queryByTestId('range')).toHaveValue() + expect(queryByTestId('range')).not.toHaveValue(4) + expect(queryByTestId('range')).not.toHaveValue('5') + + // Input type range value default to 50 + expect(queryByTestId('empty')).toHaveValue(50) + expect(queryByTestId('empty')).not.toHaveValue('5') + + // Input type range value default to 50 + expect(queryByTestId('without')).toHaveValue(50) + expect(queryByTestId('without')).not.toHaveValue('10') + queryByTestId('without').value = 10 + expect(queryByTestId('without')).toHaveValue(10) + }) + test('handles value of select element', () => { const {queryByTestId} = render(`