diff --git a/src/__tests__/click.js b/src/__tests__/click.js index 810a25d7..5630c230 100644 --- a/src/__tests__/click.js +++ b/src/__tests__/click.js @@ -1,95 +1,64 @@ import React from 'react' import {render, screen} from '@testing-library/react' -import '@testing-library/jest-dom/extend-expect' import userEvent from '..' +import {setup} from './helpers/utils' + +test('click in input', () => { + const {element, getEventCalls} = setup('input') + userEvent.click(element) + expect(getEventCalls()).toMatchInlineSnapshot(` + mouseover: Left (0) + mousemove: Left (0) + mousedown: Left (0) + focus + mouseup: Left (0) + click: Left (0) + `) +}) -test.each(['input', 'textarea'])( - 'should fire the correct events for <%s>', - type => { - const events = [] - const eventsHandler = jest.fn(evt => events.push(evt.type)) - render( - React.createElement(type, { - 'data-testid': 'element', - onMouseOver: eventsHandler, - onMouseMove: eventsHandler, - onMouseDown: eventsHandler, - onFocus: eventsHandler, - onMouseUp: eventsHandler, - onClick: eventsHandler, - }), - ) - - userEvent.click(screen.getByTestId('element')) - - expect(events).toEqual([ - 'mouseover', - 'mousemove', - 'mousedown', - 'focus', - 'mouseup', - 'click', - ]) - }, -) +test('click in textarea', () => { + const {element, getEventCalls} = setup('textarea') + userEvent.click(element) + expect(getEventCalls()).toMatchInlineSnapshot(` + mouseover: Left (0) + mousemove: Left (0) + mousedown: Left (0) + focus + mouseup: Left (0) + click: Left (0) + `) +}) it('should fire the correct events for ', () => { - const events = [] - const eventsHandler = jest.fn(evt => events.push(evt.type)) - render( - , - ) - - userEvent.click(screen.getByTestId('element')) - - expect(events).toEqual([ - 'mouseover', - 'mousemove', - 'mousedown', - 'focus', - 'mouseup', - 'click', - 'change', - ]) - - expect(screen.getByTestId('element')).toHaveProperty('checked', true) + const {element, getEventCalls} = setup('input', {type: 'checkbox'}) + expect(element).not.toBeChecked() + userEvent.click(element) + expect(getEventCalls()).toMatchInlineSnapshot(` + mouseover: Left (0) + mousemove: Left (0) + mousedown: Left (0) + focus + mouseup: Left (0) + click: unchecked -> checked + input: checked + change + `) }) it('should fire the correct events for ', () => { - const events = [] - const eventsHandler = jest.fn(evt => events.push(evt.type)) - render( - , - ) - - userEvent.click(screen.getByTestId('element')) - - expect(events).toEqual([]) - - expect(screen.getByTestId('element')).toHaveProperty('checked', false) + const {element, getEventCalls} = setup('input', { + type: 'checkbox', + disabled: true, + }) + userEvent.click(element) + expect(element).toBeDisabled() + // no event calls is expected here: + expect(getEventCalls()).toMatchInlineSnapshot(``) + expect(element).toBeDisabled() }) +// TODO: Update all these tests to use the setup util... + it('should fire the correct events for ', () => { const events = [] const eventsHandler = jest.fn(evt => events.push(evt.type)) @@ -434,12 +403,14 @@ test.each(['input', 'textarea'])( it('should fire mouse events with the correct properties', () => { const events = [] - const eventsHandler = jest.fn(evt => events.push({ - type: evt.type, - button: evt.button, - buttons: evt.buttons, - detail: evt.detail - })) + const eventsHandler = jest.fn(evt => + events.push({ + type: evt.type, + button: evt.button, + buttons: evt.buttons, + detail: evt.detail, + }), + ) render(