我試圖用expect +酶+ sinon來測試反應組分。 我在userToEdit_array
有不同的項目,並且想要檢查測試是否通過了這些項目。如何用酶測試不同道具的React組件
這裏是我的代碼:
import React from 'react';
import { shallow, mount } from 'enzyme';
import expect from 'expect';
import sinon from 'sinon';
import { UserProfileProfile } from '../UserProfile/UserProfileProfile.component.jsx';
describe('Testing new user addition form in <UserProfileProfile> component',() => {
var userToEdit_array = [
{
profile : {
name : "Long Lohn",
email : "[email protected]",
phone : "+1-000-545-11-22",
description : ""
}
},
{
profile : {
name : "Long Lohnson",
email : "[email protected]",
phone : "8900555-45-11",
description : ""
}
},
{
profile : {
name : "Giiggi Aarroron",
email : "[email protected] 234 234 ",
phone : "8-900555-45-11-234234234234",
description : ""
}
}
];
var spy = sinon.spy();
var props = {
userToEdit : userToEdit_array[0],
users: [],
action_createNewUserDB :() => spy()
}
var wrapper = mount(<UserProfileProfile {...props} />);
it('should check if SAVE button exist',() => {
expect(wrapper.find("button.UserProfile__profile__form__button").length).toEqual(1);
});
var btn = wrapper.find("button.UserProfile__profile__form__button");
function checkNow() {
it('ckecks if action_createNewUserDB is called with different parameters()',() => {
btn.simulate('click');
expect(spy.called).toEqual(true);
});
}
checkNow();
for (let i=1; i < userToEdit_array.length; i++) {
console.log("now testing:",userToEdit_array[i]);
wrapper.setProps({ userToEdit : userToEdit_array[i] }, checkNow);
}
});
我用的酶的setProps
方法重新渲染我的新道具(DOC:http://airbnb.io/enzyme/docs/api/ReactWrapper/setProps.html)組件,但似乎有一個與asyncronous代碼的問題,因爲它是試圖通過陣列中最後一個道具的測試,並且測試沒有通過。
如果我刪除/評論最後一塊:
for (let i=1; i < userToEdit_array.length; i++) {
console.log("now testing:",userToEdit_array[i]);
wrapper.setProps({ userToEdit : userToEdit_array[i] }, checkNow);
}
測試通過。
此外,如果userToEdit_array
中的最後一項有效,則所有測試都通過,但如果userToEdit_array
中的最後一項無效,則所有測試均失敗。