2019-04-18 17:03:06 +03:00
/ *
* Enzyme helpers for injecting top - level contexts
* derived from https : //lingui.js.org/guides/testing.html
* /
import React from 'react' ;
2019-05-09 22:59:43 +03:00
import { shape , object , string , arrayOf } from 'prop-types' ;
2019-04-18 17:03:06 +03:00
import { mount , shallow } from 'enzyme' ;
import { I18nProvider } from '@lingui/react' ;
import { ConfigProvider } from '../src/contexts/Config' ;
const language = 'en-US' ;
const intlProvider = new I18nProvider (
{
language ,
catalogs : {
[ language ] : { }
}
} ,
{ }
) ;
const {
linguiPublisher : { i18n : originalI18n }
} = intlProvider . getChildContext ( ) ;
const defaultContexts = {
linguiPublisher : {
i18n : {
... originalI18n ,
_ : key => key . id , // provide _ macro, for just passing down the key
toJSON : ( ) => '/i18n/' ,
} ,
} ,
config : {
ansible _version : null ,
custom _virtualenvs : [ ] ,
version : null ,
toJSON : ( ) => '/config/'
} ,
router : {
history : {
push : ( ) => { } ,
replace : ( ) => { } ,
createHref : ( ) => { } ,
2019-04-18 20:10:17 +03:00
location : {
hash : '' ,
pathname : '' ,
search : '' ,
state : '' ,
2019-04-29 17:08:50 +03:00
} ,
toJSON : ( ) => '/history/' ,
2019-04-18 17:03:06 +03:00
} ,
route : {
location : {
hash : '' ,
pathname : '' ,
search : '' ,
state : '' ,
} ,
match : {
params : { } ,
isExact : false ,
path : '' ,
url : '' ,
2019-04-18 20:10:17 +03:00
}
2019-04-18 17:03:06 +03:00
} ,
toJSON : ( ) => '/router/' ,
} ,
} ;
function wrapContexts ( node , context ) {
2019-05-09 22:59:43 +03:00
const { config } = context ;
2019-05-15 17:06:14 +03:00
class Wrap extends React . Component {
render ( ) {
// eslint-disable-next-line react/no-this-in-sfc
const { children , ... props } = this . props ;
const component = React . cloneElement ( children , props ) ;
return (
2019-05-09 22:59:43 +03:00
< ConfigProvider value = { config } >
{ component }
< / ConfigProvider >
2019-04-18 17:03:06 +03:00
) ;
}
2019-05-15 17:06:14 +03:00
}
return (
< Wrap > { node } < / Wrap >
) ;
2019-04-18 17:03:06 +03:00
}
function applyDefaultContexts ( context ) {
if ( ! context ) {
return defaultContexts ;
}
const newContext = { } ;
Object . keys ( defaultContexts ) . forEach ( key => {
newContext [ key ] = {
... defaultContexts [ key ] ,
... context [ key ] ,
} ;
} ) ;
return newContext ;
}
export function shallowWithContexts ( node , options = { } ) {
const context = applyDefaultContexts ( options . context ) ;
return shallow ( wrapContexts ( node , context ) ) ;
}
export function mountWithContexts ( node , options = { } ) {
const context = applyDefaultContexts ( options . context ) ;
const childContextTypes = {
linguiPublisher : shape ( {
i18n : object . isRequired
} ) . isRequired ,
config : shape ( {
ansible _version : string ,
custom _virtualenvs : arrayOf ( string ) ,
version : string ,
} ) ,
router : shape ( {
route : shape ( {
location : shape ( { } ) ,
match : shape ( { } ) ,
} ) . isRequired ,
history : shape ( { } ) . isRequired ,
} ) ,
... options . childContextTypes
} ;
return mount ( wrapContexts ( node , context ) , { context , childContextTypes } ) ;
}
2019-05-22 15:46:16 +03:00
/ * *
2019-05-09 22:59:43 +03:00
* Wait for element ( s ) to achieve a desired state .
2019-05-22 15:46:16 +03:00
*
* @ param [ wrapper ] - A ReactWrapper instance
2019-05-09 22:59:43 +03:00
* @ param [ selector ] - The selector of the element ( s ) to wait for .
* @ param [ callback ] - Callback to poll - by default this checks for a node count of 1.
2019-05-22 15:46:16 +03:00
* /
2019-05-09 22:59:43 +03:00
export function waitForElement ( wrapper , selector , callback = el => el . length === 1 ) {
2019-05-22 15:46:16 +03:00
const interval = 100 ;
return new Promise ( ( resolve , reject ) => {
let attempts = 30 ;
( function pollElement ( ) {
wrapper . update ( ) ;
2019-05-09 22:59:43 +03:00
const el = wrapper . find ( selector ) ;
if ( callback ( el ) ) {
return resolve ( el ) ;
2019-05-22 15:46:16 +03:00
}
if ( -- attempts <= 0 ) {
2019-05-09 22:59:43 +03:00
const message = ` Expected condition for < ${ selector } > not met: ${ callback . toString ( ) } ` ;
return reject ( new Error ( message ) ) ;
2019-05-22 15:46:16 +03:00
}
return setTimeout ( pollElement , interval ) ;
} ( ) ) ;
} ) ;
}