Skip to content

Commit

Permalink
feat: add UseMouseOut test
Browse files Browse the repository at this point in the history
  • Loading branch information
yeojz committed Apr 7, 2019
1 parent 6dd54aa commit ba39a8a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/UseMouseOut.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import { render, fireEvent } from 'react-testing-library';
import UseMouseOut from './UseMouseOut';

test('event should call props.fn when target match', (): void => {
const fn = jest.fn();

const { baseElement } = render(
<UseMouseOut fn={fn} target={(): HTMLElement => document.body} />
);

fireEvent.mouseOut(baseElement);
expect(fn).toBeCalledTimes(1);
});

test('event should NOT call props.fn when target mismatch', (): void => {
const fn = jest.fn();

const { getByTestId } = render(
<div>
<UseMouseOut fn={fn} target={(): HTMLElement => document.body} />
<div data-testid="div-element" />
</div>
);

fireEvent.mouseOut(getByTestId('div-element'));
expect(fn).toBeCalledTimes(0);
});
2 changes: 1 addition & 1 deletion src/UseMouseOut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class UseMouseOut extends React.Component<Props> {

if (
target &&
(target == evt.target || target === document || target === window)
(target === evt.target || target === document || target === window)
) {
this.props.fn();
}
Expand Down

0 comments on commit ba39a8a

Please sign in to comment.