import _, { forEach, has, noop } from 'lodash';
import React from 'react';
import assert from 'assert';
import { common } from '../../util/generic-tests';
import { shallow } from 'enzyme';
import Validation from './Validation';
import TextField from '../TextField/TextField';
describe('Validation', () => {
common(Validation, {
getDefaultProps: () => ({
children: foo,
}),
});
describe('render', () => {
it('should render children', () => {
const content =
foo
;
const wrapper = shallow({content});
assert(wrapper.children().first().equals(content));
});
});
describe('props', () => {
describe('Error', () => {
let wrapper: any;
beforeEach(
() =>
(wrapper = shallow(
foo
))
);
it('should add error class', () => {
assert(wrapper.hasClass('lucid-Validation-is-error'));
});
it('should add error content', () => {
assert.equal(
wrapper.find('.lucid-Validation-error-content').text(),
'error'
);
});
});
describe('null Error', () => {
let wrapper: any;
beforeEach(
() =>
(wrapper = shallow(
foo
))
);
it('should not add error class', () => {
assert(!wrapper.hasClass('lucid-Validation-is-error'));
});
it('should not add error content', () => {
assert.equal(wrapper.find('.lucid-Validation-error-content').length, 0);
});
});
describe('child components', () => {
describe('Error', () => {
it('should render an error', () => {
const wrapper = shallow(
foo
Content
);
assert.equal(
wrapper.find('.lucid-Validation-error-content').text(),
'foo'
);
});
});
});
describe('pass throughs for root div component', () => {
let wrapper: any;
const className = 'wut';
beforeEach(() => {
const props = {
Error: 'test error',
children: ,
className,
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-Validation').props();
expect(wrapper.first().prop(['className'])).toContain(className);
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 is is are also directly passed on the root element as a prop
forEach(['className', 'data-testid', 'style', 'children'], (prop) => {
expect(has(rootProps, prop)).toBe(true);
});
});
it('omits the component specific props defined in `propTypes` (plus, in addition, `initialState`, and `callbackId`) from the root element', () => {
const rootProps = wrapper.find('.lucid-Validation').props();
forEach(['Error', 'initialState', 'callbackId'], (prop) => {
expect(has(rootProps, prop)).toBe(false);
});
});
});
});
});