Add isClientSide helper for server-side rendering

This commit is contained in:
Jerko Steiner 2019-03-17 19:42:26 +05:00
parent 5f039f5f86
commit 3fae7adc4a
3 changed files with 33 additions and 0 deletions

View File

@ -2,3 +2,4 @@ export * from './ClientRenderer'
export * from './IClientConfig'
export * from './IRenderer'
export * from './IStoreFactory'
export * from './isClientSide'

View File

@ -0,0 +1,27 @@
import {isClientSide} from './isClientSide'
describe('isClientSide', () => {
describe('client', () => {
it('returns true when running in browser', () => {
expect(isClientSide()).toBe(true)
})
})
describe('server', () => {
const g: any = global
const window = g.window
beforeEach(() => {
delete g.window
})
afterEach(() => {
g.window = window
})
it('returns false when running on server', () => {
expect(isClientSide()).toBe(false)
})
})
})

View File

@ -0,0 +1,5 @@
export function isClientSide() {
return typeof window !== 'undefined' &&
typeof window.document !== 'undefined' &&
typeof window.document.createElement === 'function'
}