Skip to content

Commit

Permalink
Use process.env.NODE_ENV for dev warning
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanmarks committed Jun 21, 2019
1 parent 2720ff6 commit 980052f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
53 changes: 30 additions & 23 deletions src/styles/themer/__tests__/withTheme.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,43 @@ import renderer from 'react-test-renderer'
import withTheme from '../withTheme'
import { defaultTheme, themePropTypes } from '../utils'

const TestComponent = withTheme(
class extends React.Component {
// eslint-disable-line react/prefer-stateless-function
render() {
return (
<div
style={{
backgroundColor: this.props.snacksTheme.colors.primaryBackground,
}}
>
Hello
</div>
)
}
}
)
// eslint-disable-next-line react/prefer-stateless-function
class Test extends React.Component {
static propTypes = { snacksTheme: themePropTypes }

TestComponent.propTypes = { snacksTheme: themePropTypes }
render() {
return (
<div
style={{
backgroundColor: this.props.snacksTheme.colors.primaryBackground,
}}
>
Hello
</div>
)
}
}

it('renders without error with default theme', () => {
const TestComponent = withTheme(Test)
const tree = renderer.create(<TestComponent />).toJSON()
expect(tree).toMatchSnapshot()
})

describe('while in production mode', () => {
beforeAll(() => {
global.__DEV__ = false
let TestComponent
let backupEnv

beforeEach(() => {
backupEnv = { ...process.env }

jest.resetModules()
process.env.NODE_ENV = 'production'
TestComponent = require('../withTheme').default(Test)
})

afterAll(() => {
global.__DEV__ = true
afterEach(() => {
process.env = backupEnv
})

it('can be overridden', () => {
Expand All @@ -60,9 +66,8 @@ describe('while in production mode', () => {
})
})

describe('while in development mode', () => {
describe('while not in production mode', () => {
beforeEach(() => {
global.__DEV__ = true
// we can stop swallowing these when this is finished
// https://github.com/facebook/react/issues/11098
jest.spyOn(console, 'error')
Expand All @@ -72,7 +77,9 @@ describe('while in development mode', () => {
afterEach(() => {
global.console.error.mockRestore()
})

it('throws an error on invalid snacksTheme', () => {
const TestComponent = withTheme(Test)
const createTree = () => renderer.create(<TestComponent snacksTheme={1} />).toJSON()
expect(() => createTree()).toThrow()
})
Expand Down
3 changes: 3 additions & 0 deletions src/styles/themer/withTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import React, { Component } from 'react'
import themer from './index'
import { cleanConfig, themePropTypes } from './utils'

// eslint-disable-next-line no-underscore-dangle
const __DEV__ = process.env.NODE_ENV !== 'production'

const isStateless = component => {
return !component.prototype.render
}
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = {
},
plugins: [
new webpack.DefinePlugin({
__DEV__: JSON.stringify(true),
'process.env.NODE_ENV': JSON.stringify('development'),
}),
anaylzerEnabled && new BundleAnalyzerPlugin(), // optionally anaylze if flag passed in
].filter(i => !!i), // filter out false items
Expand Down
2 changes: 1 addition & 1 deletion webpack.release.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
},
plugins: [
new webpack.DefinePlugin({
__DEV__: JSON.stringify(false),
'process.env.NODE_ENV': JSON.stringify('production'),
}),
],
output: {
Expand Down

0 comments on commit 980052f

Please sign in to comment.