Skip to content

Latest commit

 

History

History
47 lines (36 loc) · 1.07 KB

with-intent.md

File metadata and controls

47 lines (36 loc) · 1.07 KB

withIntent(Component)

  1. Overview
  2. Testing

Overview

A higher order component that can be used to connect a component deep within the component tree to its associated presenter. This is useful for requesting work from Presenters where passing down callbacks as props may otherwise be exhaustive.

import React from 'react'
import withIntent from 'microcosm/addons/with-intent'

const Button = withIntent(function ({ send }) {
  return (
    <button onClick={() => send('hello-world')}>
      Say hello!
    </button>
  )
})

Testing

withIntent relies on the context setup by a Presenter. When testing, this isn't always available. To work around this, Components wrapped in withIntent can accept send as a prop:

import React from 'react'
import test from 'ava'
import {mount} from 'enzyme'
import Button from 'prior-example'

test('it emits an intent when clicked', assert => {
  assert.plan(1)

  function assertion (intent) {
    assert.is(intent, 'hello-world')
  }

  mount(<Button send={assertion}) />
})