antd Checkboxコンポーネントでコンポーネントを使用していますCheckboxWidget。コンポーネントには次のcontextType定義があります。
static contextTypes: {
checkboxGroup: any
}
shallow酵素によるレンダリングを使用してテストしたいので、ブロック内withContextからヘルパーを使用しています。recomposebeforeEach
describe('Simple Checkbox Widget', () => {
beforeEach(() => {
withContext({
getChildContext: function () {
return {checkboxGroup: null}
},
childContextTypes: {
checkboxGroup: shape({
disabled: bool,
toggleOption: func,
value: array
})
}
})(CheckboxWidget)
})
})
ただし、次のようにテストを書くと:
it('renders a checkbox from Antd', () => {
const wrapper = shallow(<Subject />)
const actual = wrapper.find(AntdCheckbox).length
const expected = 1
expect(actual).toEqual(expected)
})
Checkboxウィジェットが見つからないため、テストが失敗していることに気付きました。レンダリングされたコンポーネントが次のようになるためだと思います。
<Subject />
が未定義であることwrapper.instance()がわかりました。使用しようとしましたが、どちらでも機能していないようです。Subjectwrapper.instance().childrenwrapper.divewrapper.instance()