观察您的组件呈现的内容,似乎需要测试三件事:
- 它应该将 2 个按钮呈现为孩子
- 这些按钮何时启用或禁用
- 这些组件的类名
主要测试用例是“启用”用例。是否测试 classNames 取决于您,通常不需要测试内联样式或 css 类。
如果userCompany
和/或selectedTechnology
没有给出,两个按钮似乎都被禁用,如果它们都给出,两个按钮都被启用。
根据您提供给组件的状态和props,您可以观察 html 按钮的“禁用”props。
const wrapper = shallow(<SelectCriteraNewJob userCompany='Some Company' />);
it('SelectCriteraNewJob should render two buttons as children', () => {
expect(wrapper.childAt(0).type()).toEqual('button');
// expect(wrapper.childAt(1).type()).toEqual('span'); if you want to, not necessary to test
expect(wrapper.childAt(2).type()).toEqual('button');
});
describe('If technology and company are not empty, both buttons are enabled. Otherwise disabled.',() => {
expect(wrapper.find('button#addBtn')).toBeTruthy();
expect(wrapper.find('button#uploadBtn')).toBeTruthy();
test('Company and technology given', () => {
wrapper.setState({selectedTechnology: 'React'});
const addBtn = wrapper.find('button#addBtn').prop('disabled');
const uploadBtn = wrapper.find('button#uploadBtn').prop('disabled');
expect(addBtn).toBeFalsy();
expect(uploadBtn).toBeFalsy();
});
test('Only technology given', () => {
wrapper.setProps({userCompany:''});
const addBtn = wrapper.find('button#addBtn').prop('disabled');
const uploadBtn = wrapper.find('button#uploadBtn').prop('disabled');
expect(addBtn).toBeTruthy();
expect(uploadBtn).toBeTruthy();
});
test('Only company given', () => {
wrapper.setState({selectedTechnology:''});
wrapper.setProps({userCompany:'Some Company'});
const addBtn = wrapper.find('button#addBtn').prop('disabled');
const uploadBtn = wrapper.find('button#uploadBtn').prop('disabled');
expect(addBtn).toBeTruthy();
expect(uploadBtn).toBeTruthy();
});
test('Neither are given', () => {
wrapper.setState({selectedTechnology:''});
wrapper.setProps({userCompany:''});
const addBtn = wrapper.find('button#addBtn').prop('disabled');
const uploadBtn = wrapper.find('button#uploadBtn').prop('disabled');
expect(addBtn).toBeTruthy();
expect(uploadBtn).toBeTruthy();
expect(wrapper.find('button.jd-button')).toHaveLength(2); //optional, test css class name
});
});