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

Refactor SyntheticClipboardEvent tests to only use the public API #11365

Merged

Conversation

smaniotto
Copy link
Contributor

This is related to #11299

I've reimplemented the previous tests using only the public API, but I'm uncertain about their coverage. It doesn't check things like setData and getData behavior on the clipboardData (DataTransfer) object, for instance. We would actually rely on implementation mocks to test these things anyway, since there's no implementation of Range on jsdom and DataTransfer doesn't have a constructor.

event.clipboardData = data;
}

element.dispatchEvent(event);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaearon Does it make any huge difference on our tests using the Event constructor rather than the old-fashioned initEvent way when we want to create events?

Actually, how is Fiber dealing with these two approaches today?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if there's any difference. React just subscribes with addEventListener. If that works with both approaches in jsdom, let's use whichever is simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first attempt, but I keep getting this error:
TypeError: Argument to dispatchEvent must be an Event

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SadPandaBear Not sure if it's possible to use the Event constructor in this case, since I have to mock the clipboardData. Any ideas?

Copy link
Contributor

@SadPandaBear SadPandaBear Nov 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking on doing something like:

var event = new ClipboardEvent(type, { bubbles: true });
if(data) event.clipboardData = data;
element.dispatchEvent(event);

But unfortunately ClipboardEvent is not supported. 😢

React = require('react');
ReactDOM = require('react-dom');

document = jsdom('');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this? Don't we already have a document global?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reset changes done on document.body, replaced it by document.body.innerHTML = ''.

/>,
container,
);
document.body.appendChild(div);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add the container to body instead? This detaches the div from the original container.

React = require('react');
ReactDOM = require('react-dom');

document.body.innerHTML = '';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove container from body at the end of each test instead? Or, better, declare variable once, and remove/add it before and after each test.

});
});
});
});

describe('EventInterface', () => {
it('normalizes properties from the Event interface', () => {
var target = document.createElement('div');
var syntheticEvent = createEvent({srcElement: target});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this test specifically verifies that the synthetic event still determines the target even if only srcElement was set. Your test doesn't verify this.

@gaearon
Copy link
Collaborator

gaearon commented Nov 10, 2017

I removed some abstraction from the test. This allowed me to actually test the IE8 code branch (which the original test did). Hope this helps!

@smaniotto
Copy link
Contributor Author

Sure, it makes sense. Thanks!

Ethan-Arrowood pushed a commit to Ethan-Arrowood/react that referenced this pull request Dec 8, 2017
…cebook#11365)

* Refactor SyntheticClipboardEvent tests to only use the public API

* Replace local document creation by document body reset on each test case execution

* Set up and tear down container separately

* Tweak test assertion logic for clarity

* Remove simulate abstraction and create events directly

* Ensure the test covers IE8 behavior

* Verify that persistence works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants