I am new to Unit testing. I am trying to run a test but I keep getting the following error :
React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. I ran with { import Headers } and without {} and I keep getting the same error.
This is the current test.
import React from 'react';
import { Headers } from './Headers';
import { configure} from 'enzyme';
import Adapter from 'enzyme-adapter-react-15';
import * as renderer from 'react-test-renderer';
configure({adapter: new Adapter()});
describe('Headers', () => {
let tree;
let baseProps;
let mockauthKeyValues;
let mockheaderKeyValues = {
type: "TEST1",
defaultData : "test",
};
let mockaddNewKeyValue;
beforeEach(() => {
baseProps = { // assing all of the props into MOCK
authKeyValues: mockauthKeyValues,
headerKeyValues: mockheaderKeyValues,
addNewKeyValue: mockaddNewKeyValue
}
})
it ('should render without a authkeyvalues',() => {
baseProps = {
...baseProps,
authKeyValues: {},
};
tree = renderer.create(<Headers {...baseProps } />)
let treeJson = tree.toJSON();
expect(treeJson).toMatchSnapshot();
tree.unmount()
});
it ('should render without headerKeyValues',() => {
baseProps = {
...baseProps,
headerKeyValues: {},
};
tree = renderer.create(<Headers {...baseProps } />)
let treeJson = tree.toJSON();
expect(treeJson).toMatchSnapshot();
tree.unmount()
});
it ('should render without addNewKeyValue',() => {
baseProps = {
...baseProps,
addNewKeyValue: {},
};
tree = renderer.create(<Headers {...baseProps } />)
let treeJson = tree.toJSON();
expect(treeJson).toMatchSnapshot();
tree.unmount()
});
it('should render with all of the props', () => {
tree = renderer.create(<Headers {...baseProps} />)
let treeJson = tree.toJSON()
expect(treeJson).toMatchSnapshot();
tree.unmount()
});
});
Headers.js
import React, { PropTypes, Component } from 'react'
import KeyValue from '../Utils/KeyValuePair'
const shortid = require('shortid')
export class Headers extends Component {
componentDidMount () {
if (this.props.authKeyValues == null) {
this.setState({
useDefaultData: false
})
} else {
this.setState({
useDefaultData: true
})
}
}
generateKeyValues = () => {
if (this.props.headerKeyValues == null) {
return (
<KeyValue
id={shortid.generate()}
type='headers'
addNewKeyValue={this.props.addNewKeyValue}
/>
)
} else {
let defaultKeyValues = Object.keys(this.props.headerKeyValues).map((headerKey, idx) => {
return (
<KeyValue
key={headerKey}
id={headerKey}
type={this.props.headerKeyValues[headerKey].type}
addNewKeyValue={this.props.addNewKeyValue}
defaultData={this.props.headerKeyValues[headerKey]}
/>
)
})
defaultKeyValues.push(
<KeyValue
id={shortid.generate()}
type='headers'
addNewKeyValue={this.props.addNewKeyValue}
/>
)
return defaultKeyValues
}
}
render () {
return (
<div>
{this.generateKeyValues()}
</div>
)
}
}
I expected the test to pass by getting all the props called correct.