import _, { forEach, has, noop } from 'lodash';
import React from 'react';
import assert from 'assert';
import { shallow } from 'enzyme';
import { common } from '../../util/generic-tests';
import { VerticalTabsDumb as VerticalTabs } from './VerticalTabs';
import { VerticalListMenuDumb as VerticalListMenu } from '../VerticalListMenu/VerticalListMenu';
//👇 Destructure any child components that we will need
const { Tab, Title } = VerticalTabs;
describe('VerticalTabs', () => {
common(VerticalTabs, {
exemptChildComponents: ['Tab', 'Title'] as any,
});
describe('render', () => {
it('should render', () => {
const wrapper = shallow(
One
One content
);
expect(wrapper).toMatchSnapshot();
});
});
describe('props', () => {
it('Tab as children', () => {
const wrapper = shallow(
Foo Content
Two
);
assert.equal(
wrapper.find('.lucid-VerticalTabs-content').text(),
'Foo Content'
);
});
it('Tab as props', () => {
const wrapper = shallow(
);
assert.equal(wrapper.find('.lucid-VerticalTabs-Tab').length, 2);
assert.equal(wrapper.find('.lucid-VerticalTabs-content').text(), 'Bert');
});
it('Tab as props with Title', () => {
const wrapper = shallow(
);
assert.equal(wrapper.find('.lucid-VerticalTabs-Tab').length, 2);
assert.equal(
wrapper.find('.lucid-VerticalTabs-Tab-is-active').children().text(),
'Coolest'
);
assert.equal(wrapper.find('.lucid-VerticalTabs-content').text(), 'Bert');
});
it('Title as props', () => {
const wrapper = shallow(
Yolo fo sho
Broyoyo
);
assert.equal(
wrapper.find('.lucid-VerticalTabs-Tab').first().children().text(),
'Froyo'
);
});
it('Title as children', () => {
const wrapper = shallow(
Froyo
Yolo fo sho
Broyoyo
);
assert.equal(
wrapper.find('.lucid-VerticalTabs-Tab').first().children().text(),
'Froyo'
);
});
it('selectedIndex', () => {
const wrapper = shallow(
Yuck
Yum
);
assert.equal(
wrapper.find('.lucid-VerticalTabs-Tab-is-active').children().text(),
'Slurpee'
);
assert.equal(wrapper.find('.lucid-VerticalTabs-content').text(), 'Yum');
});
it('Tab.isSelected', () => {
const wrapper = shallow(
Yuck
Yum
);
assert.equal(
wrapper.find('.lucid-VerticalTabs-Tab-is-active').children().text(),
'Slurpee'
);
assert.equal(wrapper.find('.lucid-VerticalTabs-content').text(), 'Yum');
});
it('last Tab.isSelected beats selectedIndex', () => {
const wrapper = shallow(
One content
Two content
Three content
);
assert.equal(
wrapper.find('.lucid-VerticalTabs-Tab-is-active').children().text(),
'Three'
);
assert.equal(
wrapper.find('.lucid-VerticalTabs-content').text(),
'Three content'
);
});
describe('onSelect', () => {
const onSelectMock: any = jest.fn();
let wrapper: any;
beforeEach(() => {
onSelectMock.mockClear();
wrapper = shallow(
One
Two
);
});
it('should pass props onto `VerticalListMenu`', () => {
wrapper.find(VerticalListMenu).props().onSelect('stuff');
expect(onSelectMock).toBeCalledTimes(1);
expect(onSelectMock.mock.calls[0][0]).toEqual('stuff');
});
});
describe('root pass throughs', () => {
let wrapper: any;
beforeEach(() => {
const props = {
Tab: One content,
Title:
One,
selectedIndex: 2,
onSelect: noop,
className: 'wut',
style: { marginRight: 10 },
initialState: { test: true },
callbackId: 1,
'data-testid': 10,
};
wrapper = shallow();
});
afterEach(() => {
wrapper.unmount();
});
it('passes through props not defined in `propTypes` to the root element.', () => {
const rootProps = wrapper.find('.lucid-VerticalTabs').props();
expect(wrapper.first().prop(['className'])).toContain('wut');
expect(wrapper.first().prop(['style'])).toMatchObject({
marginRight: 10,
});
expect(wrapper.first().prop(['data-testid'])).toBe(10);
// 'className' is plucked from the pass through object
// but still appears becuase it is also directly added to the root element as a prop
forEach(
['className', 'data-testid', 'style', 'children', 'Tab', 'Title'],
(prop) => {
expect(has(rootProps, prop)).toBe(true);
}
);
});
it('omits the props defined in `propTypes` (plus, in addition, `initialState`, and `callbackId`) from the root element', () => {
const rootProps = wrapper.find('.lucid-VerticalTabs').props();
forEach(
['selectedIndex', 'onSelect', 'initialState', 'callbackId'],
(prop) => {
expect(has(rootProps, prop)).toBe(false);
}
);
});
});
});
});