add SearchForm test

jh-changes
Shin'ya Ueoka 7 years ago
parent d195890eb9
commit 0967304ebe
  1. 18
      src/settings/components/form/search-form.jsx
  2. 104
      test/settings/components/form/search-engine-form.test.jsx

@ -10,10 +10,9 @@ class SearchForm extends Component {
if (!value) { if (!value) {
value = { default: '', engines: []}; value = { default: '', engines: []};
} }
let { if (!value.engines) {
default: defaultEngine, value.engines = [];
engines }
} = value;
return <div className='form-search-form'> return <div className='form-search-form'>
<div className='form-search-form-header'> <div className='form-search-form-header'>
@ -22,7 +21,7 @@ class SearchForm extends Component {
<div className='column-option'>Default</div> <div className='column-option'>Default</div>
</div> </div>
{ {
engines.map((engine, index) => { value.engines.map((engine, index) => {
return <div key={index} className='form-search-form-row'> return <div key={index} className='form-search-form-row'>
<input data-index={index} type='text' name='name' <input data-index={index} type='text' name='name'
className='column-name' value={engine[0]} className='column-name' value={engine[0]}
@ -33,7 +32,7 @@ class SearchForm extends Component {
onChange={this.bindValue.bind(this)} /> onChange={this.bindValue.bind(this)} />
<div className='column-option'> <div className='column-option'>
<input data-index={index} type='radio' name='default' <input data-index={index} type='radio' name='default'
checked={defaultEngine === engine[0]} checked={value.default === engine[0]}
onChange={this.bindValue.bind(this)} /> onChange={this.bindValue.bind(this)} />
<DeleteButton data-index={index} name='delete' <DeleteButton data-index={index} name='delete'
onClick={this.bindValue.bind(this)} /> onClick={this.bindValue.bind(this)} />
@ -50,12 +49,17 @@ class SearchForm extends Component {
return; return;
} }
let value = this.props.value;
let name = e.target.name; let name = e.target.name;
let index = e.target.getAttribute('data-index'); let index = e.target.getAttribute('data-index');
let next = Object.assign({}, this.props.value); let next = Object.assign({}, {
default: value.default,
engines: value.engines ? value.engines.slice() : [],
});
if (name === 'name') { if (name === 'name') {
next.engines[index][0] = e.target.value; next.engines[index][0] = e.target.value;
next.default = this.props.value.engines[index][0];
} else if (name === 'url') { } else if (name === 'url') {
next.engines[index][1] = e.target.value; next.engines[index][1] = e.target.value;
} else if (name === 'default') { } else if (name === 'default') {

@ -0,0 +1,104 @@
import { expect } from 'chai';
import { h, render } from 'preact';
import SearchForm from 'settings/components/form/search-form'
describe("settings/form/SearchForm", () => {
beforeEach(() => {
document.body.innerHTML = '';
});
describe('render', () => {
it('renders SearchForm', () => {
render(<SearchForm value={{
default: 'google',
engines: [['google', 'google.com'], ['yahoo', 'yahoo.com']],
}} />, document.body);
let names = document.querySelectorAll('input[name=name]');
expect(names).to.have.lengthOf(2);
expect(names[0].value).to.equal('google');
expect(names[1].value).to.equal('yahoo');
let urls = document.querySelectorAll('input[name=url]');
expect(urls).to.have.lengthOf(2);
expect(urls[0].value).to.equal('google.com');
expect(urls[1].value).to.equal('yahoo.com');
});
it('renders blank value', () => {
render(<SearchForm />, document.body);
let names = document.querySelectorAll('input[name=name]');
let urls = document.querySelectorAll('input[name=url]');
expect(names).to.have.lengthOf(0);
expect(urls).to.have.lengthOf(0);
});
it('renders blank engines', () => {
render(<SearchForm value={{ default: 'google' }} />, document.body);
let names = document.querySelectorAll('input[name=name]');
let urls = document.querySelectorAll('input[name=url]');
expect(names).to.have.lengthOf(0);
expect(urls).to.have.lengthOf(0);
});
});
describe('onChange event', () => {
it('invokes onChange event on edit', (done) => {
render(<SearchForm
value={{
default: 'google',
engines: [['google', 'google.com'], ['yahoo', 'yahoo.com']]
}}
onChange={value => {
expect(value.default).to.equal('louvre');
expect(value.engines).to.have.lengthOf(2)
.and.have.deep.members([['louvre', 'google.com'], ['yahoo', 'yahoo.com']])
done();
}} />, document.body);
let radio = document.querySelectorAll('input[type=radio]');
radio.checked = true;
let name = document.querySelector('input[name=name]');
name.value = 'louvre';
name.dispatchEvent(new Event('change'))
});
it('invokes onChange event on delete', (done) => {
render(<SearchForm value={{
default: 'yahoo',
engines: [['louvre', 'google.com'], ['yahoo', 'yahoo.com']]
}}
onChange={value => {
expect(value.default).to.equal('yahoo');
expect(value.engines).to.have.lengthOf(1)
.and.have.deep.members([['yahoo', 'yahoo.com']])
done();
}} />, document.body);
let button = document.querySelector('input[type=button]');
button.click();
});
it('invokes onChange event on add', (done) => {
render(<SearchForm value={{
default: 'yahoo',
engines: [['google', 'google.com']]
}}
onChange={value => {
expect(value.default).to.equal('yahoo');
expect(value.engines).to.have.lengthOf(2)
.and.have.deep.members([['google', 'google.com'], ['', '']])
done();
}} />, document.body);
let button = document.querySelector('input[type=button].ui-add-button');
button.click();
});
});
});