Commit 5b470494 authored by Flavio TARSETTI's avatar Flavio TARSETTI
Browse files

[plotter] added unit tests for PlotterEditor component

parent 27485df4
Pipeline #24402 passed with stages
in 30 minutes and 18 seconds
// @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.only('<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();
console.log(wrapper.props().param);
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(updateParameter.args[0]).to.deep.equal([
name,
{
type: 'bool',
default: 'true',
description: '',
}
]);
});
});
//// @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.only('<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();
// console.log(wrapper.props().param);
// 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(updateParameter.args[0]).to.deep.equal([
// name,
// {
// type: 'bool',
// default: '',
// description: '',
// }
// ]);
// });
//});
......@@ -206,11 +206,11 @@ export class PlotterEditor extends React.Component<Props, State> {
}
</Input>
</FormGroup>
<FormGroup>
<FormGroup id='usedLibs'>
<h5>Libraries</h5>
{ this.renderLibraries() }
</FormGroup>
<FormGroup>
<FormGroup id='paramsForm'>
<h5>Parameters</h5>
{
(Object.entries(this.props.data.contents.parameters): [string, any][]).map(([name, param], i, params) => (
......
// @flow
import React from 'react';
import { expect } from 'chai';
import { mount } from 'enzyme';
import sinon from 'sinon';
import { spies } from '@test';
import { PlotterEditor as C } from '.';
import { getValidPlotterObj as getValidObj } from '@helpers/beat';
import testPlotters from '@test/test_plotters.json';
import testDfs from '@test/test_dfs.json';
import testLibs from '@test/test_libs.json';
const libs = [];
describe('<PlotterEditor />', () => {
let wrapper;
afterEach(() => {
if(wrapper && wrapper.unmount)
wrapper.unmount();
});
describe('accepts', () => {
const plots = [
{
name: 'test/myplot/1',
contents: {
description: 'A basic plotter as a sanity test for the PlotterEditor',
language: 'python',
parameters: {
'parameter_1': {
type: 'string',
default: 'default',
description: 'description',
}
},
dataformat: "plot/bar/1",
uses: {
'library': 'plot/baselib/1',
}
}
}
].concat(testPlotters.map(a => getValidObj(a)));
plots.forEach(function(plot){
const saveFunc = () => {};
const updateFunc = () => {};
const dfs = testDfs.filter(d => d.name.startsWith('plot')).map(testDfs => testDfs.name);
it(`${ plot.name }`, () => {
wrapper = mount(
<C
data={getValidObj(plot)}
plotters={plots}
dataformats={plot.contents.dataformat}
plotDfNames={dfs}
libraries={libs}
saveFunc={saveFunc}
updateFunc={updateFunc}
/>
);
expect(wrapper).to.have.props(
['data', 'plotters', 'dataformats', 'libraries', 'saveFunc', 'updateFunc']
)
});
});
});
describe('creates', () => {
it('test/createplot/1', () => {
const saveFunc = sinon.spy();
const _updateFunc = (obj) => {
wrapper.setProps && wrapper.setProps({ data: obj });
};
const updateFunc = sinon.spy(_updateFunc);
const plotName = 'test/createplot/1';
const dfs = testDfs.filter(d => d.name.startsWith('plot')).map(testDfs => testDfs.name);
wrapper = mount(
<C
data={getValidObj({name: plotName, contents: {}})}
plotters={[]}
dataformats={testDfs}
plotDfNames={dfs}
libraries={testLibs}
saveFunc={saveFunc}
updateFunc={updateFunc}
/>
);
expect(wrapper).to.have.props(
['data', 'plotters', 'dataformats', 'plotDfNames', 'libraries', 'saveFunc', 'updateFunc']
);
expect(wrapper.props().data).to.have.property('name', plotName);
expect(wrapper.props().data.contents).to.have.property('dataformat', '');
expect(wrapper.props().data.contents).to.have.property('dataformat').with.lengthOf(0);
expect(wrapper.props().data.contents).to.have.property('language').with.lengthOf(6);
expect(wrapper.props().data.contents).to.have.property('language', 'python');
wrapper.find('#plDf select').simulate('change', { target: { value: 'user/anotherdataformat/1'}});
expect(updateFunc.callCount).to.equal(1);
expect(wrapper.props().data.contents).to.have.property('dataformat', 'user/anotherdataformat/1');
expect(wrapper.props().data.contents).to.have.property('language', 'python');
expect(wrapper.props().data.contents).to.have.property('description').with.lengthOf(0);
expect(wrapper.props().data.contents).to.have.property('language').with.lengthOf(6);
expect(wrapper.props().data.contents).to.have.property('dataformat').with.lengthOf(24);
expect(wrapper.props().data.contents).to.have.property('parameters').that.deep.equals({});
expect(wrapper.props().data.contents).to.have.property('uses').that.deep.equals({});
expect(wrapper.props().data.contents).to.deep.equal({
'description': '',
'uses': {},
'parameters': {},
'language': 'python',
'dataformat': 'user/anotherdataformat/1'
});
});
it('test/plot_add_lib/1', () => {
const saveFunc = sinon.spy();
const _updateFunc = (obj) => {
wrapper.setProps && wrapper.setProps({ data: obj });
};
const updateFunc = sinon.spy(_updateFunc);
const plotName = 'test/plot_other/1';
const dfs = testDfs.filter(d => d.name.startsWith('plot')).map(testDfs => testDfs.name);
wrapper = mount(
<C
data={getValidObj({name: plotName, contents: {}})}
plotters={[]}
dataformats={testDfs}
plotDfNames={dfs}
libraries={testLibs}
saveFunc={saveFunc}
updateFunc={updateFunc}
/>
);
expect(wrapper).to.have.props(
['data', 'plotters', 'dataformats', 'plotDfNames', 'libraries', 'saveFunc', 'updateFunc']
);
expect(wrapper.props().data).to.have.property('name', plotName);
expect(wrapper.props().data.contents).to.have.property('dataformat', '');
expect(wrapper.props().data.contents).to.have.property('dataformat').with.lengthOf(0);
expect(wrapper.props().data.contents).to.have.property('language').with.lengthOf(6);
expect(wrapper.props().data.contents).to.have.property('language', 'python');
expect(wrapper.props().data.contents).to.have.property('parameters').that.deep.equals({});
expect(wrapper.props().data.contents).to.have.property('uses').that.deep.equals({});
wrapper.find('button#newLibraryBtn').simulate('click');
wrapper.find('#usedLibs CacheInput[type="text"]').prop('onChange')({ target: { value: 'lib'}});
expect(updateFunc.callCount).to.equal(2);
wrapper.find('#usedLibs select').simulate('change', { target: { value: 'user/anotherlib/1'}});
expect(updateFunc.callCount).to.equal(3);
expect(wrapper.props().data.contents).to.deep.equal({
'description': '',
'uses': {
lib: 'user/anotherlib/1'
},
'language': 'python',
'parameters': {},
'dataformat': ''
});
});
it('test/plot_add_param/1', () => {
const saveFunc = sinon.spy();
const _updateFunc = (obj) => {
wrapper.setProps && wrapper.setProps({ data: obj });
};
const updateFunc = sinon.spy(_updateFunc);
const plotName = 'test/plot_param/1';
const dfs = testDfs.filter(d => d.name.startsWith('plot')).map(testDfs => testDfs.name);
wrapper = mount(
<C
data={getValidObj({name: plotName, contents: {}})}
plotters={[]}
dataformats={testDfs}
plotDfNames={dfs}
libraries={testLibs}
saveFunc={saveFunc}
updateFunc={updateFunc}
/>
);
expect(wrapper).to.have.props(
['data', 'plotters', 'dataformats', 'plotDfNames', 'libraries', 'saveFunc', 'updateFunc']
);
expect(wrapper.props().data).to.have.property('name', plotName);
expect(wrapper.props().data.contents).to.have.property('dataformat', '');
expect(wrapper.props().data.contents).to.have.property('dataformat').with.lengthOf(0);
expect(wrapper.props().data.contents).to.have.property('language').with.lengthOf(6);
expect(wrapper.props().data.contents).to.have.property('language', 'python');
expect(wrapper.props().data.contents).to.have.property('parameters').that.deep.equals({});
expect(wrapper.props().data.contents).to.have.property('uses').that.deep.equals({});
wrapper.find('button#newParameterBtn').simulate('click');
expect(updateFunc.callCount).to.equal(1);
wrapper.find('CacheInput[placeholder="Parameter name..."]').prop('onChange')( { target: { value: 'offset' }});
expect(updateFunc.callCount).to.equal(2);
wrapper.find('#paramsForm select').prop('onChange')( { target: { value: 'int32' }});
expect(updateFunc.callCount).to.equal(3);
wrapper.find('input[placeholder="Default"]').simulate('change', { target: { value: '1' }});
expect(updateFunc.callCount).to.equal(4);
expect(wrapper.props().data.contents.parameters).to.have.deep.property('offset', {
'default': '1',
'type': 'int32',
'description': '',
});
expect(wrapper.props().data.contents).to.deep.equal({
'description': '',
'uses': {},
'language': 'python',
'parameters': {
'offset': {
'default': '1',
'type': 'int32',
description: '',
}
},
'dataformat': ''
});
});
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment