- 
                Notifications
    You must be signed in to change notification settings 
- Fork 276
feat: User Event core code #1405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      65710a8
              
                chore: extract user event common parts
              
              
                mdjastrzebski 1c22b21
              
                chore: stub press/type implementations
              
              
                mdjastrzebski a9114ab
              
                chore: add sample tests
              
              
                mdjastrzebski d5f2bd2
              
                refactor: tweaks
              
              
                mdjastrzebski c407cff
              
                chore: improve test coverage
              
              
                mdjastrzebski 416a7a5
              
                chore: more sample tests
              
              
                mdjastrzebski 72fbd12
              
                refactor: remove Date.now touch event timestamp
              
              
                mdjastrzebski 56da2fd
              
                chore: hide new docs from website
              
              
                mdjastrzebski 6a65d53
              
                feat: expose direct access
              
              
                mdjastrzebski File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| interface EventEntry { | ||
| name: string; | ||
| payload: any; | ||
| } | ||
|  | ||
| export function createEventLogger() { | ||
| const events: EventEntry[] = []; | ||
| const logEvent = (name: string) => { | ||
| return (event: unknown) => { | ||
| const eventEntry: EventEntry = { | ||
| name, | ||
| payload: event, | ||
| }; | ||
|  | ||
| events.push(eventEntry); | ||
| }; | ||
| }; | ||
|  | ||
| return { events, logEvent }; | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './events'; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| export const CommonEventBuilder = { | ||
| /** | ||
| * Experimental values: | ||
| * - iOS: `{"changedTouches": [[Circular]], "identifier": 1, "locationX": 253, "locationY": 30.333328247070312, "pageX": 273, "pageY": 141.3333282470703, "target": 75, "timestamp": 875928682.0450834, "touches": [[Circular]]}` | ||
| * - Android: `{"changedTouches": [[Circular]], "identifier": 0, "locationX": 160, "locationY": 40.3636360168457, "pageX": 180, "pageY": 140.36363220214844, "target": 53, "targetSurface": -1, "timestamp": 10290805, "touches": [[Circular]]}` | ||
| */ | ||
| touch: () => { | ||
| return { | ||
| nativeEvent: { | ||
| changedTouches: [], | ||
| identifier: 0, | ||
| locationX: 0, | ||
| locationY: 0, | ||
| pageX: 0, | ||
| pageY: 0, | ||
| target: 0, | ||
| timestamp: 0, | ||
| touches: [], | ||
| }, | ||
| }; | ||
| }, | ||
|  | ||
| /** | ||
| * Experimental values: | ||
| * - iOS: `{"eventCount": 0, "target": 75, "text": ""}` | ||
| * - Android: `{"target": 53}` | ||
| */ | ||
| focus: () => { | ||
| return { | ||
| nativeEvent: { | ||
| target: 0, | ||
| }, | ||
| }; | ||
| }, | ||
|  | ||
| /** | ||
| * Experimental values: | ||
| * - iOS: `{"eventCount": 0, "target": 75, "text": ""}` | ||
| * - Android: `{"target": 53}` | ||
| */ | ||
| blur: () => { | ||
| return { | ||
| nativeEvent: { | ||
| target: 0, | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { CommonEventBuilder } from './common'; | ||
|  | ||
| export const EventBuilder = { | ||
| Common: CommonEventBuilder, | ||
| }; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { ReactTestInstance } from 'react-test-renderer'; | ||
| import { setup } from './setup'; | ||
|  | ||
| export const userEvent = { | ||
| setup, | ||
|  | ||
| // Direct access for User Event v13 compatibility | ||
| press: (element: ReactTestInstance) => setup().press(element), | ||
| type: (element: ReactTestInstance, text: string) => | ||
| setup().type(element, text), | ||
| }; | 
        
          
          
            54 changes: 54 additions & 0 deletions
          
          54 
        
  src/user-event/press/__tests__/__snapshots__/press.test.tsx.snap
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|  | ||
| exports[`user.press() dispatches required events on Text 1`] = ` | ||
| [ | ||
| { | ||
| "name": "pressIn", | ||
| "payload": { | ||
| "nativeEvent": { | ||
| "changedTouches": [], | ||
| "identifier": 0, | ||
| "locationX": 0, | ||
| "locationY": 0, | ||
| "pageX": 0, | ||
| "pageY": 0, | ||
| "target": 0, | ||
| "timestamp": 0, | ||
| "touches": [], | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| "name": "press", | ||
| "payload": { | ||
| "nativeEvent": { | ||
| "changedTouches": [], | ||
| "identifier": 0, | ||
| "locationX": 0, | ||
| "locationY": 0, | ||
| "pageX": 0, | ||
| "pageY": 0, | ||
| "target": 0, | ||
| "timestamp": 0, | ||
| "touches": [], | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| "name": "pressOut", | ||
| "payload": { | ||
| "nativeEvent": { | ||
| "changedTouches": [], | ||
| "identifier": 0, | ||
| "locationX": 0, | ||
| "locationY": 0, | ||
| "pageX": 0, | ||
| "pageY": 0, | ||
| "target": 0, | ||
| "timestamp": 0, | ||
| "touches": [], | ||
| }, | ||
| }, | ||
| }, | ||
| ] | ||
| `; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import * as React from 'react'; | ||
| import { Text } from 'react-native'; | ||
| import { createEventLogger } from '../../../test-utils'; | ||
| import { render } from '../../..'; | ||
| import { userEvent } from '../..'; | ||
|  | ||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
| }); | ||
|  | ||
| describe('user.press()', () => { | ||
| it('dispatches required events on Text', async () => { | ||
| const { events, logEvent } = createEventLogger(); | ||
| const user = userEvent.setup(); | ||
| const screen = render( | ||
| <Text | ||
| testID="view" | ||
| onPress={logEvent('press')} | ||
| onPressIn={logEvent('pressIn')} | ||
| onPressOut={logEvent('pressOut')} | ||
| /> | ||
| ); | ||
|  | ||
| await user.press(screen.getByTestId('view')); | ||
|  | ||
| const eventNames = events.map((event) => event.name); | ||
| expect(eventNames).toEqual(['pressIn', 'press', 'pressOut']); | ||
| expect(events).toMatchSnapshot(); | ||
| }); | ||
|  | ||
| it('supports direct access', async () => { | ||
| const { events, logEvent } = createEventLogger(); | ||
| const screen = render( | ||
| <Text | ||
| testID="view" | ||
| onPress={logEvent('press')} | ||
| onPressIn={logEvent('pressIn')} | ||
| onPressOut={logEvent('pressOut')} | ||
| /> | ||
| ); | ||
|  | ||
| await userEvent.press(screen.getByTestId('view')); | ||
|  | ||
| const eventNames = events.map((event) => event.name); | ||
| expect(eventNames).toEqual(['pressIn', 'press', 'pressOut']); | ||
| }); | ||
|  | ||
| it.each(['modern', 'legacy'])('works with fake %s timers', async (type) => { | ||
| jest.useFakeTimers({ legacyFakeTimers: type === 'legacy' }); | ||
|  | ||
| const { events, logEvent } = createEventLogger(); | ||
| const user = userEvent.setup(); | ||
| const screen = render( | ||
| <Text | ||
| testID="view" | ||
| onPress={logEvent('press')} | ||
| onPressIn={logEvent('pressIn')} | ||
| onPressOut={logEvent('pressOut')} | ||
| /> | ||
| ); | ||
|  | ||
| await user.press(screen.getByTestId('view')); | ||
|  | ||
| const eventNames = events.map((event) => event.name); | ||
| expect(eventNames).toEqual(['pressIn', 'press', 'pressOut']); | ||
| }); | ||
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { press } from './press'; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { ReactTestInstance } from 'react-test-renderer'; | ||
| import { EventBuilder } from '../event-builder'; | ||
| import { UserEventInstance } from '../setup'; | ||
| import { dispatchHostEvent, wait } from '../utils'; | ||
|  | ||
| export async function press( | ||
| this: UserEventInstance, | ||
| element: ReactTestInstance | ||
| ) { | ||
| // TODO provide real implementation | ||
| dispatchHostEvent(element, 'pressIn', EventBuilder.Common.touch()); | ||
|  | ||
| await wait(this.config); | ||
| dispatchHostEvent(element, 'press', EventBuilder.Common.touch()); | ||
| dispatchHostEvent(element, 'pressOut', EventBuilder.Common.touch()); | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export type { UserEventConfig, UserEventInstance } from './setup'; | ||
| export { setup } from './setup'; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { ReactTestInstance } from 'react-test-renderer'; | ||
| import { jestFakeTimersAreEnabled } from '../../helpers/timers'; | ||
| import { press } from '../press'; | ||
| import { type } from '../type'; | ||
|  | ||
| export interface UserEventSetupOptions { | ||
| /** | ||
| * Between some subsequent inputs like typing a series of characters | ||
| * the code execution is delayed per `setTimeout` for (at least) `delay` seconds. | ||
| * This moves the next changes at least to next macro task | ||
| * and allows other (asynchronous) code to run between events. | ||
| * | ||
| * `null` prevents `setTimeout` from being called. | ||
| * | ||
| * @default 0 | ||
| */ | ||
| delay?: number; | ||
|  | ||
| /** | ||
| * Function to be called to advance fake timers. Setting it is necessary for | ||
| * fake timers to work. | ||
| * | ||
| * @example jest.advanceTimersByTime | ||
| */ | ||
| advanceTimers?: (delay: number) => Promise<void> | void; | ||
| } | ||
|  | ||
| /** | ||
| * This functions allow wait to work correctly under both real and fake Jest timers. | ||
| */ | ||
| function universalJestAdvanceTimersBy(ms: number) { | ||
| if (jestFakeTimersAreEnabled()) { | ||
| return jest.advanceTimersByTime(ms); | ||
| } else { | ||
| return Promise.resolve(); | ||
| } | ||
| } | ||
|  | ||
| const defaultOptions: Required<UserEventSetupOptions> = { | ||
| delay: 0, | ||
| advanceTimers: universalJestAdvanceTimersBy, | ||
| }; | ||
|  | ||
| /** | ||
| * Creates a new instance of user event instance with the given options. | ||
| * | ||
| * @param options | ||
| * @returns | ||
| */ | ||
| export function setup(options?: UserEventSetupOptions) { | ||
| const config = createConfig(options); | ||
| const instance = createInstance(config); | ||
| return instance; | ||
| } | ||
|  | ||
| export interface UserEventConfig { | ||
| delay: number; | ||
| advanceTimers: (delay: number) => Promise<void> | void; | ||
| } | ||
|  | ||
| function createConfig(options?: UserEventSetupOptions): UserEventConfig { | ||
| return { | ||
| ...defaultOptions, | ||
| ...options, | ||
| }; | ||
| } | ||
|  | ||
| export interface UserEventInstance { | ||
| config: UserEventConfig; | ||
| press: (element: ReactTestInstance) => Promise<void>; | ||
| type: (element: ReactTestInstance, text: string) => Promise<void>; | ||
| } | ||
|  | ||
| function createInstance(config: UserEventConfig): UserEventInstance { | ||
| const instance = { | ||
| config, | ||
| } as UserEventInstance; | ||
|  | ||
| // We need to bind these functions, as they access the config through 'this.config'. | ||
| const api = { | ||
| press: press.bind(instance), | ||
| type: type.bind(instance), | ||
| }; | ||
|  | ||
| Object.assign(instance, api); | ||
| return instance; | ||
| } | 
        
          
          
            26 changes: 26 additions & 0 deletions
          
          26 
        
  src/user-event/type/__tests__/__snapshots__/type.test.tsx.snap
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|  | ||
| exports[`user.type() dispatches required events 1`] = ` | ||
| [ | ||
| { | ||
| "name": "focus", | ||
| "payload": { | ||
| "nativeEvent": { | ||
| "target": 0, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| "name": "changeText", | ||
| "payload": "Hello World!", | ||
| }, | ||
| { | ||
| "name": "blur", | ||
| "payload": { | ||
| "nativeEvent": { | ||
| "target": 0, | ||
| }, | ||
| }, | ||
| }, | ||
| ] | ||
| `; | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.