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

[react-events] DOM event testing library #16433

Merged
merged 1 commit into from
Aug 19, 2019

Conversation

necolas
Copy link
Contributor

@necolas necolas commented Aug 16, 2019

This patch formalizes the mock native events and event sequences used in unit tests.

The createEventTarget function returns an object that can be used to dispatch native event sequences on the target without having to manually do so across all the scenarios we need to account for. Unit tests can be written as if we were only working with PointerEvent, but they will dispatch realistic native event sequences based on the execution environment (e.g., is PointerEvent supported?) and pointer type.

describe.each(environments)('Suite', (hasPointerEvents) => {
  beforeEach(() => {
    // setup
  });

  test.each(pointerTypes)('Test', (pointerType) => {
    const target = createEventTarget(node);
    target.pointerdown({pointerType});
    expect(callback).toBeCalled();
  });
});

Every native event that is dispatched now includes a complete object by default. The properties of the events can be customized. Properties that shouldn't be relied on in responder implementations are excluded from the mock native events to ensure tests will fail. Equivalent properties are normalized across different event types, e.g., 'pointerId' is converted to 'identifier' before a TouchEvent is dispatched.

@sizebot
Copy link

sizebot commented Aug 16, 2019

Details of bundled changes.

Comparing: 9b5985b...312054b

react-events

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-events-press.development.js 0.0% -0.0% 20.49 KB 20.49 KB 4.77 KB 4.77 KB UMD_DEV
react-events-focus.production.min.js 0.0% -0.1% 4.22 KB 4.22 KB 1.4 KB 1.4 KB UMD_PROD
ReactEventsScroll-dev.js +8.0% +5.6% 5.84 KB 6.31 KB 1.56 KB 1.65 KB FB_WWW_DEV
react-events-context-menu.development.js 0.0% -0.1% 2.69 KB 2.69 KB 1002 B 1001 B UMD_DEV
react-events-input.development.js 0.0% -0.1% 4.54 KB 4.54 KB 1.44 KB 1.43 KB UMD_DEV
react-events-swipe.development.js 0.0% -0.1% 6.57 KB 6.57 KB 1.73 KB 1.73 KB UMD_DEV
react-events-context-menu.production.min.js 0.0% -0.3% 1.39 KB 1.39 KB 727 B 725 B UMD_PROD
react-events-input.production.min.js 0.0% -0.1% 1.82 KB 1.82 KB 973 B 972 B UMD_PROD
react-events-swipe.production.min.js 0.0% -0.1% 2.63 KB 2.63 KB 1.19 KB 1.19 KB UMD_PROD
react-events-input.development.js 0.0% -0.1% 4.36 KB 4.36 KB 1.39 KB 1.39 KB NODE_DEV
react-events-swipe.development.js 0.0% -0.1% 6.38 KB 6.38 KB 1.69 KB 1.69 KB NODE_DEV
react-events-swipe.production.min.js 0.0% -0.1% 2.45 KB 2.45 KB 1.14 KB 1.14 KB NODE_PROD
react-events-hover.development.js 0.0% -0.1% 7.08 KB 7.08 KB 1.56 KB 1.56 KB UMD_DEV
react-events-scroll.development.js +8.1% +4.7% 5.9 KB 6.38 KB 1.59 KB 1.66 KB UMD_DEV
react-events-hover.production.min.js 0.0% -0.1% 3.15 KB 3.15 KB 1.14 KB 1.14 KB UMD_PROD
react-events-scroll.production.min.js 🔺+11.4% 🔺+6.1% 2.39 KB 2.66 KB 1.09 KB 1.16 KB UMD_PROD
react-events-scroll.development.js +8.4% +5.5% 5.72 KB 6.2 KB 1.54 KB 1.62 KB NODE_DEV
react-events-hover.production.min.js 0.0% -0.1% 2.97 KB 2.97 KB 1.08 KB 1.08 KB NODE_PROD
react-events-scroll.production.min.js 🔺+12.4% 🔺+8.4% 2.2 KB 2.47 KB 1.02 KB 1.1 KB NODE_PROD
ReactEventsScroll-prod.js 🔺+11.0% 🔺+7.6% 4.48 KB 4.97 KB 1.33 KB 1.43 KB FB_WWW_PROD
react-events-press.development.js 0.0% -0.0% 20.3 KB 20.3 KB 4.72 KB 4.72 KB NODE_DEV
react-events-focus.production.min.js 0.0% -0.1% 4.04 KB 4.04 KB 1.33 KB 1.33 KB NODE_PROD
react-events-press.production.min.js 0.0% -0.0% 6.73 KB 6.73 KB 2.54 KB 2.54 KB NODE_PROD
react-events-drag.development.js 0.0% -0.1% 5.92 KB 5.92 KB 1.66 KB 1.66 KB UMD_DEV
react-events-drag.production.min.js 0.0% -0.2% 2.48 KB 2.48 KB 1.15 KB 1.14 KB UMD_PROD
react-events-keyboard.production.min.js 0.0% -0.1% 1.7 KB 1.7 KB 961 B 960 B NODE_PROD

Generated by 🚫 dangerJS

@necolas necolas force-pushed the react-events/testing-library branch 2 times, most recently from 2e12eea to 37fadd7 Compare August 17, 2019 19:25
@necolas necolas force-pushed the react-events/testing-library branch from 37fadd7 to 0b39231 Compare August 17, 2019 20:54
Copy link
Contributor

@trueadm trueadm left a comment

Choose a reason for hiding this comment

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

This is awesome. It's also makes tests much easier to read and digest. :)

This patch formalizes the mock native events and event sequences used in unit tests.

The `createEventTarget` function returns an object that can be used to dispatch native event sequences on the target without having to manually do so across all the scenarios we need to account for. Unit tests can be written as if we were only working with PointerEvent, but they will dispatch realistic native event sequences based on the execution environment (e.g., is PointerEvent supported?) and pointer type.

```
describe.each(environments)('Suite', (hasPointerEvents) => {
  beforeEach(() => {
    // setup
  });

  test.each(pointerTypes)('Test', (pointerType) => {
    const target = createEventTarget(node);
    target.pointerdown({pointerType});
    expect(callback).toBeCalled();
  });
});
```

Every native event that is dispatched now includes a complete object by default. The properties of the events can be customized. Properties that shouldn't be relied on in responder implementations are excluded from the mock native events to ensure tests will fail. Equivalent properties are normalized across different event types, e.g., 'pointerId' is converted to 'identifier' before a TouchEvent is dispatched.
@necolas necolas force-pushed the react-events/testing-library branch from 0b39231 to 312054b Compare August 19, 2019 15:55
@necolas necolas merged commit 56d1b0f into facebook:master Aug 19, 2019
@necolas necolas deleted the react-events/testing-library branch December 20, 2019 15:36
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.

None yet

4 participants