From c00e8653d0b077cf3bb7a18888fdc02110b3bed4 Mon Sep 17 00:00:00 2001 From: Gpx Date: Sun, 23 Sep 2018 00:30:01 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20click=20handles=20checkbo?= =?UTF-8?q?xes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/events.js | 234 ++++++++++++++++++++++++++------------------ src/index.js | 92 ++++++++++------- 2 files changed, 192 insertions(+), 134 deletions(-) diff --git a/__tests__/events.js b/__tests__/events.js index 80711d55..1b9c01fc 100644 --- a/__tests__/events.js +++ b/__tests__/events.js @@ -1,57 +1,99 @@ -import React from 'react' -import { render, cleanup } from 'react-testing-library' -import 'jest-dom/extend-expect' -import userEvent from '../src' +import React from "react"; +import { render, cleanup } from "react-testing-library"; +import "jest-dom/extend-expect"; +import userEvent from "../src"; -afterEach(cleanup) +afterEach(cleanup); -describe('fireEvent.click', () => { - it.each(['input', 'textarea'])( - 'should fire the correct events for <%s>', +describe("fireEvent.click", () => { + it.each(["input", "textarea"])( + "should fire the correct events for <%s>", type => { - const onMouseOver = jest.fn() - const onMouseMove = jest.fn() - const onMouseDown = jest.fn() - const onFocus = jest.fn() - const onMouseUp = jest.fn() - const onClick = jest.fn() + const onMouseOver = jest.fn(); + const onMouseMove = jest.fn(); + const onMouseDown = jest.fn(); + const onFocus = jest.fn(); + const onMouseUp = jest.fn(); + const onClick = jest.fn(); const { getByTestId } = render( React.createElement(type, { - 'data-testid': 'element', + "data-testid": "element", onMouseOver: onMouseOver, onMouseMove: onMouseMove, onMouseDown: onMouseDown, onFocus: onFocus, onMouseUp: onMouseUp, - onClick: onClick, + onClick: onClick }) - ) - - expect(onMouseOver).not.toHaveBeenCalled() - expect(onMouseMove).not.toHaveBeenCalled() - expect(onMouseDown).not.toHaveBeenCalled() - expect(onFocus).not.toHaveBeenCalled() - expect(onMouseUp).not.toHaveBeenCalled() - expect(onClick).not.toHaveBeenCalled() - - userEvent.click(getByTestId('element')) - - expect(onMouseOver).toHaveBeenCalledTimes(1) - expect(onMouseMove).toHaveBeenCalledTimes(1) - expect(onMouseDown).toHaveBeenCalledTimes(1) - expect(onFocus).toHaveBeenCalledTimes(1) - expect(onMouseUp).toHaveBeenCalledTimes(1) - expect(onClick).toHaveBeenCalledTimes(1) + ); + + expect(onMouseOver).not.toHaveBeenCalled(); + expect(onMouseMove).not.toHaveBeenCalled(); + expect(onMouseDown).not.toHaveBeenCalled(); + expect(onFocus).not.toHaveBeenCalled(); + expect(onMouseUp).not.toHaveBeenCalled(); + expect(onClick).not.toHaveBeenCalled(); + + userEvent.click(getByTestId("element")); + + expect(onMouseOver).toHaveBeenCalledTimes(1); + expect(onMouseMove).toHaveBeenCalledTimes(1); + expect(onMouseDown).toHaveBeenCalledTimes(1); + expect(onFocus).toHaveBeenCalledTimes(1); + expect(onMouseUp).toHaveBeenCalledTimes(1); + expect(onClick).toHaveBeenCalledTimes(1); } - ) - - it('should fire the correct events for
', () => { - const onMouseOver = jest.fn() - const onMouseMove = jest.fn() - const onMouseDown = jest.fn() - const onFocus = jest.fn() - const onMouseUp = jest.fn() - const onClick = jest.fn() + ); + + it('should fire the correct events for ', () => { + const onMouseOver = jest.fn(); + const onMouseMove = jest.fn(); + const onMouseDown = jest.fn(); + const onFocus = jest.fn(); + const onMouseUp = jest.fn(); + const onClick = jest.fn(); + const onChange = jest.fn(); + const { getByTestId } = render( + + ); + + expect(onMouseOver).not.toHaveBeenCalled(); + expect(onMouseMove).not.toHaveBeenCalled(); + expect(onMouseDown).not.toHaveBeenCalled(); + expect(onFocus).not.toHaveBeenCalled(); + expect(onMouseUp).not.toHaveBeenCalled(); + expect(onClick).not.toHaveBeenCalled(); + expect(onChange).not.toHaveBeenCalled(); + + userEvent.click(getByTestId("element")); + + expect(onMouseOver).toHaveBeenCalledTimes(1); + expect(onMouseMove).toHaveBeenCalledTimes(1); + expect(onMouseDown).toHaveBeenCalledTimes(1); + expect(onFocus).not.toHaveBeenCalledTimes(1); + expect(onMouseUp).toHaveBeenCalledTimes(1); + expect(onClick).toHaveBeenCalledTimes(1); + expect(onChange).toHaveBeenCalledTimes(1); + expect(getByTestId("element")).toHaveProperty("checked", true); + }); + + it("should fire the correct events for
", () => { + const onMouseOver = jest.fn(); + const onMouseMove = jest.fn(); + const onMouseDown = jest.fn(); + const onFocus = jest.fn(); + const onMouseUp = jest.fn(); + const onClick = jest.fn(); const { getByTestId } = render(
{ onMouseUp={onMouseUp} onClick={onClick} /> - ) - - expect(onMouseOver).not.toHaveBeenCalled() - expect(onMouseMove).not.toHaveBeenCalled() - expect(onMouseDown).not.toHaveBeenCalled() - expect(onFocus).not.toHaveBeenCalled() - expect(onMouseUp).not.toHaveBeenCalled() - expect(onClick).not.toHaveBeenCalled() - - userEvent.click(getByTestId('div')) - - expect(onMouseOver).toHaveBeenCalledTimes(1) - expect(onMouseMove).toHaveBeenCalledTimes(1) - expect(onMouseDown).toHaveBeenCalledTimes(1) - expect(onFocus).not.toHaveBeenCalled() - expect(onMouseUp).toHaveBeenCalledTimes(1) - expect(onClick).toHaveBeenCalledTimes(1) - }) - - it('toggles the focus', () => { + ); + + expect(onMouseOver).not.toHaveBeenCalled(); + expect(onMouseMove).not.toHaveBeenCalled(); + expect(onMouseDown).not.toHaveBeenCalled(); + expect(onFocus).not.toHaveBeenCalled(); + expect(onMouseUp).not.toHaveBeenCalled(); + expect(onClick).not.toHaveBeenCalled(); + + userEvent.click(getByTestId("div")); + + expect(onMouseOver).toHaveBeenCalledTimes(1); + expect(onMouseMove).toHaveBeenCalledTimes(1); + expect(onMouseDown).toHaveBeenCalledTimes(1); + expect(onFocus).not.toHaveBeenCalled(); + expect(onMouseUp).toHaveBeenCalledTimes(1); + expect(onClick).toHaveBeenCalledTimes(1); + }); + + it("toggles the focus", () => { const { getByTestId } = render( - ) + ); - const a = getByTestId('A') - const b = getByTestId('B') + const a = getByTestId("A"); + const b = getByTestId("B"); - expect(a).not.toHaveFocus() - expect(b).not.toHaveFocus() + expect(a).not.toHaveFocus(); + expect(b).not.toHaveFocus(); - userEvent.click(a) - expect(a).toHaveFocus() - expect(b).not.toHaveFocus() + userEvent.click(a); + expect(a).toHaveFocus(); + expect(b).not.toHaveFocus(); - userEvent.click(b) - expect(a).not.toHaveFocus() - expect(a).not.toHaveFocus() - }) + userEvent.click(b); + expect(a).not.toHaveFocus(); + expect(a).not.toHaveFocus(); + }); - it.each(['input', 'textarea'])( - 'gives focus to <%s> when clicking a