2017-09-11 26 views
0

私は、次のコンポーネントがあります(私は小道具値をチェックする必要はありません)私は(DOMで)実際の高さはBtnコンポーネントに渡されたものであることを確認することができるようにしたいJestを使ってDOMノードのスタイル属性を取得する方法は?

// Styled component. 
export const StyledBtn = styled.button` 
    height: ${(height) => height}; 
`; 

// Functional component. 
const Btn = ({ height }) => <StyledBtn height={height} />; 

export default Btn; 

を。

test('button renders at the correct height',() => { 
    const btn = mount(<Btn height={20}); 
    const node = findDOMNode(btn); 
    const height = node.getAttribute('height'); 
    expect(height).toEqual('20'); 
}); 

をしかし、テストは失敗します:

expect(received).toEqual(expected) 

    Expected value to equal: 
     "20" 
    Received: 
     null 

任意のアイデアはどのようにこれをテストするためにこれは私が見てテストを想定だろうかありますか?

+0

私は 'のためにあなたのコードがBtn'が間違っていると思う:あなたはそれ以内スタイルのコンポーネントをfindする必要があるので、また、mount()機能は、機能部品を実装しています。あなたは 'height'属性に小道具を渡しています。それは正しいはずです: 'const Btn = props =>;' – ericgio

+0

ええ、申し訳ありません、私の間違い私は正しい構文を表示する質問を編集しました。問題は正しいコードでは依然として存在します。 – JoeTidee

+0

にはEnzymeにあるような '.props()'関数がありますか?そうであれば、.props()。style'を実行することができます。 – AHB

答えて

0

jest-styled-componentsライブラリを使用してください。

import 'jest-styled-components'; 
... 
test('button renders at the correct height',() => { 
    const component = mount(<Btn height={20}); 
    const btn = component.find('button'); 
    expect(btn).toHaveStyleRule('height', '20px'); 
}); 
関連する問題