Skip to content

Commit

Permalink
Code coverage improvements and bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
airdeploy-eng committed Dec 17, 2020
1 parent 1a747d7 commit 490ecb1
Show file tree
Hide file tree
Showing 9 changed files with 525 additions and 96 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flagger",
"version": "3.0.12",
"version": "3.0.13",
"description": "An open source JavaScript SDK for feature flagging (feature gating, feature toggles) for Node.js and Javascript applications",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
Expand Down
78 changes: 78 additions & 0 deletions src/Core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,22 @@ describe("Individual Blacklist test, 'new-signup-flow' flag is in blacklist for
})
})

describe('setEntity tests', () => {
const flagCodename = 'new-signup-flow'
const entity = {type: 'User', id: '90843823'}

test('isEnabled should be true', () => {
core.setEntity(entity)
const flag = core.evaluateFlagProperties(flagCodename)
expect(flag.isEnabled).toEqual(true)
expect(flag.reason).toEqual(Reason.INDIVIDUAL_WHITELIST)
expect(flag.hashkey).not.toEqual(undefined)
expect(core.getEntity()).toBe(entity)
core.setEntity(undefined)
expect(core.getEntity()).toBe(undefined)
})
})

describe("Individual Whitelist test, 'new-signup-flow' flag is in whitelist for type:User; ids:90843823,14612844,64741829; variation: enabled", () => {
const flagCodename = 'new-signup-flow'
const entity = {type: 'User', id: '90843823'}
Expand Down Expand Up @@ -224,6 +240,31 @@ describe("Individual Whitelist test, 'new-signup-flow' flag is in whitelist for
expect(variation.payload).not.toBeNull()
expect(variation.payload.showButtons).toEqual(true)
})

test('flag without variations should return empty variation', () => {
const flagResult = core.evaluateFlagProperties('flag-without-variation', {
id: '90843823',
type: 'User'
})
expect(flagResult.isEnabled).toBeTruthy()
expect(flagResult.isSampled).toBeFalsy()
expect(flagResult.payload).toEqual({})
expect(flagResult.variation.codename).toEqual('off')
})

test('invalid whitelist variation should return empty variation', () => {
const flagResult = core.evaluateFlagProperties(
'invalid-whitelist-variation',
{
id: '90843823',
type: 'User'
}
)
expect(flagResult.isEnabled).toBeTruthy()
expect(flagResult.isSampled).toBeFalsy()
expect(flagResult.payload).toEqual({})
expect(flagResult.variation.codename).toEqual('off')
})
})

describe("Group Blacklist test, 'premium-support' flag has group blacklist for Company:52272353", () => {
Expand Down Expand Up @@ -303,6 +344,21 @@ describe("Group whitelist test, 'enterprise-dashboard' flag has group whitelist
expect(variation.payload).not.toBeNull()
expect(variation.payload.newFeature).toEqual('on')
})

test('flag without variations should return empty variation', () => {
const flagResult = core.evaluateFlagProperties('flag-without-variation', {
id: 'any',
type: 'entity',
group: {
id: '90843823',
type: 'User'
}
})
expect(flagResult.isEnabled).toBeTruthy()
expect(flagResult.isSampled).toBeFalsy()
expect(flagResult.payload).toEqual({})
expect(flagResult.variation.codename).toEqual('off')
})
})

describe('individual policy always beats group policy', () => {
Expand Down Expand Up @@ -382,6 +438,28 @@ describe('Individual subpopulation sample, Single subpopulation of type "User",
expect(variation.payload).not.toBeNull()
expect(variation.payload.showButtons).toEqual(true)
})

it('flagConfig without variation should return empty variation', () => {
const flagResult = core.evaluateFlagProperties('flag-without-variation', {
id: '1',
type: 'User'
})
expect(flagResult.isEnabled).toBeTruthy()
expect(flagResult.isSampled).toBeTruthy()
expect(flagResult.payload).toEqual({})
expect(flagResult.variation.codename).toEqual('off')
})

it('flagConfig with wrong probability should return empty variation', () => {
const flagResult = core.evaluateFlagProperties('invalid-probability', {
id: '1',
type: 'User'
})
expect(flagResult.isEnabled).toBeTruthy()
expect(flagResult.isSampled).toBeTruthy()
expect(flagResult.payload).toEqual({})
expect(flagResult.variation.codename).toEqual('off')
})
})

describe('Group subpopulation sample, Single subpopulation of type "User", filtered for Users in Japan or France, sampled at 30%', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export class Core implements ICoreInterface {
return {
hashkey: flagFromConfig.hashkey,
codename,
isEnabled: !flagFromConfig.killSwitchEngaged,
isEnabled: true,
isSampled: true,
payload: variation ? variation.payload : {},
variation,
Expand Down Expand Up @@ -300,11 +300,11 @@ export class Core implements ICoreInterface {
}
const allocationHashKey = flagCodename + entityId + entityType
const allocationHashedPercentage = getHashedValue(allocationHashKey)
let cummulativeSum = 0
let cumulativeSum = 0

for (const variation of variations) {
cummulativeSum += variation.probability
if (cummulativeSum > allocationHashedPercentage) {
cumulativeSum += variation.probability
if (cumulativeSum > allocationHashedPercentage) {
return variation
}
}
Expand Down
Loading

0 comments on commit 490ecb1

Please sign in to comment.