import { describe, expect, test } from 'vitest' import { render } from './utils' describe('.toHaveValue', () => { test('handles value of text input', () => { const {queryByTestId, getInputByTestId} = render(` `) expect(queryByTestId('value')).toHaveValue('foo') expect(queryByTestId('value')).toHaveValue() expect(queryByTestId('value')).not.toHaveValue('bar') expect(queryByTestId('value')).not.toHaveValue('') expect(queryByTestId('empty')).toHaveValue('') expect(queryByTestId('empty')).not.toHaveValue() expect(queryByTestId('empty')).not.toHaveValue('foo') expect(queryByTestId('without')).toHaveValue('') expect(queryByTestId('without')).not.toHaveValue() expect(queryByTestId('without')).not.toHaveValue('foo') getInputByTestId('without').value = 'bar' expect(queryByTestId('without')).toHaveValue('bar') }) test('handles value of number input', () => { const {queryByTestId, getInputByTestId} = render(` `) expect(queryByTestId('number')).toHaveValue(5) expect(queryByTestId('number')).toHaveValue() expect(queryByTestId('number')).not.toHaveValue(4) expect(queryByTestId('number')).not.toHaveValue('5') expect(queryByTestId('empty')).toHaveValue(null) expect(queryByTestId('empty')).not.toHaveValue() expect(queryByTestId('empty')).not.toHaveValue('5') expect(queryByTestId('without')).toHaveValue(null) expect(queryByTestId('without')).not.toHaveValue() expect(queryByTestId('without')).not.toHaveValue('10') // @ts-expect-error ts doesn't allow value to be a number, but browser will convert it getInputByTestId('without').value = 10 expect(queryByTestId('without')).toHaveValue(10) }) test('handles value of select element', () => { const {queryByTestId} = render(` `) expect(queryByTestId('single')).toHaveValue('second') expect(queryByTestId('single')).toHaveValue() expect(queryByTestId('multiple')).toHaveValue(['second', 'third']) expect(queryByTestId('multiple')).toHaveValue() expect(queryByTestId('not-selected')).not.toHaveValue() expect(queryByTestId('not-selected')).toHaveValue('') queryByTestId('single').children[0].setAttribute('selected', 'true') expect(queryByTestId('single')).toHaveValue('first') }) test('handles value of textarea element', () => { const {queryByTestId} = render(` `) expect(queryByTestId('textarea')).toHaveValue('text value') }) test('throws when passed checkbox or radio', () => { const {queryByTestId} = render(` `) expect(() => { expect(queryByTestId('checkbox')).toHaveValue('') }).toThrow() expect(() => { expect(queryByTestId('radio')).toHaveValue('') }).toThrow() }) test('throws when the expected input value does not match', () => { const {container} = render(``) const input = container.firstChild let errorMessage try { expect(input).toHaveValue('something else') } catch (error) { errorMessage = error.message } expect(errorMessage).toMatchInlineSnapshot(` expect(element).toHaveValue(something else) Expected the element to have value: something else Received: foo `) }) test('throws with type information when the expected text input value has loose equality with received value', () => { const {container} = render(``) const input = container.firstChild let errorMessage try { expect(input).toHaveValue(8) } catch (error) { errorMessage = error.message } expect(errorMessage).toMatchInlineSnapshot(` expect(element).toHaveValue(8) Expected the element to have value: 8 (number) Received: 8 (string) `) }) test('throws when using not but the expected input value does match', () => { const {container} = render(``) const input = container.firstChild let errorMessage try { expect(input).not.toHaveValue('foo') } catch (error) { errorMessage = error.message } expect(errorMessage).toMatchInlineSnapshot(` expect(element).not.toHaveValue(foo) Expected the element not to have value: foo Received: foo `) }) test('throws when the form has no a value but a value is expected', () => { const {container} = render(``) const input = container.firstChild let errorMessage try { expect(input).toHaveValue() } catch (error) { errorMessage = error.message } expect(errorMessage).toMatchInlineSnapshot(` expect(element).toHaveValue(expected) Expected the element to have value: (any) Received: `) }) test('throws when the form has a value but none is expected', () => { const {container} = render(``) const input = container.firstChild let errorMessage try { expect(input).not.toHaveValue() } catch (error) { errorMessage = error.message } expect(errorMessage).toMatchInlineSnapshot(` expect(element).not.toHaveValue(expected) Expected the element not to have value: (any) Received: foo `) }) test('handles value of aria-valuenow', () => { const valueToCheck = 70 const {queryByTestId} = render(`
`) expect(queryByTestId('meter')).toHaveValue(valueToCheck) expect(queryByTestId('meter')).not.toHaveValue(valueToCheck + 1) // Role that does not support aria-valuenow expect(queryByTestId('textbox')).not.toHaveValue(70) }) })