I'm trying to test the generated class of a component that uses React CSS modules
Here is a sample Foo component:
import React from 'react';
import CSSModules from 'react-css-modules';
import styles from './Foo.css';
const Foo = ({ className }) => <div styleName={className} />;
Foo.propTypes = {
className: React.PropTypes.string.isRequired,
};
export default CSSModules(Foo, styles, { allowMultiple: true, errorWhenNotFound: false });
Test code (Foo.spec.jsx):
import React from 'react';
import { expect } from 'chai';
import { shallow } from 'enzyme';
import Foo from '../../../app/components/Foo';
describe('<Foo/>', () => {
it.only('should return a className prop as class value', () => {
const wrapper = shallow(<Foo className="bar" />);
console.log('DEBUG----->', wrapper.html());
});
});
Foo.css code:
.bar {
background-color: red;
}
And here is the test html output:
DEBUG-----> <div></div>
As you can see it generates an empty div without any class, so no assertion works.
Also I would like to bring up the fact that this only fails in the test, the component generates the expected html class in the web app generated html code.
Any idea how to fix this?
UPDATE
As suggested in the comment by fabio I've tried to remove the option errorWhenNotFound: false
Now the test gives the following error message:
Error: "bar" CSS module is undefined.
Which I don't understand since the bar class is defined in the Foo.css file.
Also I've found that if I debug the styles in the Foo component:
import styles from './Foo.css';
console.log('STYLES', styles);
It returns an empty output:
STYLES {}
Although both in this simplified example and in the full fledged code the component generates the expected html class in the web app generated html code with the corresponding styles working fine.
errorWhenNotFound: false(which you could also try to remove that to help debugging the issue).ignore-stylesoption, now that this is fixed, the following error message is being generated:SyntaxError: /var/www/web/app/components/Icon.css: Leading decorators must be attached to a class declarationBut at least it's a different problem now.