add input test
This commit is contained in:
parent
256e7372d4
commit
e90cf895ad
3 changed files with 86 additions and 4 deletions
|
@ -7,11 +7,13 @@ module.exports = function (config) {
|
||||||
frameworks: ['mocha'],
|
frameworks: ['mocha'],
|
||||||
files: [
|
files: [
|
||||||
'test/**/*.test.js',
|
'test/**/*.test.js',
|
||||||
|
'test/**/*.test.jsx',
|
||||||
'test/**/*.html'
|
'test/**/*.html'
|
||||||
],
|
],
|
||||||
|
|
||||||
preprocessors: {
|
preprocessors: {
|
||||||
'test/**/*.test.js': [ 'webpack' ],
|
'test/**/*.test.js': [ 'webpack' ],
|
||||||
|
'test/**/*.test.jsx': [ 'webpack' ],
|
||||||
'test/**/*.html': ['html2js']
|
'test/**/*.html': ['html2js']
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,7 @@ class Input extends Component {
|
||||||
renderText(props) {
|
renderText(props) {
|
||||||
let inputClassName = props.error ? 'input-error' : '';
|
let inputClassName = props.error ? 'input-error' : '';
|
||||||
return <div className='settings-ui-input'>
|
return <div className='settings-ui-input'>
|
||||||
<label
|
<label htmlFor={props.id}>{ props.label }</label>
|
||||||
type='text'
|
|
||||||
htmlFor={props.id}
|
|
||||||
>{ props.label }</label>
|
|
||||||
<input type='text' className={inputClassName} {...props} />
|
<input type='text' className={inputClassName} {...props} />
|
||||||
</div>;
|
</div>;
|
||||||
}
|
}
|
||||||
|
|
83
test/settings/components/ui/input.test.jsx
Normal file
83
test/settings/components/ui/input.test.jsx
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
import { expect } from 'chai';
|
||||||
|
import { h, render } from 'preact';
|
||||||
|
import Input from 'settings/components/ui/input'
|
||||||
|
|
||||||
|
describe("settings/ui/Input", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
document.body.innerHTML = '';
|
||||||
|
});
|
||||||
|
|
||||||
|
context("type=text", () => {
|
||||||
|
it('renders text input', () => {
|
||||||
|
render(<Input type='text' name='myname' label='myfield' value='myvalue'/>, document.body)
|
||||||
|
|
||||||
|
let label = document.querySelector('label');
|
||||||
|
let input = document.querySelector('input');
|
||||||
|
expect(label.textContent).to.contain('myfield');
|
||||||
|
expect(input.type).to.contain('text');
|
||||||
|
expect(input.name).to.contain('myname');
|
||||||
|
expect(input.value).to.contain('myvalue');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('invoke onChange', (done) => {
|
||||||
|
render(<Input type='text' name='myname' label='myfield' value='myvalue' onChange={(e) => {
|
||||||
|
expect(e.target.value).to.equal('newvalue');
|
||||||
|
done();
|
||||||
|
}}/>, document.body);
|
||||||
|
|
||||||
|
let input = document.querySelector('input');
|
||||||
|
input.value = 'newvalue';
|
||||||
|
input.dispatchEvent(new Event('change'))
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context("type=radio", () => {
|
||||||
|
it('renders radio button', () => {
|
||||||
|
render(<Input type='radio' name='myname' label='myfield' value='myvalue'/>, document.body)
|
||||||
|
|
||||||
|
let label = document.querySelector('label');
|
||||||
|
let input = document.querySelector('input');
|
||||||
|
expect(label.textContent).to.contain('myfield');
|
||||||
|
expect(input.type).to.contain('radio');
|
||||||
|
expect(input.name).to.contain('myname');
|
||||||
|
expect(input.value).to.contain('myvalue');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('invoke onChange', (done) => {
|
||||||
|
render(<Input type='text' name='radio' label='myfield' value='myvalue' onChange={(e) => {
|
||||||
|
expect(e.target.checked).to.be.true;
|
||||||
|
done();
|
||||||
|
}}/>, document.body);
|
||||||
|
|
||||||
|
let input = document.querySelector('input');
|
||||||
|
input.checked = true;
|
||||||
|
input.dispatchEvent(new Event('change'))
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context("type=textarea", () => {
|
||||||
|
it('renders textarea button', () => {
|
||||||
|
render(<Input type='textarea' name='myname' label='myfield' value='myvalue' error='myerror' />, document.body)
|
||||||
|
|
||||||
|
let label = document.querySelector('label');
|
||||||
|
let textarea = document.querySelector('textarea');
|
||||||
|
let error = document.querySelector('.settings-ui-input-error');
|
||||||
|
expect(label.textContent).to.contain('myfield');
|
||||||
|
expect(textarea.nodeName).to.contain('TEXTAREA');
|
||||||
|
expect(textarea.name).to.contain('myname');
|
||||||
|
expect(textarea.value).to.contain('myvalue');
|
||||||
|
expect(error.textContent).to.contain('myerror');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('invoke onChange', (done) => {
|
||||||
|
render(<Input type='textarea' name='myname' label='myfield' value='myvalue' onChange={(e) => {
|
||||||
|
expect(e.target.value).to.equal('newvalue');
|
||||||
|
done();
|
||||||
|
}}/>, document.body);
|
||||||
|
|
||||||
|
let input = document.querySelector('textarea');
|
||||||
|
input.value = 'newvalue'
|
||||||
|
input.dispatchEvent(new Event('change'))
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Reference in a new issue