Skip to content

Commit

Permalink
[sc-118386] - Adds proper support to MVT experiments (#83)
Browse files Browse the repository at this point in the history
* Adds proper support to MVT experiments and simplifies the usage of Optimizely's SDK after update to >v4.5

* Fix tests
  • Loading branch information
vlacerda authored Jun 19, 2024
1 parent fc32e5b commit 5e7f8ae
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 120 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-phones-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'opticks': minor
---

Adds support to MVT tests and simplify the code in the Optimizely integration to abstract the difference between rollouts and MVT/AB
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,25 @@ export const createInstanceMock = jest.fn(() => ({
notificationCenter: {
addNotificationListener: addNotificationListenerMock
},
activate: activateMock,
createUserContext: optimizelyUserContextMock
}))

export const decideMock = jest.fn((toggleKey) => ({
enabled: toggleKey === 'foo'
}))
export const decideMock = jest.fn((toggleKey) => {
switch (toggleKey) {
case 'foo':
return {variationKey: 'b'}
case 'bax':
return {variationKey: 'c'}
default:
return {variationKey: undefined}
}
})
export const optimizelyUserContextMock = jest.fn(() => ({
decide: decideMock
}))

export const isFeatureEnabledMock = jest.fn((toggleId) => toggleId === 'foo')

export const activateMock = jest.fn((toggleId, userId) => {
const shouldReturnB =
(toggleId === 'foo' && userId === 'fooBSide') ||
(toggleId === 'bar' && userId === 'barBSide')

return shouldReturnB && 'b'
})

const originalModule = jest.requireActual('@optimizely/optimizely-sdk')

const mock = {
Expand Down
37 changes: 9 additions & 28 deletions packages/lib/src/integrations/optimizely.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import Optimizely, {
// @ts-expect-error
isFeatureEnabledMock,
// @ts-expect-error
activateMock,
// @ts-expect-error
decideMock,
// @ts-expect-error
optimizelyUserContextMock
Expand All @@ -44,16 +42,12 @@ const testAudienceSegmentationCacheBusting = (toggleFn, fn) => {

toggleFn('foo', 'a', 'b', 'c') // call 1
expect(fn).toHaveBeenCalledTimes(1)
expect(fn).toHaveBeenCalledWith('foo', 'fooBSide', {
foo: 'baz'
})
expect(fn).toHaveBeenCalledWith('foo')
toggleFn('foo', 'a', 'b', 'c') // cached
toggleFn('bar', 'a', 'b', 'c') // call 2
toggleFn('bar', 'a', 'b', 'c') // cached
expect(fn).toHaveBeenCalledTimes(2)
expect(fn).toHaveBeenCalledWith('bar', 'fooBSide', {
foo: 'baz'
})
expect(fn).toHaveBeenCalledWith('bar')
})
}

Expand Down Expand Up @@ -82,7 +76,7 @@ describe('Optimizely Integration', () => {
expect(addNotificationListenerMock).toHaveBeenCalledWith(
NOTIFICATION_TYPES.DECISION,
// 'DECISION:type, userId, attributes, decisionInfo',
activateHandler
expect.any(Function)
)
})
})
Expand Down Expand Up @@ -141,14 +135,9 @@ describe('Optimizely Integration', () => {

it('Forwards toggle reading and audienceSegmentationAttributes to Optimizely', () => {
toggle('foo', 'a', 'b', 'c')
expect(activateMock).toHaveBeenCalledWith('foo', 'fooBSide', {})
toggle('foo')
expect(decideMock).toHaveBeenCalledWith(
'foo'
)
expect(optimizelyUserContextMock).toHaveBeenCalledWith(
'fooBSide', {}
)
expect(decideMock).toHaveBeenCalledWith('foo')
expect(optimizelyUserContextMock).toHaveBeenCalledWith('fooBSide', {})
})
})

Expand All @@ -166,15 +155,6 @@ describe('Optimizely Integration', () => {

it('Forwards toggle reading and audienceSegmentationAttributes to Optimizely', () => {
toggle('foo', 'a', 'b', 'c')
expect(activateMock).toHaveBeenCalledWith('foo', 'fooBSide', {
thisWillNotBeOverwritten: 'foo',
deviceType: 'mobile',
isLoggedIn: false
})
})

it('Forwards toggle reading and audienceSegmentationAttributes to Optimizely', () => {
toggle('foo')
expect(optimizelyUserContextMock).toHaveBeenCalledWith('fooBSide', {
thisWillNotBeOverwritten: 'foo',
deviceType: 'mobile',
Expand All @@ -192,7 +172,8 @@ describe('Optimizely Integration', () => {

it('Forwards correct audience segmentation attributes', () => {
toggle('foo', 'a', 'b', 'c')
expect(activateMock).toHaveBeenCalledWith('foo', 'fooBSide', {

expect(optimizelyUserContextMock).toHaveBeenCalledWith('fooBSide', {
valueAfterReset: true
})

Expand All @@ -208,7 +189,7 @@ describe('Optimizely Integration', () => {
})
})

testAudienceSegmentationCacheBusting(toggle, activateMock)
testAudienceSegmentationCacheBusting(toggle, decideMock)

it("Returns Optimizely's value when no arguments supplied", () => {
// maps to a, b, c
Expand Down Expand Up @@ -252,7 +233,7 @@ describe('Optimizely Integration', () => {
})

it('makes sure Toggles return defaults if forced values are of wrong type', () => {
expect(toggle('baz', 'a', 'b', 'c')).toEqual('a')
expect(toggle('boz', 'a', 'b', 'c')).toEqual('a')
})

describe('Clearing forced toggles', () => {
Expand Down
Loading

0 comments on commit 5e7f8ae

Please sign in to comment.