mirror of
https://github.com/Unleash/unleash.git
synced 2024-11-01 19:07:38 +01:00
db61a8a40c
-
https://linear.app/unleash/issue/2-546/fetch-the-payload-from-a-real-feature-flag
-
https://linear.app/unleash/issue/2-547/adapt-ui-to-use-the-feature-flag-information-were-fetching
Tackles the 2 tasks above.
Adapts our `FlagResolver` logic to support variants, so we can use them
for our message banner project but also anything else in the future.
Also adapts MessageBanner to the new logic.
- Add support for variants in `FlagResolver`;
- Adapt `MessageBanner` to a variants flag;
- Adds `sticky` support for the `MessageBanner`;
- Adds our first variants flag to `uiConfig` and `experimental`:
`messageBanner`;
- Adds a `variant-flag-schema` to make it easy to represent the variant
output that we specify in `uiConfig`;
- Adapts `experimental` to be able to represent default variants while
still maintaining type safety;
- Adds helpers to make it easy to use variants in our project, such as
`getVariantValue` and the `useVariant` hook;
- Adapts and adds new tests in `flag-resolver.test.ts`;
### Notes
- ~~The `as PayloadType` assertions need
https://github.com/Unleash/unleash-client-node/pull/454 since it
includes https://github.com/Unleash/unleash-client-node/pull/452~~
(50ccf60893
);
- ~~Enterprise needs a PR that will follow soon~~;
![image](https://github.com/Unleash/unleash/assets/14320932/034ff64f-3020-4ed0-863b-ed1fd9190430)
164 lines
4.3 KiB
TypeScript
164 lines
4.3 KiB
TypeScript
import { PayloadType } from 'unleash-client';
|
|
import { defaultExperimentalOptions, IFlagKey } from '../types/experimental';
|
|
import FlagResolver, { getVariantValue } from './flag-resolver';
|
|
import { IExperimentalOptions } from '../types/experimental';
|
|
|
|
test('should produce empty exposed flags', () => {
|
|
const resolver = new FlagResolver(defaultExperimentalOptions);
|
|
|
|
const result = resolver.getAll();
|
|
|
|
expect(result.anonymiseEventLog).toBe(false);
|
|
});
|
|
|
|
test('should produce UI flags with extra dynamic flags', () => {
|
|
const config = {
|
|
...defaultExperimentalOptions,
|
|
flags: { extraFlag: false },
|
|
};
|
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
|
const result = resolver.getAll() as typeof config.flags;
|
|
|
|
expect(result.extraFlag).toBe(false);
|
|
});
|
|
|
|
test('should use external resolver for dynamic flags', () => {
|
|
const externalResolver = {
|
|
isEnabled: (name: string) => {
|
|
if (name === 'extraFlag') {
|
|
return true;
|
|
}
|
|
},
|
|
getVariant: () => undefined,
|
|
};
|
|
|
|
const config = {
|
|
flags: { extraFlag: false },
|
|
externalResolver,
|
|
};
|
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
|
|
|
const result = resolver.getAll() as typeof config.flags;
|
|
|
|
expect(result.extraFlag).toBe(true);
|
|
});
|
|
|
|
test('should not use external resolver for enabled experiments', () => {
|
|
const externalResolver = {
|
|
isEnabled: () => {
|
|
return false;
|
|
},
|
|
getVariant: () => undefined,
|
|
};
|
|
|
|
const config = {
|
|
flags: { should_be_enabled: true, extraFlag: false },
|
|
externalResolver,
|
|
};
|
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
|
|
|
const result = resolver.getAll() as typeof config.flags;
|
|
|
|
expect(result.should_be_enabled).toBe(true);
|
|
});
|
|
|
|
test('should load experimental flags', () => {
|
|
const externalResolver = {
|
|
isEnabled: () => {
|
|
return false;
|
|
},
|
|
getVariant: () => undefined,
|
|
};
|
|
|
|
const config = {
|
|
flags: { extraFlag: false, someFlag: true },
|
|
externalResolver,
|
|
};
|
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
|
|
|
expect(resolver.isEnabled('someFlag' as IFlagKey)).toBe(true);
|
|
expect(resolver.isEnabled('extraFlag' as IFlagKey)).toBe(false);
|
|
});
|
|
|
|
test('should load experimental flags from external provider', () => {
|
|
const externalResolver = {
|
|
isEnabled: (name: string) => {
|
|
if (name === 'extraFlag') {
|
|
return true;
|
|
}
|
|
},
|
|
getVariant: () => undefined,
|
|
};
|
|
|
|
const config = {
|
|
flags: { extraFlag: false, someFlag: true },
|
|
externalResolver,
|
|
};
|
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
|
|
|
expect(resolver.isEnabled('someFlag' as IFlagKey)).toBe(true);
|
|
expect(resolver.isEnabled('extraFlag' as IFlagKey)).toBe(true);
|
|
});
|
|
|
|
test('should support variant flags', () => {
|
|
const variant = {
|
|
enabled: true,
|
|
name: 'variant',
|
|
payload: {
|
|
type: PayloadType.STRING,
|
|
value: 'variant-A',
|
|
},
|
|
};
|
|
|
|
const externalResolver = {
|
|
isEnabled: () => true,
|
|
getVariant: (name: string) => {
|
|
if (name === 'extraFlag') {
|
|
return variant;
|
|
}
|
|
},
|
|
};
|
|
|
|
const config = {
|
|
flags: { extraFlag: undefined, someFlag: true, otherflag: false },
|
|
externalResolver,
|
|
};
|
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
|
|
|
expect(resolver.getVariant('someFlag' as IFlagKey)).toBe(undefined);
|
|
expect(resolver.getVariant('otherFlag' as IFlagKey)).toBe(undefined);
|
|
expect(resolver.getVariant('extraFlag' as IFlagKey)).toStrictEqual(variant);
|
|
});
|
|
|
|
test('should expose an helper to get variant value', () => {
|
|
expect(
|
|
getVariantValue({
|
|
enabled: true,
|
|
name: 'variant',
|
|
payload: {
|
|
type: PayloadType.STRING,
|
|
value: 'variant-A',
|
|
},
|
|
}),
|
|
).toBe('variant-A');
|
|
|
|
expect(
|
|
getVariantValue({
|
|
enabled: true,
|
|
name: 'variant',
|
|
payload: {
|
|
type: PayloadType.JSON,
|
|
value: `{"foo": "bar"}`,
|
|
},
|
|
}),
|
|
).toStrictEqual({
|
|
foo: 'bar',
|
|
});
|
|
});
|