Skip to content

Commit

Permalink
tests refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Przodała committed Jul 11, 2017
1 parent 3b9553a commit 9dc1349
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 74 deletions.
4 changes: 2 additions & 2 deletions Bootstrap.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/components/ListField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ListField extends React.Component {
value: item
}));
}
return [{ id: ListField.generateItemId() }];
return [];
}

setModel(name, value, callback) {
Expand Down
65 changes: 16 additions & 49 deletions tests/ListField.test.jsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
import React from 'react';
import { mount } from 'enzyme';
import Schema from 'form-schema-validation';
import {
Form,
TextField,
SubmitField,
ObjectField,
ListField
} from '../src/components';
import { addressFormSchema, languagesFormSchema } from './data/schemas';


describe('ListField', () => {
it('should add and remove field',() => {
const submitMethod = (data) => {
expect(data.languages.length).toBe(3);
};

const formSchema = new Schema({
languages: {
type: [String]
}
});

const addButton = {
className: 'addButtonClass'
};

const removeButton = {
className: 'removeButtonClass'
};
const submitMethod = (data) => { expect(data.languages.length).toBe(3); };
const addButton = { className: 'addButtonClass' };
const removeButton = { className: 'removeButtonClass'};

const wrapper = mount(
<Form
onSubmit={submitMethod}
schema={formSchema}
schema={languagesFormSchema}
>
<ListField name="languages" label="Languages" addButton={addButton} removeButton={removeButton}>
<TextField />
Expand Down Expand Up @@ -61,38 +47,14 @@ describe('ListField', () => {
expect(data.address[1].postCode).toBe('testPostCode3');
};

const addressSchema = new Schema({
city: {
type: String
},
street: {
type: String
},
postCode: {
type: String
}
});

const formSchema = new Schema({
address: {
type: [addressSchema]
}
});

const model={};

const addButton = {
className: 'addButtonClass'
};

const removeButton = {
className: 'removeButtonClass'
};
const addButton = { className: 'addButtonClass' };
const removeButton = { className: 'removeButtonClass' };

const wrapper = mount(
<Form
onSubmit={submitMethod}
schema={formSchema}
schema={addressFormSchema}
model={model}
>
<ListField name="address" label="Address" addButton={addButton} removeButton={removeButton}>
Expand All @@ -106,26 +68,31 @@ describe('ListField', () => {
</Form>
);
const list = wrapper.find(ListField);
const object = list.find(ObjectField).first();
const fields = object.find(TextField);


const submit = wrapper.find(SubmitField);

const setDataToObjectField = (fields, number = '') => {
fields.find('[name="city"]').simulate('change', {target: {value: `testCity${number}`}});
fields.find('[name="street"]').simulate('change', {target: {value: `testStreet${number}`}});
fields.find('[name="postCode"]').simulate('change', {target: {value: `testPostCode${number}`}});
};
setDataToObjectField(fields);

list.find('.addButtonClass').first().simulate('click');
list.find('.addButtonClass').first().simulate('click');
list.find('.addButtonClass').first().simulate('click');
expect(list.find(ObjectField).length).toBe(3);

const object = list.find(ObjectField).first();
const fields = object.find(TextField);
setDataToObjectField(fields);
const object2 = list.find(ObjectField).at(1);
const fields2 = object2.find(TextField);
setDataToObjectField(fields2, 2);
const object3 = list.find(ObjectField).at(2);
const fields3 = object3.find(TextField);
setDataToObjectField(fields3, 3);

list.find('.removeButtonClass').at(1).simulate('click');
expect(list.find(ObjectField).length).toBe(2);
submit.find('button').simulate('click');
Expand Down
26 changes: 6 additions & 20 deletions tests/ObjectField.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ObjectField,
ListField
} from '../src/components';
import { addressFormSchema } from './data/schemas';


describe('ObjectField', () => {
Expand Down Expand Up @@ -89,31 +90,13 @@ describe('ObjectField', () => {
expect(data.address[0].street).toBe('testStreet');
expect(data.address[0].postCode).toBe('testPostCode');
};

const addressSchema = new Schema({
city: {
type: String
},
street: {
type: String
},
postCode: {
type: String
}
});

const formSchema = new Schema({
address: {
type: [addressSchema]
}
});

const model={};
const addButton = { className: 'addButtonClass' };

const wrapper = mount(
<Form
onSubmit={submitMethod}
schema={formSchema}
schema={addressFormSchema}
model={model}
>
<ListField name="address" label="Address">
Expand All @@ -127,9 +110,12 @@ describe('ObjectField', () => {
</Form>
);
const list = wrapper.find(ListField);
list.find('span').last().simulate('click');

const object = list.find(ObjectField);
const fields = object.find(TextField);
const submit = wrapper.find(SubmitField);

fields.find('[name="city"]').simulate('change', {target: {value: 'testCity'}});
fields.find('[name="street"]').simulate('change', {target: {value: 'testStreet'}});
fields.find('[name="postCode"]').simulate('change', {target: {value: 'testPostCode'}});
Expand Down
25 changes: 25 additions & 0 deletions tests/data/schemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Schema from 'form-schema-validation';

export const addressSchema = new Schema({
city: {
type: String
},
street: {
type: String
},
postCode: {
type: String
}
});

export const addressFormSchema = new Schema({
address: {
type: [addressSchema]
}
});

export const languagesFormSchema = new Schema({
languages: {
type: [String]
}
});
3 changes: 2 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
]
},
devServer: {
historyApiFallback: true
historyApiFallback: true,
port: 9002
}
};

0 comments on commit 9dc1349

Please sign in to comment.