@navinc/base-react-components
Version:
Nav's Pattern Library
132 lines (102 loc) • 3.51 kB
JavaScript
import React from 'react'
import { shallow } from 'enzyme'
import { Card, CardHeader, Footer } from './'
import KebabMenu from './parts/kebab-menu'
describe('Base Components: Card', () => {
let shallowWrapper
let instance
const event = { stopPropagation: () => {} }
beforeEach(() => {
shallowWrapper = shallow(<Card />)
instance = shallowWrapper.instance()
})
describe('toggleKebabMenu', () => {
it('updates state to hide/show KebabMenu', () => {
expect(shallowWrapper.state().isKebabMenuVisible).toBeFalsy()
instance.toggleKebabMenu(event)
expect(shallowWrapper.state().isKebabMenuVisible).toBeTruthy()
})
})
describe('togglehideCard', () => {
it('updates state to hide/show Card', () => {
expect(shallowWrapper.state().isCardHidden).toBeFalsy()
instance.togglehideCard(event)
expect(shallowWrapper.state().isCardHidden).toBeTruthy()
})
})
describe('dismissCard', () => {
it('updates state to hide card', () => {
expect(shallowWrapper.state().isCardDismissed).toBeFalsy()
instance.dismissCard(event)
expect(shallowWrapper.state().isCardDismissed).toBeTruthy()
})
})
describe('setHeight', () => {
it('doesnt update the state if shouldNotAnimate is true', (done) => {
shallowWrapper.setProps({
shouldNotAnimate: true,
})
shallowWrapper.setState({
contentHeight: null,
})
instance.setHeight()
setTimeout(() => {
expect(instance.state.contentHeight).toBe(null)
done()
}, 250)
})
it('updates state to the rendered height of the card', (done) => {
shallowWrapper.setState({
contentHeight: null,
})
instance.setHeight()
setTimeout(() => {
expect(shallowWrapper.state().contentHeight).toEqual(expect.any(Number))
done()
}, 300)
})
})
describe('render', () => {
it('renders the CardHeader if there is cardLabel', () => {
shallowWrapper.setProps({
cardLabel: 'asdf',
})
expect(shallowWrapper.find(CardHeader).exists()).toBe(true)
})
it('doesnt render the CardHeader if there is no cardLabel', () => {
expect(shallowWrapper.find(CardHeader).exists()).toBe(false)
})
it('renders the KebabMenu if isKebabMenuVisible = true', () => {
shallowWrapper.setState({
isKebabMenuVisible: true,
})
expect(shallowWrapper.find(KebabMenu).exists()).toBe(true)
})
it('doesnt render the KebabMenu if shouldShowKebabMenu = false', () => {
expect(shallowWrapper.find(KebabMenu).exists()).toBe(false)
})
it('renders the Footer if hasfooter = true and onNext exists', () => {
shallowWrapper.setProps({
hasfooter: true,
onNext: () => {},
})
expect(shallowWrapper.find(Footer).exists()).toBe(true)
})
it('doesnt render the HeightController if controlledHeight = false', () => {
expect(shallowWrapper.find(Footer).exists()).toBe(false)
})
})
describe('componentDidMount', () => {
it('calls setHeight when height changes', () => {
instance.setHeight = jest.fn()
shallowWrapper.setState({ contentHeight: 5 })
instance.componentDidMount()
expect(instance.setHeight).toHaveBeenCalled()
})
it(`height is set on mount`, () => {
instance.setHeight = jest.fn()
instance.componentDidMount()
expect(instance.setHeight).toHaveBeenCalled()
})
})
})