Skip to content

Commit

Permalink
fix(ssr): add type check for window (#205)
Browse files Browse the repository at this point in the history
Also add tests to avoid breaking SSR in the future
  • Loading branch information
Kent C. Dodds authored Sep 28, 2017
1 parent e3935f2 commit 67f53de
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
20 changes: 20 additions & 0 deletions other/ssr/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import Downshift from '../../../src'

test('does not throw an error when server rendering', () => {
expect(() => {
ReactDOMServer.renderToString(
<Downshift id="my-autocomplete-component">
{({getInputProps, getLabelProps}) => (
<div>
<label {...getLabelProps({htmlFor: 'my-autocomplete-input'})} />
<input {...getInputProps({id: 'my-autocomplete-input'})} />
</div>
)}
</Downshift>
)
}).not.toThrow()
})

/* eslint jsx-a11y/label-has-for:0 */
9 changes: 9 additions & 0 deletions other/ssr/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This is separate because the test environment is set via the config
// and we want most of our tests to run with jsdom, but we still want
// to make sure that the server rendering use case continues to work.
const jestConfig = require('kcd-scripts/config').jest

module.exports = Object.assign(jestConfig, {
roots: ['.'],
testEnvironment: 'node',
})
5 changes: 4 additions & 1 deletion src/downshift.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ class Downshift extends Component {
onStateChange: () => {},
onUserAction: () => {},
onChange: () => {},
environment: window,
environment:
typeof window === 'undefined' /* istanbul ignore next (ssr) */
? {}
: window,
}

// this is an experimental feature
Expand Down

0 comments on commit 67f53de

Please # to comment.