-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add typed variation methods. #288
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,6 +160,99 @@ describe('given an LDClient with test data', () => { | |
const valueB = await client.variation('my-feature-flag-1', userContextObject, 'default'); | ||
expect(valueB).toEqual(true); | ||
}); | ||
|
||
it('evaluates with jsonVariation', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This just addresses the problem with |
||
td.update(td.flag('flagkey').booleanFlag().on(true)); | ||
const boolRes: boolean = (await client.jsonVariation('flagkey', defaultUser, false)) as boolean; | ||
expect(boolRes).toBe(true); | ||
|
||
td.update(td.flag('flagkey').valueForAll(62)); | ||
const numericRes: number = (await client.jsonVariation( | ||
'flagkey', | ||
defaultUser, | ||
false, | ||
)) as number; | ||
expect(numericRes).toBe(62); | ||
|
||
td.update(td.flag('flagkey').valueForAll('potato')); | ||
const stringRes: string = (await client.jsonVariation('flagkey', defaultUser, false)) as string; | ||
expect(stringRes).toBe('potato'); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Below are all the typed method calls. |
||
it('evaluates an existing boolean flag', async () => { | ||
td.update(td.flag('flagkey').booleanFlag().on(true)); | ||
expect(await client.boolVariation('flagkey', defaultUser, false)).toEqual(true); | ||
}); | ||
|
||
it('it uses the default value when a boolean variation is for a flag of the wrong type', async () => { | ||
td.update(td.flag('flagkey').valueForAll('potato')); | ||
expect(await client.boolVariation('flagkey', defaultUser, false)).toEqual(false); | ||
}); | ||
|
||
it('evaluates an existing numeric flag', async () => { | ||
td.update(td.flag('flagkey').booleanFlag().valueForAll(18)); | ||
expect(await client.numberVariation('flagkey', defaultUser, 36)).toEqual(18); | ||
}); | ||
|
||
it('it uses the default value when a numeric variation is for a flag of the wrong type', async () => { | ||
td.update(td.flag('flagkey').valueForAll('potato')); | ||
expect(await client.numberVariation('flagkey', defaultUser, 36)).toEqual(36); | ||
}); | ||
|
||
it('evaluates an existing string flag', async () => { | ||
td.update(td.flag('flagkey').booleanFlag().valueForAll('potato')); | ||
expect(await client.stringVariation('flagkey', defaultUser, 'default')).toEqual('potato'); | ||
}); | ||
|
||
it('it uses the default value when a string variation is for a flag of the wrong type', async () => { | ||
td.update(td.flag('flagkey').valueForAll(8)); | ||
expect(await client.stringVariation('flagkey', defaultUser, 'default')).toEqual('default'); | ||
}); | ||
|
||
it('evaluates an existing boolean flag with detail', async () => { | ||
td.update(td.flag('flagkey').booleanFlag().on(true)); | ||
const res = await client.boolVariationDetail('flagkey', defaultUser, false); | ||
expect(res.value).toEqual(true); | ||
expect(res.reason.kind).toBe('FALLTHROUGH'); | ||
}); | ||
|
||
it('it uses the default value when a boolean variation is for a flag of the wrong type with detail', async () => { | ||
td.update(td.flag('flagkey').valueForAll('potato')); | ||
const res = await client.boolVariationDetail('flagkey', defaultUser, false); | ||
expect(res.value).toEqual(false); | ||
expect(res.reason.kind).toEqual('ERROR'); | ||
expect(res.reason.errorKind).toEqual('WRONG_TYPE'); | ||
}); | ||
|
||
it('evaluates an existing numeric flag with detail', async () => { | ||
td.update(td.flag('flagkey').booleanFlag().valueForAll(18)); | ||
const res = await client.numberVariationDetail('flagkey', defaultUser, 36); | ||
expect(res.value).toEqual(18); | ||
expect(res.reason.kind).toBe('FALLTHROUGH'); | ||
}); | ||
|
||
it('it uses the default value when a numeric variation is for a flag of the wrong type with detail', async () => { | ||
td.update(td.flag('flagkey').valueForAll('potato')); | ||
const res = await client.numberVariationDetail('flagkey', defaultUser, 36); | ||
expect(res.value).toEqual(36); | ||
expect(res.reason.kind).toEqual('ERROR'); | ||
expect(res.reason.errorKind).toEqual('WRONG_TYPE'); | ||
}); | ||
|
||
it('evaluates an existing string flag with detail', async () => { | ||
td.update(td.flag('flagkey').booleanFlag().valueForAll('potato')); | ||
const res = await client.stringVariationDetail('flagkey', defaultUser, 'default'); | ||
expect(res.value).toEqual('potato'); | ||
expect(res.reason.kind).toBe('FALLTHROUGH'); | ||
}); | ||
|
||
it('it uses the default value when a string variation is for a flag of the wrong type with detail', async () => { | ||
td.update(td.flag('flagkey').valueForAll(8)); | ||
const res = await client.stringVariationDetail('flagkey', defaultUser, 'default'); | ||
expect(res.value).toEqual('default'); | ||
expect(res.reason.kind).toEqual('ERROR'); | ||
expect(res.reason.errorKind).toEqual('WRONG_TYPE'); | ||
}); | ||
}); | ||
|
||
describe('given an offline client', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ describe('given an LDClient with test data', () => { | |
const defaultValue = Object.values(LDMigrationStage).find((item) => item !== value); | ||
// Verify the pre-condition that the default value is not the value under test. | ||
expect(defaultValue).not.toEqual(value); | ||
const res = await client.variationMigration( | ||
const res = await client.migrationVariation( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to make this the same as the other SDKs with this change. This has not been released yet (for Yus). |
||
flagKey, | ||
{ key: 'test-key' }, | ||
defaultValue as LDMigrationStage, | ||
|
@@ -74,15 +74,15 @@ describe('given an LDClient with test data', () => { | |
LDMigrationStage.RampDown, | ||
LDMigrationStage.Complete, | ||
])('returns the default value if the flag does not exist: default = %p', async (stage) => { | ||
const res = await client.variationMigration('no-flag', { key: 'test-key' }, stage); | ||
const res = await client.migrationVariation('no-flag', { key: 'test-key' }, stage); | ||
|
||
expect(res.value).toEqual(stage); | ||
}); | ||
|
||
it('produces an error event for a migration flag with an incorrect value', async () => { | ||
const flagKey = 'bad-migration'; | ||
td.update(td.flag(flagKey).valueForAll('potato')); | ||
const res = await client.variationMigration(flagKey, { key: 'test-key' }, LDMigrationStage.Off); | ||
const res = await client.migrationVariation(flagKey, { key: 'test-key' }, LDMigrationStage.Off); | ||
expect(res.value).toEqual(LDMigrationStage.Off); | ||
expect(errors.length).toEqual(1); | ||
expect(errors[0].message).toEqual( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Run the typed contract tests.