-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added bucketing key logic to shared-bucketing-as (#940)
- Loading branch information
Showing
12 changed files
with
296 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import { | |
} from '../bucketingImportHelper' | ||
import testData from '@devcycle/bucketing-test-data/json-data/testData.json' | ||
const { config, barrenConfig, configWithNullCustomData } = testData | ||
import { configWithBucketingKey } from '../helpers/configWithBucketingKey' | ||
|
||
import moment from 'moment' | ||
import * as uuid from 'uuid' | ||
|
@@ -1629,3 +1630,193 @@ describe('Client Data', () => { | |
setClientCustomDataJSON({}) | ||
}) | ||
}) | ||
|
||
describe('bucketingKey tests', () => { | ||
afterEach(() => cleanupSDK(sdkKey)) | ||
|
||
it('buckets a user with user_id if no bucketingKey', () => { | ||
const user = { | ||
user_id: 'test-id', | ||
customData: { | ||
favouriteFood: 'pizza', | ||
}, | ||
platformVersion: '1.1.2', | ||
email: '[email protected]', | ||
} | ||
const cWithBucketingKey = configWithBucketingKey('user_id') | ||
initSDK(sdkKey, cWithBucketingKey) | ||
|
||
const c = generateBucketedConfig(user) | ||
|
||
cleanupSDK(sdkKey) | ||
|
||
initSDK(sdkKey, config) | ||
const c2 = generateBucketedConfig(user) | ||
|
||
expect(c.features.feature5).not.toBeFalsy() | ||
expect(c.features.feature5.variationKey).toEqual( | ||
c2.features.feature5.variationKey, | ||
) | ||
}) | ||
|
||
it('buckets a user with custom bucketingKey', () => { | ||
const user = { | ||
country: 'U S AND A', | ||
user_id: 'pass_rollout', | ||
customData: { | ||
favouriteFood: 'pizza', | ||
}, | ||
privateCustomData: { | ||
favouriteDrink: 'coffee', | ||
}, | ||
platformVersion: '1.1.2', | ||
os: 'Android', | ||
email: '[email protected]', | ||
} | ||
const sameUserDifferentFood = { | ||
...user, | ||
customData: { | ||
favouriteFood: 'pasta', | ||
}, | ||
} | ||
const differentUserSameFood = { | ||
...sameUserDifferentFood, | ||
user_id: 'a_different_person', | ||
} | ||
const cWithBucketingKey = configWithBucketingKey('favouriteFood') | ||
initSDK(sdkKey, cWithBucketingKey) | ||
|
||
const c = generateBucketedConfig(user) | ||
const cSameUserDifferentFood = generateBucketedConfig( | ||
sameUserDifferentFood, | ||
) | ||
const cDifferentUserSameFood = generateBucketedConfig( | ||
differentUserSameFood, | ||
) | ||
|
||
expect(c.features.feature5.variationKey).not.toEqual( | ||
cSameUserDifferentFood.features.feature5.variationKey, | ||
) | ||
expect(cSameUserDifferentFood.features.feature5.variationKey).toEqual( | ||
cDifferentUserSameFood.features.feature5.variationKey, | ||
) | ||
}) | ||
|
||
it('buckets a user with custom bucketingKey from privateCustomData', () => { | ||
const user = { | ||
country: 'U S AND A', | ||
user_id: 'pass_rollout', | ||
privateCustomData: { | ||
favouriteFood: 'pizza', | ||
}, | ||
platformVersion: '1.1.2', | ||
os: 'Android', | ||
email: '[email protected]', | ||
} | ||
const sameUserDifferentFood = { | ||
...user, | ||
privateCustomData: { | ||
favouriteFood: 'pasta', | ||
}, | ||
} | ||
const differentUserSameFood = { | ||
...sameUserDifferentFood, | ||
user_id: 'a_different_person', | ||
} | ||
const cWithBucketingKey = configWithBucketingKey('favouriteFood') | ||
initSDK(sdkKey, cWithBucketingKey) | ||
|
||
const c = generateBucketedConfig(user) | ||
const cSameUserDifferentFood = generateBucketedConfig( | ||
sameUserDifferentFood, | ||
) | ||
const cDifferentUserSameFood = generateBucketedConfig( | ||
differentUserSameFood, | ||
) | ||
|
||
expect(c.features.feature5.variationKey).not.toEqual( | ||
cSameUserDifferentFood.features.feature5.variationKey, | ||
) | ||
expect(cSameUserDifferentFood.features.feature5.variationKey).toEqual( | ||
cDifferentUserSameFood.features.feature5.variationKey, | ||
) | ||
}) | ||
|
||
it('buckets a user with custom number bucketingKey', () => { | ||
const user = { | ||
country: 'U S AND A', | ||
user_id: 'pass_rollout', | ||
privateCustomData: { | ||
favouriteNumber: 610, | ||
}, | ||
platformVersion: '1.1.2', | ||
os: 'Android', | ||
email: '[email protected]', | ||
} | ||
const sameUserDifferentNum = { | ||
...user, | ||
privateCustomData: { | ||
favouriteNumber: 52900, | ||
}, | ||
} | ||
const differentUserSameNum = { | ||
...sameUserDifferentNum, | ||
user_id: 'a_different_person', | ||
} | ||
const cWithBucketingKey = configWithBucketingKey('favouriteNumber') | ||
initSDK(sdkKey, cWithBucketingKey) | ||
|
||
const c = generateBucketedConfig(user) | ||
const cSameUserDifferentNum = | ||
generateBucketedConfig(sameUserDifferentNum) | ||
const cDifferentUserSameNum = | ||
generateBucketedConfig(differentUserSameNum) | ||
|
||
expect(c.features.feature5.variationKey).not.toEqual( | ||
cSameUserDifferentNum.features.feature5.variationKey, | ||
) | ||
expect(cSameUserDifferentNum.features.feature5.variationKey).toEqual( | ||
cDifferentUserSameNum.features.feature5.variationKey, | ||
) | ||
}) | ||
|
||
it('buckets a user with custom boolean bucketingKey', () => { | ||
const user = { | ||
country: 'U S AND A', | ||
user_id: 'pass_rollout', | ||
privateCustomData: { | ||
signed_up: true, | ||
}, | ||
platformVersion: '1.1.2', | ||
os: 'Android', | ||
email: '[email protected]', | ||
} | ||
const sameUserDifferentBool = { | ||
...user, | ||
privateCustomData: { | ||
signed_up: false, | ||
}, | ||
} | ||
const differentUserSameBool = { | ||
...sameUserDifferentBool, | ||
user_id: 'a_different_person', | ||
} | ||
const cWithBucketingKey = configWithBucketingKey('signed_up') | ||
initSDK(sdkKey, cWithBucketingKey) | ||
|
||
const c = generateBucketedConfig(user) | ||
const cSameUserDifferentBool = generateBucketedConfig( | ||
sameUserDifferentBool, | ||
) | ||
const cDifferentUserSameBool = generateBucketedConfig( | ||
differentUserSameBool, | ||
) | ||
|
||
expect(c.features.feature5.variationKey).not.toEqual( | ||
cSameUserDifferentBool.features.feature5.variationKey, | ||
) | ||
expect(cSameUserDifferentBool.features.feature5.variationKey).toEqual( | ||
cDifferentUserSameBool.features.feature5.variationKey, | ||
) | ||
}) | ||
}) |
17 changes: 17 additions & 0 deletions
17
lib/shared/bucketing-assembly-script/__tests__/helpers/configWithBucketingKey.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { config } from '@devcycle/bucketing-test-data/json-data/testData.json' | ||
|
||
export function configWithBucketingKey(bucketingKey: string): unknown { | ||
return { | ||
...config, | ||
features: config.features.map((feature) => ({ | ||
...feature, | ||
configuration: { | ||
...feature.configuration, | ||
targets: feature.configuration.targets.map((target) => ({ | ||
...target, | ||
bucketingKey, | ||
})), | ||
}, | ||
})), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.