aboutsummaryrefslogtreecommitdiffstats
path: root/test/react/Input.spec.js
blob: 3766e45f2d3eadb3e4fc5a757b7070ed638756d7 (plain)
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
import React from 'react';
import Input from '../../src/react/Input.js';

import renderer from 'react-test-renderer';
import {mount} from 'enzyme';

class InputForm extends React.Component {
	constructor(props) {
		super(props);
		this.state = {value: 'Initial'};
		this.handleChange = this.handleChange.bind(this);
	}

	handleChange(val) {
		this.setState({value: val});
	}

	getValue() {
		return this.input.getValue();
	}

	render() {
		return (
			<form >
				<Input ref={(input)=>{this.input = input;}} value={this.state.value} name='testinput' onChange={this.handleChange} />
			</form>
		);
	}
}

describe('Input', () => {
	test('Input - default', () => {
		const input = renderer.create(<Input label='label for input' name='clickme' type='text'/>).toJSON();
		expect(input).toMatchSnapshot();
	});

	test('Input - required', () => {
		const input = renderer.create(<Input required label='label for input' name='clickme' type='text'/>).toJSON();
		expect(input).toMatchSnapshot();
	});

	test('Input - number', () => {
		const input = renderer.create(<Input  label='label for input' name='clickme' type='number'/>).toJSON();
		expect(input).toMatchSnapshot();
	});

	test('Input - readonly', () => {
		const input = renderer.create(<Input  label='label for input' name='clickme' type='text' readOnly/>).toJSON();
		expect(input).toMatchSnapshot();
	});

	test('Input - placeholder', () => {
		const input = renderer.create(<Input  label='label for input' name='clickme' type='text' placeholder='hint'/>).toJSON();
		expect(input).toMatchSnapshot();
	});

	test('Input - error', () => {
		const input = renderer.create(<Input  label='label for input' name='clickme' type='text' errorMessage='this is an error'/>).toJSON();
		expect(input).toMatchSnapshot();
	});

	test('Input - checked state value changes', () => {
		const input = mount(<InputForm />);
		expect(input.instance().getValue()).toEqual('Initial');
		input.find('input').simulate('change', { target : { value: 'Changed' }});
		expect(input.instance().getValue()).toEqual('Changed');
		expect(input.find('input').prop('value')).toEqual('Changed');
	});
});