我有以下組件:export default function Button({ className, children, ...otherProps }) { return ( <button className={'button} {...otherProps}> {children} </button> );}在父組件中,我在里面?zhèn)鬟f了這樣的道具和標(biāo)簽:<Button className={test-button} onClick={this.removeItems} > <i className="fa fa-trash-alt" /> Remove all items</Button>我無(wú)法理解如何正確地對(duì)這些組件進(jìn)行單元測(cè)試。例如,我想測(cè)試onClick單擊組件時(shí)調(diào)用的函數(shù)。我寫(xiě)了這樣一個(gè)測(cè)試:const buttonFunc = jest.fn(); const props = { children: { className: "test", onClick: buttonFunc, }};let wrapper;beforeEach(() => { wrapper = shallow(<Button {...props} />);});test('click on switch button', () => { wrapper.simulate('click'); expect(buttonFunc).toHaveBeenCalledTimes(1); console.log(wrapper.debug())});但我有一個(gè)錯(cuò)誤預(yù)期模擬函數(shù)已被調(diào)用一次,但被調(diào)用了零次。
如何測(cè)試在內(nèi)部呈現(xiàn)子元素的組件?
富國(guó)滬深
2021-06-09 09:04:39