From c7634ee0e0065ed940f39255c201daa1c45aa807 Mon Sep 17 00:00:00 2001 From: Ryan Castner Date: Mon, 18 Dec 2017 19:55:18 -0500 Subject: [PATCH] Add tests for radios and checkbox groups --- tests/checkbox-radio.js | 26 +++++++++++++++++++++++++- tests/radio-button.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/radio-button.js diff --git a/tests/checkbox-radio.js b/tests/checkbox-radio.js index 69df5ac..6e7df88 100644 --- a/tests/checkbox-radio.js +++ b/tests/checkbox-radio.js @@ -10,7 +10,7 @@ const Input = ({ error, ...props }) => ( ); -test('Input works with checkbox', () => { +test('Input works with checkbox that has no value', () => { const wrapper = mount(
@@ -29,3 +29,27 @@ test('Input works with checkbox', () => { expect(wrapper.find(Input).props().value).toEqual(false); }); + +test('Input works with checkbox that has value', () => { + const wrapper = mount( + + + +
+ ); + + wrapper.find('input').first().simulate('change', { + target: { name: 'Awesome', checked: true, type: 'checkbox', value: 'impressive' }, + }); + + expect(wrapper.find(Input).first().props().value).toEqual('impressive'); + // because we arent intelligently managing this group of inputs the values get overwritten + expect(wrapper.find(Input).last().props().value).toEqual('impressive'); + + wrapper.find('input').first().simulate('change', { + target: { name: 'Awesome', checked: false, type: 'checkbox', value: 'impressive' }, + }); + + expect(wrapper.find(Input).first().props().value).toEqual('impressive'); + expect(wrapper.find(Input).last().props().value).toEqual('impressive'); +}) diff --git a/tests/radio-button.js b/tests/radio-button.js new file mode 100644 index 0000000..8d3e644 --- /dev/null +++ b/tests/radio-button.js @@ -0,0 +1,31 @@ +//Test that components work with fields that pass target.checked and target.value +import React from 'react'; +import Form from '../src/form'; +import { mount } from 'enzyme'; + +const Input = ({ error, ...props }) => ( +
+ {error ?

{error}

: null} + +
+); + +test('Input works with radio buttons', () => { + const wrapper = mount( +
+ + +
+ ); + wrapper.find('[type="radio"]').last().simulate('change', { + target: { name: 'lightbulb', value: 'off', type: 'radio' }, + }); + + const radios = wrapper.find('[type="radio"]'); + + // because we aren't intelligently managing this radio group the value gets overwritten + expect(radios.first().props().value).toEqual('off'); + expect(radios.first().props().checked).toEqual(true); + expect(radios.last().props().value).toEqual('off'); + expect(radios.last().props().checked).toEqual(undefined); +});