Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning for non styled components #2070

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/utils/flatten.js
@@ -1,4 +1,7 @@
// @flow

import * as ReactIs from 'react-is';
import getComponentName from './getComponentName';
import isFunction from './isFunction';
import isPlainObject from './isPlainObject';
import isStyledComponent from './isStyledComponent';
Expand Down Expand Up @@ -55,6 +58,18 @@ export default function flatten(chunk: any, executionContext: ?Object, styleShee
/* Either execute or defer the function */
if (isFunction(chunk)) {
if (executionContext) {
if (process.env.NODE_ENV !== 'production') {
/* Warn if not referring styled component */
// eslint-disable-next-line new-cap
if (ReactIs.isElement(new chunk(executionContext))) {
console.warn(
`${getComponentName(
chunk
)} is not a styled component and cannot be referred to via component selector. See https://www.styled-components.com/docs/advanced#referring-to-other-components for more details.`
);
}
}

return flatten(chunk(executionContext), executionContext, styleSheet);
} else return chunk;
}
Expand Down
21 changes: 21 additions & 0 deletions src/utils/test/flatten.test.js
@@ -1,5 +1,8 @@
// @flow
import React from 'react';
import flatten from '../flatten';
import styled from '../../constructors/styled';
import TestRenderer from 'react-test-renderer';

describe('flatten', () => {
it('doesnt merge strings', () => {
Expand Down Expand Up @@ -99,4 +102,22 @@ describe('flatten', () => {
expect(flatten(['foo', func], { bool: true })).toEqual(['foo', 'static', 'bar']);
expect(flatten(['foo', func], { bool: false })).toEqual(['foo', 'static', 'baz']);
});
it('warns to refer styled-components', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you change this test name to "warns if trying to interpolate a normal React component"

const Foo = ({ className }) => <div className={className}>hello there!</div>

const Bar = styled.div`
${Foo}: {
background-color: red;
};
`;

console.warn = jest.fn();
global.console = { warn: jest.fn() };

expect(() => {
flatten(TestRenderer.create(<Bar />));
}).toThrowError();

expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('Foo is not a styled component and cannot be referred to via component selector. See https://www.styled-components.com/docs/advanced#referring-to-other-components for more details.'));
});
});