ParameterCreate.spec.jsx 5.55 KB
Newer Older
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
// @flow
import React from 'react';
import { expect } from 'chai';
import { mount } from 'enzyme';
import sinon from 'sinon';
import { spies } from '@test';

import C from './ParameterCreate.jsx';

describe('<ParameterCreate />', () => {
	let wrapper;
	const name = 'parameter';
	// shortcut for a func to update just the parameter obj, not the name
	const _updateParameter = (name, p, oldName) => {
		wrapper.setProps({
			param: p,
		});
	};
	const params = [];

	afterEach(() => {
		if(wrapper && wrapper.unmount)
			wrapper.unmount();
	});

	it(`saves the default value of a boolean parameter`, () => {
		let param = {
			type: '',
			default: '',
			description: '',
		};
		const updateParameter = sinon.spy(_updateParameter);

		wrapper = mount(
			<C
				name={name}
				param={param}
				params={params}
				updateParameter={updateParameter}
			/>
		);

		// sanity checks
		expect(wrapper.find('TypedField')).to.have.prop('name', name);

		wrapper.find('Input.custom-select').prop('onChange')( { target: { value: 'bool' }});
		wrapper.update();
		expect(wrapper.props().param).to.deep.equal(
			{
				type: 'bool',
				default: '',
				description: '',
			}
		);

		wrapper.find('input[type="radio"]').at(0).simulate('change', { target: { checked: 'true' }});;
		//wrapper.find('input[type="radio"]').at(0).simulate('change', { target: { value: 'true' }});;
		wrapper.update();
		expect(wrapper.props().param).to.deep.equal({
			type: 'bool',
			default: true,
			description: '',
		});
	});
65

66
	describe('regression', () => {
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 100
		it(`doesnt let min be larger than max in a range parameter`, () => {
			let param = {
				type: 'int8',
				default: '',
				description: '',
				range: [
					1,
					2
				]
			};
			const updateParameter = sinon.spy(_updateParameter);

			wrapper = mount(
				<C
					name={name}
					param={param}
					params={params}
					updateParameter={updateParameter}
				/>
			);

			// sanity checks
			expect(wrapper.find('TypedField')).to.have.prop('name', name);

			wrapper.find('Input[step="1"]').at(0).prop('onChange')( { target: { value: 3 }});
			wrapper.update();
			expect(updateParameter.callCount).to.equal(0);
			expect(wrapper.props().param.range).to.deep.equal([1, 2]);

			wrapper.find('Input[step="1"]').at(0).prop('onChange')( { target: { value: 2 }});
			wrapper.update();
			expect(updateParameter.callCount).to.equal(1);
			expect(wrapper.props().param.range).to.deep.equal([2, 2]);
		});
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		it(`doesnt let min be larger than max in a range parameter for negative numbers`, () => {
			let param = {
				type: 'int8',
				default: '',
				description: '',
				range: [
					-1,
					-2
				]
			};
			const updateParameter = sinon.spy(_updateParameter);

			wrapper = mount(
				<C
					name={name}
					param={param}
					params={params}
					updateParameter={updateParameter}
				/>
			);

			// sanity checks
			expect(wrapper.find('TypedField')).to.have.prop('name', name);

			wrapper.find('Input[step="1"]').at(0).prop('onChange')( { target: { value: -1 }});
			wrapper.update();
			expect(updateParameter.callCount).to.equal(0);
			expect(wrapper.props().param.range).to.deep.equal([-1, -2]);

			wrapper.find('Input[step="1"]').at(0).prop('onChange')( { target: { value: -2 }});
			wrapper.update();
			expect(updateParameter.callCount).to.equal(1);
			expect(wrapper.props().param.range).to.deep.equal([-2, -2]);

			wrapper.find('Input[step="1"]').at(0).prop('onChange')( { target: { value: -3 }});
			wrapper.update();
			expect(updateParameter.callCount).to.equal(2);
			expect(wrapper.props().param.range).to.deep.equal([-3, -2]);
		});
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
		it(`doesnt let max be smaller than min in a range parameter`, () => {
			let param = {
				type: 'int8',
				default: '',
				description: '',
				range: [
					3,
					4
				]
			};
			const updateParameter = sinon.spy(_updateParameter);

			wrapper = mount(
				<C
					name={name}
					param={param}
					params={params}
					updateParameter={updateParameter}
				/>
			);

			// sanity checks
			expect(wrapper.find('TypedField')).to.have.prop('name', name);

			wrapper.find('Input[step="1"]').at(1).prop('onChange')( { target: { value: 1 }});
			wrapper.update();
			expect(updateParameter.callCount).to.equal(0);
			expect(wrapper.props().param.range).to.deep.equal([3, 4]);

			wrapper.find('Input[step="1"]').at(1).prop('onChange')( { target: { value: 3 }});
			wrapper.update();
			expect(updateParameter.callCount).to.equal(1);
			expect(wrapper.props().param.range).to.deep.equal([3, 3]);
		});
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
		it(`doesnt let max be smaller than min in a range parameter for negative numbers`, () => {
			let param = {
				type: 'int8',
				default: '',
				description: '',
				range: [
					-3,
					-2
				]
			};
			const updateParameter = sinon.spy(_updateParameter);

			wrapper = mount(
				<C
					name={name}
					param={param}
					params={params}
					updateParameter={updateParameter}
				/>
			);

			// sanity checks
			expect(wrapper.find('TypedField')).to.have.prop('name', name);

			wrapper.find('Input[step="1"]').at(1).prop('onChange')( { target: { value: -5 }});
			wrapper.update();
			expect(updateParameter.callCount).to.equal(0);
			expect(wrapper.props().param.range).to.deep.equal([-3, -2]);

			wrapper.find('Input[step="1"]').at(1).prop('onChange')( { target: { value: -3 }});
			wrapper.update();
			expect(updateParameter.callCount).to.equal(1);
			expect(wrapper.props().param.range).to.deep.equal([-3, -3]);

			wrapper.find('Input[step="1"]').at(1).prop('onChange')( { target: { value: -1 }});
			wrapper.update();
			expect(updateParameter.callCount).to.equal(2);
			expect(wrapper.props().param.range).to.deep.equal([-3, -1]);
		});
213
	});
214
});