Skip to content

Commit

Permalink
add tests and handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche committed Oct 19, 2021
1 parent 3cf0336 commit bb2cd4a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
67 changes: 67 additions & 0 deletions src/__tests__/pointer/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {wait} from 'utils'
import userEvent from '../../index'
import {setup} from '../helpers/utils'

Expand Down Expand Up @@ -231,3 +232,69 @@ test('drag touch', () => {
click - button=0; buttons=0; detail=1
`)
})

test('unknown button does nothing', () => {
const {element, getEvents} = setup(`<div></div>`)

userEvent.pointer({keys: '[foo]', target: element})

expect(getEvents()).toEqual([])
})

describe('error', () => {
afterEach(() => {
;(console.error as jest.MockedFunction<typeof console.error>).mockClear()
})

it('error for unknown pointer in sync', async () => {
const err = jest.spyOn(console, 'error')
err.mockImplementation(() => {})

const {element} = setup(`<div></div>`)
userEvent.pointer({pointerName: 'foo', target: element})

// the catch will be asynchronous
await wait(10)

expect(err).toHaveBeenCalledWith(expect.any(Error) as unknown)
expect(err.mock.calls[0][0]).toHaveProperty(
'message',
expect.stringContaining('does not exist'),
)
})

it('error for unknown pointer in async', async () => {
const {element} = setup(`<div></div>`)
const promise = userEvent.pointer(
{pointerName: 'foo', target: element},
{delay: 1},
)

return expect(promise).rejects.toThrowError('does not exist')
})
})

test('asynchronous pointer', async () => {
const {element, getClickEventsSnapshot} = setup(`<div></div>`)

// eslint-disable-next-line testing-library/no-await-sync-events
const pointerState = await userEvent.pointer(
{keys: '[MouseLeft]', target: element},
{delay: 1},
)
// eslint-disable-next-line testing-library/no-await-sync-events
await userEvent.pointer([{coords: {x: 20, y: 20}}, '[/MouseLeft]'], {
delay: 1,
pointerState,
})

expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
mousedown - button=0; buttons=0; detail=1
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
mouseup - button=0; buttons=0; detail=1
click - button=0; buttons=0; detail=1
pointermove - pointerId=1; pointerType=mouse; isPrimary=undefined
mousemove - button=0; buttons=0; detail=0
`)
})
25 changes: 25 additions & 0 deletions src/__tests__/utils/misc/isDescendantOrSelf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {setup} from '__tests__/helpers/utils'
import {isDescendantOrSelf} from '../../../utils'

test('isDescendantOrSelf', () => {
setup(`<div><p><span></span><a></a></p></div>`)

expect(
isDescendantOrSelf(
document.querySelector('span') as Element,
document.querySelector('a') as Element,
),
).toBe(false)
expect(
isDescendantOrSelf(
document.querySelector('span') as Element,
document.querySelector('div') as Element,
),
).toBe(true)
expect(
isDescendantOrSelf(
document.querySelector('span') as Element,
document.querySelector('span') as Element,
),
).toBe(true)
})
8 changes: 7 additions & 1 deletion src/pointer/pointerAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export async function pointerAction(
actions: PointerAction[],
options: pointerOptions,
state: pointerState,
): Promise<void> {
): Promise<unknown[]> {
const ret: Array<Promise<void>> = []

for (let i = 0; i < actions.length; i++) {
const action = actions[i]
const pointerName =
Expand All @@ -43,6 +45,8 @@ export async function pointerAction(
? pointerPress({...action, target, coords}, state)
: pointerMove({...action, target, coords}, state)

ret.push(promise)

if (options.delay > 0) {
await promise
if (i < actions.length - 1) {
Expand All @@ -52,6 +56,8 @@ export async function pointerAction(
}

delete state.activeClickCount

return Promise.all(ret)
}

function getPrevTarget(pointerName: string, state: pointerState) {
Expand Down

0 comments on commit bb2cd4a

Please sign in to comment.