Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With event does not override currentTarget #69

Open
salomvary opened this issue Oct 1, 2020 · 4 comments
Open

With event does not override currentTarget #69

salomvary opened this issue Oct 1, 2020 · 4 comments

Comments

@salomvary
Copy link

I expected this to pass:

        it('calls events with currentTarget', () => {

            const EventTargetComponent = () => {
                const [value, setValue] = useState();
                const handleChange = (event) => setValue(event.currentTarget.value);
                return <div><input onChange={handleChange} value="before-change"/><span>{value}</span></div>;
            }

            expect(<EventTargetComponent/>, 'when deeply rendered', 'with event',
                  'change', { currentTarget: {value: 'test'} }, 'on', <input/>, 'to have rendered',
                <div>
                    <input/>
                    <span>test</span>
                </div>);
        });

But it failed with this:

  2) unexpected-react (deep rendering) with events calls events with currentTarget:
     UnexpectedError: 
expected <EventTargetComponent />
when deeply rendered with event 'change', { currentTarget: { value: 'test' } } on <input /> to have rendered <div><input /><span>test</span></div>

<EventTargetComponent>
  <div>
    <input onChange={function handleChange(event) { /* ... */ }} value="before-change" />
    <span>
      before-change // before-change
                    // test
    </span>
  </div>

Does this maybe mean the deep renderer does not allow triggering events with arbitrary properties? The documentation suggests that it should be possible..

@bruderstein
Copy link
Owner

The documentation is actually this link: http://bruderstein.github.io/unexpected-react/assertions/RenderedReactElement/with-event/ - this also should be made clearer, not every property can be overridden. It uses react-dom/test-utils Simulate so not all properties are overridable - currentTarget being one of them as that is created as part of the event.

@bmhardy
Copy link

bmhardy commented Oct 5, 2020 via email

@bruderstein
Copy link
Owner

@bmhardy No (I had to Google snabbdom), but it's actually straightforward, as all the real work is in unexpected-htmllike, and then there is just adapters for react elements, DOM elements and things like snapshot elements. See unexpected-preact for example of implementing it for another virtual DOM implementation.

(Next time, it would be better to open a new issue rather than commenting on an unrelated issue)

@salomvary
Copy link
Author

The documentation is actually this link: http://bruderstein.github.io/unexpected-react/assertions/RenderedReactElement/with-event/ - this also should be made clearer, not every property can be overridden. It uses react-dom/test-utils Simulate so not all properties are overridable - currentTarget being one of them as that is created as part of the event.

I see. Now I wonder what the unexpected-react equivalent of this test-utils example is:

// <input ref={(node) => this.textInput = node} />
const node = this.textInput;
node.value = 'giraffe';
ReactTestUtils.Simulate.change(node);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants