Skip to content

Commit aa54818

Browse files
add test
1 parent f7a306a commit aa54818

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/feature-management/test/targetingFilter.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,28 @@ describe("targeting filter", () => {
130130
expect(featureManager.isEnabled("ComplexTargeting", { userId: "Dave", groups: ["Stage1"] })).eventually.eq(false, "Dave is excluded because he is in the exclusion list"),
131131
]);
132132
});
133+
134+
it("should evaluate feature with targeting filter with targeting context accessor", async () => {
135+
const dataSource = new Map();
136+
dataSource.set("feature_management", {
137+
feature_flags: [complexTargetingFeature]
138+
});
139+
140+
let userId = "";
141+
let groups: string[] = [];
142+
const testTargetingContextAccessor = () => ({ userId, groups });
143+
const provider = new ConfigurationMapFeatureFlagProvider(dataSource);
144+
const featureManager = new FeatureManager(provider, {targetingContextAccessor: testTargetingContextAccessor});
145+
146+
userId = "Aiden";
147+
expect(await featureManager.isEnabled("ComplexTargeting")).to.eq(false);
148+
userId = "Blossom";
149+
expect(await featureManager.isEnabled("ComplexTargeting")).to.eq(true);
150+
expect(await featureManager.isEnabled("ComplexTargeting", {userId: "Aiden"})).to.eq(true); // targeting id will be overridden by the context accessor
151+
userId = "Aiden";
152+
groups = ["Stage2"];
153+
expect(await featureManager.isEnabled("ComplexTargeting")).to.eq(true);
154+
userId = "Chris";
155+
expect(await featureManager.isEnabled("ComplexTargeting")).to.eq(false);
156+
});
133157
});

src/feature-management/test/variant.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,23 @@ describe("feature variant", () => {
9090
it("throw exception for invalid doubles From and To in the Percentile section");
9191

9292
});
93+
});
9394

95+
describe("variant assignment with targeting context accessor", () => {
96+
it("should assign variant based on targeting context accessor", async () => {
97+
let userId = "";
98+
let groups: string[] = [];
99+
const testTargetingContextAccessor = () => ({ userId, groups });
100+
const provider = new ConfigurationObjectFeatureFlagProvider(featureFlagsConfigurationObject);
101+
const featureManager = new FeatureManager(provider, {targetingContextAccessor: testTargetingContextAccessor});
102+
userId = "Marsha";
103+
let variant = await featureManager.getVariant(Features.VariantFeatureUser);
104+
expect(variant).not.to.be.undefined;
105+
expect(variant?.name).eq("Small");
106+
userId = "Jeff";
107+
variant = await featureManager.getVariant(Features.VariantFeatureUser);
108+
expect(variant).to.be.undefined;
109+
variant = await featureManager.getVariant(Features.VariantFeatureUser, {userId: "Marsha"}); // targeting id will be overridden by the context accessor
110+
expect(variant).to.be.undefined;
111+
});
94112
});

0 commit comments

Comments
 (0)