# All files / helpers generateSnapshots.ts

70.59% Statements 12/17
50% Branches 3/6
100% Functions 2/2
68.75% Lines 11/16

    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
    1x
    1x

    1x
    1x

    1x

    1x

    1x
    1x
    1x

    1x

    1x

     /* tslint:disable:no-console */
    import React = require("react");
    import renderer = require("react-test-renderer");

    export interface IExampleProps {
        component: React.ReactNode;
        data: any;
        styles: string;
    }

    export interface IExample {
        title: string;
        props?: IExampleProps;
        children?: React.ReactNode;
    }

    export interface IExampes {
        data: IExample[];
    }

    /**
     * Automates taking snapshot tests from code examples.
     * @param {Array} examples - a set of example objects that contain a title, a
     * @param {Function} reactComponent - The react component to render
     * react component, props for that component, and children for that component
     */
    export default (examples: IExampes, reactComponent: React.ComponentClass<any>): void => {
        Iif (!reactComponent) {
            console.error("No component param passed to generateSnapshots.");
            return;
        }

        Iif (!examples) {
            console.error("No examples param passed to generateSnapshots.");
            return;
        }

        Iif (!examples.data.length) {
            console.warn(
                "No examples found - ensure your example exports an object with a `data` member containing an array of component examples."
            );
        }

        for (const example of examples.data) {
            test(example.title, (): void => {
                const component: any = renderer.create(
                    React.createElement(reactComponent, example.props, example.children)
                );

                const json: any = component.toJSON();

                expect(json).toMatchSnapshot();
            });
        }
    };