All files / src test-utils.js

88.89% Statements 24/27
75% Branches 9/12
100% Functions 9/9
87.5% Lines 21/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                      6x 5x 5x 5x           6x           1x 1x 1x 1x 10x   1x                             6x 106x     667x     667x 667x       667x       106x 106x     667x 106x    
import jsdom from 'jsdom'
import fetch from 'node-fetch'
import yakbak from 'yakbak'
import http from 'http'
import killable from 'killable'
import Promise from 'bluebird'
import ReactPropTypesSecret from 'prop-types/lib/ReactPropTypesSecret'
 
/**
 * @private
 */
export const yakbakServer = ({url, dirname, port}) => {
    const server = killable(http.createServer(yakbak(url, {dirname})))
    const listen = Promise.promisify(server.listen, {context: server})
    return listen(port).then(() => server)
}
 
/**
 * @private
 */
export const getWindow = (url) => jsdom.JSDOM.fromURL(url, {
        resources: 'usable',
        runScripts: 'dangerously',
        virtualConsole: new jsdom.VirtualConsole()
    })
    .then((dom) => {
        const window = dom.window
        window.fetch = fetch
        return new Promise((resolve) => {
            const poll = () => {
                return !window.jQuery ? setTimeout(poll, 200) : resolve(window)
            }
            poll()
        })
    })
 
 
/**
 * Perform prop-type validation and return the error strings as an Array
 * or undefined, if there were no errors.
 *
 * @private
 *
 * @param spec {Object} The prop type spec
 * @param value {Object} The value to validate
 * @returns (string[]|undefined)
 */
export const propTypeErrors = (spec, value) => {
    const getError = (key) => {
        let error
 
        Iif (typeof spec[key] !== 'function') {
            throw new Error(`Invalid type spec at ${key} - expected a function`)
        }
        try {
          error = spec[key](value, key, 'assertion', '', '', ReactPropTypesSecret)
        } catch (ex) {
          error = ex
        }
        return (error && (!(error instanceof Error) ?
            'Invalid type spec - function must return `null` or an `Error`' :
            `Failed prop type: ${key} ${error.message}`))
    }
    const keys = Object.keys(spec)
    Iif (keys.length === 0) {
        return [new Error('Invalid type spec - expected at least one key')]
    }
    const errors = keys.map(getError).filter((x) => Boolean(x))
    return errors.length > 0 ? errors : undefined
}