# All files / react testFromStyleguidist.js

100% Statements 27/27
100% Branches 2/2
100% Functions 11/11
100% Lines 26/26

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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184

    1x
    1x
    2x

    1x
    19x

    62x

    19x

    19x
    19x
    19x
    19x
    19x
    123x
    19x
    19x
    19x
    62x

    19x

    19x
    62x

    62x
    62x
    62x
    62x
    62x
    19x

     import React from 'react'
    import Preview from 'react-styleguidist/lib/rsg-components/Preview'
    import fs from 'fs'
    import chunkify from 'react-styleguidist/loaders/utils/chunkify'
    import pretty from 'pretty'

    /*
      Styleguidist does some black magic with AST to automatically provide
      components to the code in the README ([examples-loader]).

      We manually do that below by importing each components and binding
      them to the function that executes the README code.

      examples-loader: https://github.com/styleguidist/react-styleguidist/blob/master/loaders/examples-loader.js
     */
    import ActionMenu from './ActionMenu'
    import Alerter from './Alerter'
    import Avatar from './Avatar'
    import Badge from './Badge'
    import Button from './Button'
    import ButtonAction from './ButtonAction'
    import Checkbox from './Checkbox'
    import Field from './Field'
    import Hero from './Hero'
    import I18n from './I18n'
    import Icon from './Icon'
    import Input from './Input'
    import IntentHeader from './IntentHeader'
    import IntentIframe from './IntentIframe'
    import IntentModal from './IntentModal'
    import IntentOpener from './IntentOpener'
    import Label from './Label'
    import ListItemText from './ListItemText'
    import Media from './Media'
    import Menu from './Menu'
    import MidEllipsis from './MidEllipsis'
    import Modal from './Modal'
    import Nav from './Nav'
    import Overlay from './Overlay'
    import Panel from './Panel'
    import Radio from './Radio'
    import SelectBox from './SelectBox'
    import SelectionBar from './SelectionBar'
    import Sidebar from './Sidebar'
    import Spinner from './Spinner'
    import Tabs from './Tabs'
    import Text from './Text'
    import Textarea from './Textarea'
    import Toggle from './Toggle'

    // Mock error otherwise there are errors with the createStylesheet function
    jest.mock('react-styleguidist/lib/rsg-components/PlaygroundError', () => {
      return function(props) {
        return <div>{props.message}</div>
      }
    })

    const testFromStyleguidist = (name, markdown, require) => {
      const evalInContext = a =>
        // eslint-disable-next-line no-new-func
        new Function(
          'require',
          'ActionMenu',
          'Alerter',
          'Avatar',
          'Badge',
          'Button',
          'ButtonAction',
          'Checkbox',
          'Field',
          'Hero',
          'I18n',
          'Icon',
          'Input',
          'IntentHeader',
          'IntentIframe',
          'IntentModal',
          'IntentOpener',
          'Label',
          'ListItemText',
          'Media',
          'Menu',
          'MidEllipsis',
          'Modal',
          'Nav',
          'Overlay',
          'Panel',
          'Radio',
          'SelectBox',
          'SelectionBar',
          'Sidebar',
          'Spinner',
          'Tabs',
          'Text',
          'Textarea',
          'Toggle',
          'isTesting',
          'state',
          'setState',
          `const React = require("react");
          ` + a
        ).bind(
          null,
          require,
          ActionMenu,
          Alerter,
          Avatar,
          Badge,
          Button,
          ButtonAction,
          Checkbox,
          Field,
          Hero,
          I18n,
          Icon,
          Input,
          IntentHeader,
          IntentIframe,
          IntentModal,
          IntentOpener,
          Label,
          ListItemText,
          Media,
          Menu,
          MidEllipsis,
          Modal,
          Nav,
          Overlay,
          Panel,
          Radio,
          SelectBox,
          SelectionBar,
          Sidebar,
          Spinner,
          Tabs,
          Text,
          Textarea,
          Toggle,
          true
        )

      const options = {
        context: {
          config: {
            compilerConfig: {
              objectAssign: 'Object.assign'
            }
          }
        }
      }

      describe(name, () => {
        it('should render examples', done => {
          let doneCounter = 0
          const Readme = fs.readFileSync(markdown)
          const chunks = chunkify(Readme)
          const codes = chunks.filter(x => x.type === 'code')
          const rendered = []
          const finish = () => {
            rendered.forEach(rendered => {
              expect(rendered).toMatchSnapshot(name)
            })
            done()
          }
          codes.forEach(code => {
            const root = mount(
              <Preview code={code.content} evalInContext={evalInContext} />,
              options
            )
            requestAnimationFrame(() => {
              root.update()
              rendered.push(pretty(root.html()))
              doneCounter++
              if (doneCounter === codes.length) {
                finish()
              }
            })
          })
        })
      })
    }

    export default testFromStyleguidist