ten1seven / what-input

A global utility for tracking the current input method (mouse/pointer, keyboard or touch).
https://ten1seven.github.io/what-input
MIT License
1.35k stars 89 forks source link

How to force input type in tests? #65

Open vysinsky opened 7 years ago

vysinsky commented 7 years ago

Hello,

how can I force some input type in tests? I need to test my DropdownMenu which is behaving differently on touch (opens on click) and on mouse (opens on hover). I can run any JS from the test. Is there any way to trigger what-input detection?

Thank you.

ten1seven commented 7 years ago

Hi @vysinsky,

I've played around with using CasperJS to do testing. You can use things like mouse.move and mouse.click to simulate mouse events and then test the DOM to see if the event triggered what you wanted. Unfortunately CasperJS doesn't yet have touch event simulation.

greypants commented 4 years ago

If you're using Jest, i generally create a second *.touch.test.js file where I mock whatInput.ask to return touch:

jest.mock("what-input", () => ({
  ask: jest.fn(() => "touch")
}));
mihkeleidast commented 3 years ago

Triggering change events is also very difficult. I ended up ignoring the function passed into whatInput.registerOnChange, because there is no simple way to trigger that in tests.