@bigtest/react

cleanup Promise

  • returns Promise Resolves after unmounting the component and clearing the current context

Cleans up any component that was mounted by previously calling mount, and clears the current context used by other helpers.

The teardown option provided to the previous mount function will be called when using this helper. If a promise is returned, the cleanup will not happen until after that promise resovles.

goBack

  • throws Error When setupAppForTesting was not called

Uses the history context setup during setupAppForTesting and calls the goBack method.

// must be called to setup the `history` context
setupAppForTesting(App)

// go to a route
visit('/someroute')

// go back
goBack()

goForward

  • throws Error When setupAppForTesting was not called

Uses the history context setup during setupAppForTesting and calls the goForward method.

// must be called to setup the `history` context
setupAppForTesting(App)

// go to a route, and back
visit('/someroute')
goBack()

// go back to `/someroute`
goForward()

location Object

  • returns Object Current history location object
  • throws Error When setupAppForTesting was not called

Uses the history context setup during setupAppForTesting and returns the current location.

// must be called to setup the `history` context
setupAppForTesting(App)
visit('/someroute')

location().pathname
//=> "/someroute"

mount component, options Promise

  • component Component The component to mount
  • options.mountId String The ID given to the insterted mounting node.
  • options.rootElement Node The root element the new node will be insterted into.
  • options.setup Function Called after cleaning up the previously mounted component but before mounting the new component. If a promise is returned, the component will not be mounted until after the promise resolves.
  • options.teardown Function Called when cleanup is used after mounting the component, or when mount is used again. If a promise is returned, the component will not be unmounted until after the promise resolves.
  • returns Promise Resolves after the component has been mounted into the newly inserted DOM node.

Mounts the component within a freshly inserted dom node. If there was a component previously mounted by this function, the cleanup helper is automatically used to safely unmount it and clear any existing context.

await mount(() => (
  <SomeComponent foo="bar">
    <SomeOtherComponent/>
  </SomeComponent>
))

The ID given to the mounting node can be customized by providing a mountId option, and where the node is inserted into can be controlled by providing a rootElement.

The setup hook is called after cleaning up the previously mounted component, and the new component is not mounted until after any resulting promise resolves.

The teardown hook is called on the next invokation of cleanup, either by using it directly, or by calling mount again. Cleanup will not complete until any optional promise returned from teardown resolves.

setupAppForTesting App, options Promise

  • App Component The Application component class to mount with additional properties
  • options Object Mounting options passed along to mount
  • options.props Object Additional props to pass to the App component when it renders
  • returns Promise Resolves with the app instance after it has been mounted in the DOM

Mounts an application component in the DOM with additional properties useful for testing. Using the props option, you may provide any custom properties to the app component.

setupAppForTesting(App, {
  props: { testing: true },
  setup: () => server = startMockServer(),
  teardown: () => server.shutdown()
})

If the application component accepts a history property, and one was not already provided via props, an in-memory history object is created which can then be used with routers such as React Router.

The history object is kept in a context which is used by the visit helpers to make it easy to navigate your app. The visit helpers will not work unless setupAppForTesting is called at least once.

// `history` must be defined as a prop type
App.propTypes = {
  history: PropTypes.object
}

// if this is not called, the visit helpers will throw errors
setupAppForTesting(App)

// forwards to `history.push`
visit('/someroute')
visit({ pathname: '/foo', search: '?bar' })

// other history helpers
goBack()
goForward()

Every time a new component is mounted via the setupAppForTesting or mount helpers, or when using the cleanup helper, the previous component is unmounted and the context is cleared.

setupAppForTesting(App)
visit('/someroute')
cleanup()

visit('/someroute')
//=> Error: undefined history context

visit location

  • location Object|String Argument for history.push()
  • throws Error When setupAppForTesting was not called

Uses the history context setup during setupAppForTesting and calls push with the provided location argument.

// must be called to setup the `history` context
setupAppForTesting(App)

// calls `history.push()`
visit('/someroute')
visit({ pathname: '/foo', search: '?bar' })