Skip to content

Commit

Permalink
fix: Expose FlagSwitcher only in browser context
Browse files Browse the repository at this point in the history
Exposing FlagSwitcher on node context was causing an error in Node
context because it's importing the flags browser implementation and
`localStorage` doesn't exist in this context.

Fixes #402
  • Loading branch information
drazik committed May 3, 2019
1 parent badb3c9 commit 2a4829b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
11 changes: 7 additions & 4 deletions packages/cozy-flags/src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/* global __ENABLED_FLAGS__ */

import isNode from 'detect-node'
export { default as FlagSwitcher } from './browser/FlagSwitcher'
const isNode = require('detect-node')

const flag = isNode
? require('./node/flag').default
: require('./browser/flag').default

if (!isNode) {
flag.FlagSwitcher = require('./browser/FlagSwitcher').default
}

/**
* Enables a list of flags
* @param {string[]} flagsToEnable
*/
export function enableFlags(flagsToEnable) {
function enableFlags(flagsToEnable) {
if (!Array.isArray(flagsToEnable)) {
return
}
Expand All @@ -25,4 +28,4 @@ if (typeof __ENABLED_FLAGS__ !== 'undefined') {

flag.enable = enableFlags

export default flag
module.exports = flag
16 changes: 8 additions & 8 deletions packages/cozy-flags/src/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import flag, { enableFlags } from '.'
import flag from '.'

describe('enableFlags', () => {
describe('enable', () => {
afterEach(() => {
flag.reset()
})

it('should do nothing if the parameter is not an array', () => {
enableFlags('blablabla')
enableFlags(42)
enableFlags(true)
enableFlags({})
enableFlags()
flag.enable('blablabla')
flag.enable(42)
flag.enable(true)
flag.enable({})
flag.enable()

expect(flag.list()).toEqual([])
})

it('should enable the flags if the parameter is an array', () => {
const flagsToEnable = ['hello', 'world']
enableFlags(flagsToEnable)
flag.enable(flagsToEnable)

expect(flag.list()).toEqual(flagsToEnable)
})
Expand Down

0 comments on commit 2a4829b

Please sign in to comment.