Closed painedpineapple closed 6 years ago
simulate
doesn't faithfully simulate anything; I'd recommend avoiding it. Use .prop('onClick')()
instead.
Separately, in enzyme 3, you have to re-find from the root wrapper any time there's an update - so you can't reuse dropdownWrapper
, and in fact you also have to store component()
so you can find everything from there.
Thanks @ljharb. I've updated my test with your suggestions, but i'm still getting the same outputs at both console.logs, as well as the test fails because it says the 'ul' has a length of 0, even though it can be seen in the log after the click.
Updated test:
it('clicking <button> always toggles the <ul>', () => {
const node = component()
console.log(node.find('nav').find('span').html())
node.find('nav').find('button').prop('onClick')()
console.log(node.find('nav').find('span').html())
expect(node.find('nav').find('span').find('ul').length).toBeGreaterThan(0)
})
If you add node.update()
after the onClick line? (If that doesn’t work, can you provide the code for the component
function?)
@logancalldev did you figure this out?
Thanks for following up. The component's needs have actually changed, so this has become irrelevant. Thank you for your help!
I just found how to do it. You have to handle the click and not use the same variable to check the component prop
const wrapper = mountWithTheme(
<AccordionContainer>
<Accordion title="Accordion 2" id="2" type="medium">
<div>Accordion content 2</div>
</Accordion>
</AccordionContainer>
);
//First click
wrapper.find('.accordion-button').at(0).simulate('click');
const button = wrapper.find('.accordion-button').at(0);
const container = wrapper.find('.accordion-content').at(0);
expect(button.prop('aria-expanded')).toBe(false);
expect(container.prop('aria-expanded')).toBe(true);
//Second click
wrapper.find('.accordion-button').at(0).simulate('click');
const buttonRefreshed = wrapper.find('.accordion-button').at(0);
const containerfreshed = wrapper.find('.accordion-content').at(0);
expect(buttonRefreshed.prop('aria-expanded')).toBe(false);
expect(containerfreshed.prop('aria-expanded')).toBe(false);
When I was using the same variable to simulate the click, to validate it was all the same. Hope that helps other peeps that find this.
I have a button that, when clicked, triggers a
<ul>
. The actual component is working, but the test is not.Components
Jest Test, with Enzyme
Current behavior
Error –
However, I know the
<ul>
is there, because when i console.log the.html()
of the wrapper, i see it there.Console.log before the simulated click
Console.log before the simulated click
Expected behavior
For
.find('ul')
to be found/have a length greater than 0Your environment
API
Version
Adapter