import test from 'ava' import React from 'react' import { create as render } from 'react-test-renderer' import { StaticRouter } from 'react-router-dom' import sinon from 'sinon' import browserEnv from 'browser-env' import { Catch, CenteredLayout, FileList, Library, LiveEditor, LivePreview, ScopeProvider, ScrollTop, SidebarLayout, scope } from '../src' browserEnv() global.DIRNAME = 'beep' const renderJSON = el => render(el).toJSON() test('CenteredLayout renders with active prop', t => { const json = renderJSON( Hello ) t.snapshot(json) }) test('CenteredLayout does not render without active prop', t => { const json = renderJSON( Hello ) t.is(json, 'Hello') }) test('Catch renders', t => { const json = renderJSON( Catch ) t.snapshot(json) }) test('Catch renders error', t => { const Throws = props => { throw new Error('nope') return false } const json = renderJSON( ) t.is(json.type, 'pre') t.is(json.children[0], 'Error: nope') t.snapshot(json) }) test('FileList renders', t => { const json = renderJSON( ) t.snapshot(json) }) // doesn't seem to render correctly with refs test.skip('LiveEditor renders', t => { const json = renderJSON( ) t.snapshot(json) }) test('LivePreview renders', t => { const json = renderJSON( ) t.snapshot(json) }) test('ScopeProvider renders', t => { const json = renderJSON( Hello ) t.snapshot(json) }) test('ScrollTop renders', t => { const json = renderJSON( ) t.is(json, null) }) test('ScrollTop scrolls window on location change', t => { sinon.stub(window, 'scrollTo') const instance = render( ) instance.update( ) t.true(window.scrollTo.calledOnce) window.scrollTo.restore() }) test('ScrollTop scrolls to hash', t => { const el = document.body.appendChild( document.createElement('div') ) el.id = 'hello' el.scrollIntoView = sinon.spy() const instance = render( ) instance.update( ) t.true(el.scrollIntoView.calledOnce) }) test('ScrollTop does not scroll to hash when there is no element', t => { const el = document.body.appendChild( document.createElement('div') ) el.scrollIntoView = sinon.spy() const instance = render( ) instance.update( ) t.false(el.scrollIntoView.calledOnce) }) test('Library renders', t => { const json = renderJSON(
Hello
}, ]} />
) t.snapshot(json) }) test('SidebarLayout renders', t => { const home = { key: '/', path: '/', name: 'index', props: {}, Component: () =>

Home

} const json = renderJSON(

About

} ]} />
) t.snapshot(json) }) const blacklist = { pre: true, MDXTag: true, components: true } Object.keys(scope) .filter(key => !blacklist[key]) .forEach(key => { test(`scope.${key} renders`, t => { const Component = scope[key] const json = renderJSON( ) t.snapshot(json) }) }) test('scope.pre renders children only', t => { const json = renderJSON( React.createElement(scope.pre, null, 'Hello') ) t.is(json, 'Hello') }) test('scope.a renders a plain link for absolute URLs', t => { const Link = scope.a const json = renderJSON( Hello ) t.is(json.props.href, 'http://example.com') t.snapshot(json) })