我有一個搜索欄,我想測試用戶輸入。我使用React渲染視圖,並使用Chai,Mocha和JSDom編寫測試。使用Mocha,Chai和JSDom嘲弄TDD for React的用戶輸入?
下面是簡單的測試,我已經寫到目前爲止
describe('Search',() => {
it('renders a search bar',() => {
const component = renderIntoDocument(
<Search />
);
const search_bar = scryRenderedDOMComponentsWithClass(component, 'input-group');
expect(search_bar.length).to.equal(1);
});
it('invokes a callback when the Add button is clicked',() => {
let search_term;
const clickHandler = (term) => search_term = term;
const component = renderIntoDocument(
<Search onClick={clickHandler}/>
)
const add_button = scryRenderedDOMComponentsWithTag(component, 'button');
Simulate.click(add_button[0]);
expect(search_term).to.equal('');
});
it('accepts user input from the search button and performs a regex check for invalid input',() => {
let search_term;
const clickHandler = (term) => search_term = term;
const component = renderIntoDocument(
<Search onClick={clickHandler} />
);
const add_button = scryRenderedDOMComponentsWithTag(component, 'button');
const search_bar = scryRenderedDOMComponentsWithClass(component, 'input-group');
Simulate.change(search_bar[0], {target: {value: 'FB'}});
expect(search_bar[0].state.value).to.equal('FB');
})
})
1)我想測試擊中添加按鈕之前,用戶輸入的字符。
2)我想測試用戶沒有在搜索詞中輸入任何數字或特殊字符,字符串只需要字母表。
如何模擬這些測試?