import { act , fireEvent , render } from '@testing-library/react' ; import React from 'react' ; import { SWRConfig } from 'swr' ; export function sleep ( time ) { return new Promise ( resolve => setTimeout ( resolve , time ) ) ; } export const createResponse = < T > ( response , { delay } = { delay : 10 } ) : Promise < T > => new Promise ( ( resolve , reject ) => setTimeout ( ( ) => { if ( response instanceof Error ) { reject ( response ) ; } else { resolve ( response ) ; } } , delay ) ) ; export const nextTick = ( ) => act ( ( ) => sleep ( 1 ) ) ; export const focusOn = ( element ) => act ( async ( ) => { fireEvent . focus ( element ) ; } ) ; export const createKey = ( ) => 'swr-key-' + ~ ~ ( Math . random ( ) * 1e7 ) ; const _renderWithConfig = ( element , config ) : ReturnType < typeof render > => { const TestSWRConfig = ( { children } ) => ( < SWRConfig value = { config } > { children } < /SWRConfig>); return render ( element , { wrapper : TestSWRConfig } ) ; } ; export const renderWithConfig = ( element , config ? ) : ReturnType < typeof _renderWithConfig > => { const provider = ( ) => new Map ( ) ; return _renderWithConfig ( element , { provider , ... config } ) ; } ; export const renderWithGlobalCache = ( element , config ? ) : ReturnType < typeof _renderWithConfig > => { return _renderWithConfig ( element , { ... config } ) ; } ; export const mockVisibilityHidden = ( ) => { const mockVisibilityState = jest . spyOn ( document , 'visibilityState' , 'get' ) ; mockVisibilityState . mockImplementation ( ( ) => 'hidden' ) ; return ( ) => mockVisibilityState . mockRestore ( ) ; } ;