Skip to content

Commit

Permalink
Starting tests for FileFieldGroup component
Browse files Browse the repository at this point in the history
  • Loading branch information
New0 committed Aug 3, 2018
1 parent ec039c5 commit d351bc4
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 149 deletions.
15 changes: 10 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,19 @@ class App extends Component {
</div>

<div>
<h2>File field Select</h2>
<h2>File Field</h2>
<FileFieldGroup
id={'file-1'}
label={'File field'}
fieldClassName={'file'}
id={'file-5'}
label={'File Field'}
className={'file'}
onValueChange={(newValue) => {
magicFieldValue = newValue;
} }
} }
isOpen={true}
value={fileFieldValue}
/>
</div>


<div>
<h2>Inputs</h2>
Expand Down Expand Up @@ -334,6 +336,9 @@ class App extends Component {
label: 'Two'
}
]}
onValueChange={(newValue) => { //Added after test response, ask Josh if this was on purpose
selectFieldValue = newValue;
}}
/>

</div>
Expand Down
3 changes: 0 additions & 3 deletions src/components/fields/FieldGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ export const FieldGroup = (props) => {
if( 'magic' === props.type ){
return <MagicFieldGroup {...props} />;
}
if( 'file' === props.type ){
return <FileFieldGroup {...props} />;
}

/**
* Creates the id attribute
Expand Down
40 changes: 15 additions & 25 deletions src/components/fields/file-field/FileFieldGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {RenderGroup} from '../../RenderGroup';
import {Message} from '../messages/Message';
import {FieldGroup} from '../FieldGroup';
import {FileSelect} from './FileSelect';
import {fieldGroupPropTypes} from '../propTypes';

/**
* Encapsulates a complete File field group
Expand All @@ -18,18 +19,12 @@ export class FileFieldGroup extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
currentListType: props.defaultList,
isOpen: props.isOpen,
};
this.onChange = this.onChange.bind(this);
//this.items = this.items.bind(this);
this.onChange = this.onChange.bind(this);
this.onSelect = this.onSelect.bind(this);
this.onInputFocus = this.onInputFocus.bind(this);
//this.renderItem = this.renderItem.bind(this);
this.onInputBlur = this.onInputBlur.bind(this);
this.onChangeListType = this.onChangeListType.bind(this);
//this.listTypeOptions = this.listTypeOptions.bind(this);
}

/**
Expand All @@ -54,7 +49,6 @@ export class FileFieldGroup extends React.PureComponent {
this.setState({isOpen: false});
}


/**
* Handle when the option is chosen
* @param {String|number} value
Expand All @@ -64,17 +58,6 @@ export class FileFieldGroup extends React.PureComponent {
this.setState({isOpen: false});
}

/**
* Update list of tags to show
* @param {String}newType
*/
onChangeListType(newType){
if( ! this.state.isOpen ){
this.setState({isOpen:true});
}
this.setState({currentListType:newType});
}

/**
* Render FileFieldGroup component
* @return {*}
Expand All @@ -89,10 +72,9 @@ export class FileFieldGroup extends React.PureComponent {
)
}
>
<FieldGroup.Label
id={this.props.id}
label={this.props.label}
/>
<label
htmlFor={this.props.id}
>{this.props.label}</label>
{this.props.message.message &&
<Message
message={this.props.message}
Expand All @@ -101,32 +83,40 @@ export class FileFieldGroup extends React.PureComponent {

<FileSelect
id={this.props.id}
//options={this.items()}
onValueChange={this.props.onValueChange}
value={this.props.value}
isRequired={this.props.isRequired}
isOpen={this.state.isOpen}
onBlur={this.onInputBlur}
onFocus={this.onInputFocus}
className={this.props.fieldClassName}
attachToMailer={this.props.attachToMailer}
saveInLibrary={this.props.saveInLibrary}
/>
</div>
);
}
}

/**
* Prop definitions for FileFieldGroup component
*/
FileFieldGroup.propTypes = fieldGroupPropTypes;

/**
* Default property values for FileFieldGroup component
*
* @type {{}}
*/
FileFieldGroup.defaultProps = {
isOpen: false,
message: {
error: false,
message: ''
},
type: 'file'
isOpen: false,
attachToMailer: false,
saveInLibrary: false,
type: 'file',
};

/**
Expand Down
63 changes: 63 additions & 0 deletions src/components/fields/file-field/FileFieldGroup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import renderer from 'react-test-renderer';
import React from 'react';
import {mount, shallow} from 'enzyme';
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import {FileFieldGroup} from './FileFieldGroup';

Enzyme.configure({adapter: new Adapter()});

const genericChangeHandler = () => {
};
describe('FileFieldGroup component', () => {


it('matches snapshot with no message', () => {
const component = renderer.create(
<FileFieldGroup
id={'file-3'}
label={'Upload file'}
fieldClassName={'file'}
onValueChange={genericChangeHandler}
isOpen={false}
/>
);
expect(component.toJSON()).toMatchSnapshot();
});

describe('Inner input', () => {
it('Has inner input', () => {
const component = mount(
<FileFieldGroup
id={'file-1'}
fieldClassName={'file'}
label={'Upload file'}
onValueChange={genericChangeHandler}
isOpen={false}
/>
);
expect(component.find('input')).toHaveLength(1);
});
});

it('Receives updated value', () => {
let value = 'Fotolia_65093732_femme-mains-rites-automne-2.jpg';
const component = mount(
<FileFieldGroup
id={'file-5'}
fieldClassName={'file'}
label={'Upload file'}
onValueChange={(newValue) => {
value = newValue;
}}

value={value}
isOpen={true}
/>
);
component.find('input').simulate('focus');
component.find('input').simulate('change', {target: {value: 'Fotolia_65093732_femme-mains-rites-automne.jpg'}});
expect(value).toEqual('Fotolia_65093732_femme-mains-rites-automne.jpg');
});

});
Loading

0 comments on commit d351bc4

Please sign in to comment.