Skip to content

Commit

Permalink
[styled-engine] Fix GlobalStyles not to throw when no theme is availa…
Browse files Browse the repository at this point in the history
…ble (#24671)
  • Loading branch information
mnajdova committed Jan 28, 2021
1 parent 1bd1e64 commit 2c42921
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import PropTypes from 'prop-types';
import { createGlobalStyle } from 'styled-components';

function isEmpty(obj) {
return Object.keys(obj).length === 0;
return obj === undefined || obj === null || Object.keys(obj).length === 0;
}

const GlobalStyles = createGlobalStyle((props) => {
const { styles, defaultTheme } = props;
const { styles, defaultTheme = {} } = props;

if (typeof styles === 'function') {
return styles(isEmpty(props.theme) ? defaultTheme : props.theme);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ describe('GlobalStyles', () => {
});
});

it('should not throw if no theme is available', () => {
expect(() =>
render(
<GlobalStyles
defaultTheme={{ color: 'rgb(0, 0, 255)' }}
styles={(theme) => ({ span: { color: theme.color } })}
/>,
),
).not.to.throw();
});

it('should give presedence to styled()', function test() {
if (/jsdom/.test(window.navigator.userAgent)) this.skip();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import PropTypes from 'prop-types';
import { Global } from '@emotion/react';

function isEmpty(obj) {
return Object.keys(obj).length === 0;
return obj === undefined || obj === null || Object.keys(obj).length === 0;
}

export default function GlobalStyles(props) {
const { styles, defaultTheme } = props;
const { styles, defaultTheme = {} } = props;

const globalStyles =
typeof styles === 'function'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ describe('GlobalStyles', () => {
});
});

it('should not throw if no theme is available', () => {
expect(() =>
render(
<GlobalStyles
defaultTheme={{ color: 'rgb(0, 0, 255)' }}
styles={(theme) => ({ span: { color: theme.color } })}
/>,
),
).not.to.throw();
});

it('should give presedence to styled()', function test() {
if (/jsdom/.test(window.navigator.userAgent)) this.skip();

Expand Down

0 comments on commit 2c42921

Please sign in to comment.