-
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.
Allow for Stubing LaunchDarkly in cypress environment (#48)
- Loading branch information
1 parent
1bff297
commit e37ab02
Showing
5 changed files
with
112 additions
and
53 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
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { default as VueLd } from './plugin'; | ||
export { default as VueLd, initialize } from './plugin'; | ||
export { default as ldRedirectMixin } from './mixins/ldRedirect'; | ||
export { default as LDRouteGuard } from './components/LDRouteGuard.vue'; |
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 |
---|---|---|
@@ -1,46 +1,72 @@ | ||
import * as LDClient from 'launchdarkly-js-client-sdk'; | ||
import { formatFlags } from './utils'; | ||
|
||
export default { | ||
install(Vue, options) { | ||
const { clientSideId, user, options: ldOptions, readyBeforeIdentify = true } = options; | ||
export const initialize = ({ clientSideId, user, ldOptions, readyBeforeIdentify }) => { | ||
const ldClient = LDClient.initialize(clientSideId, user, ldOptions); | ||
const $ld = { | ||
ldClient, | ||
identify({ newUser, hash, callback }, vueLdCallback) { | ||
return new Promise((r) => { | ||
this.ready = false; | ||
ldClient.identify(newUser, hash, callback).then(() => { | ||
this.ready = true; | ||
if (vueLdCallback) { | ||
const boundVueLdCallback = vueLdCallback.bind($ld); | ||
boundVueLdCallback(); | ||
} | ||
r(); | ||
}); | ||
}); | ||
}, | ||
flags: {}, | ||
ready: false, | ||
}; | ||
|
||
const ldClient = LDClient.initialize(clientSideId, user, ldOptions); | ||
ldClient.on('ready', () => { | ||
$ld.flags = formatFlags(ldClient.allFlags()); | ||
$ld.ready = readyBeforeIdentify; | ||
}); | ||
|
||
const $ld = Vue.observable({ | ||
ldClient, | ||
identify({ newUser, hash, callback }, vueLdCallback) { | ||
return new Promise((r) => { | ||
this.ready = false; | ||
ldClient.identify(newUser, hash, callback).then(() => { | ||
this.ready = true; | ||
if (vueLdCallback) { | ||
const boundVueLdCallback = vueLdCallback.bind($ld); | ||
boundVueLdCallback(); | ||
} | ||
r(); | ||
}); | ||
}); | ||
}, | ||
flags: {}, | ||
ready: false, | ||
}); | ||
ldClient.on('change', (changes) => { | ||
const flattenedFlags = Object.fromEntries( | ||
Object.keys(changes).map((key) => [key, changes[key].current]) | ||
); | ||
$ld.flags = { | ||
...$ld.flags, | ||
...formatFlags(flattenedFlags), | ||
}; | ||
}); | ||
return $ld; | ||
}; | ||
|
||
const stub = ({ flagsStub, readyBeforeIdentify }) => { | ||
return { | ||
identify() { | ||
this.ready = true; | ||
}, | ||
flags: flagsStub, | ||
ready: readyBeforeIdentify, | ||
}; | ||
}; | ||
|
||
export default { | ||
async install(Vue, options) { | ||
const { | ||
clientSideId, | ||
user, | ||
options: ldOptions, | ||
flagsStub, | ||
readyBeforeIdentify = true, | ||
} = options; | ||
|
||
ldClient.on('ready', () => { | ||
$ld.flags = formatFlags(ldClient.allFlags()); | ||
$ld.ready = readyBeforeIdentify; | ||
}); | ||
let $ld; | ||
|
||
ldClient.on('change', (changes) => { | ||
const flattenedFlags = Object.fromEntries( | ||
Object.keys(changes).map((key) => [key, changes[key].current]) | ||
); | ||
$ld.flags = { | ||
...$ld.flags, | ||
...formatFlags(flattenedFlags), | ||
}; | ||
}); | ||
if (flagsStub) { | ||
$ld = stub({ flagsStub, readyBeforeIdentify }); | ||
} else { | ||
$ld = initialize({ clientSideId, user, ldOptions, readyBeforeIdentify }); | ||
} | ||
// eslint-disable-next-line no-param-reassign | ||
Vue.prototype.$ld = $ld; | ||
Vue.prototype.$ld = Vue.observable($ld); | ||
}, | ||
}; |
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