From ca232d239e164d07873f67f73949d063ed238848 Mon Sep 17 00:00:00 2001 From: Zhiyuan Liang Date: Wed, 25 Sep 2024 14:03:07 +0800 Subject: [PATCH 1/5] update browser test --- test/browser/browser.test.ts | 14 ++-- test/browser/index.html | 4 +- test/browser/testcases.js | 130 ++++++++++++++++++++++++----------- 3 files changed, 99 insertions(+), 49 deletions(-) diff --git a/test/browser/browser.test.ts b/test/browser/browser.test.ts index e6b137c..a8dc678 100644 --- a/test/browser/browser.test.ts +++ b/test/browser/browser.test.ts @@ -1,4 +1,4 @@ -import { test } from "@playwright/test"; +import { test, expect } from "@playwright/test"; import chai from "chai"; import path from "path"; @@ -7,7 +7,6 @@ test("Testcase can pass in browser environment", async ({ page }) => { const filePath = path.join(__dirname, "index.html"); let hasPageError = false; - page.on("pageerror", (err) => { hasPageError = true; console.log(`Page Error: ${err.message}`); @@ -15,8 +14,15 @@ test("Testcase can pass in browser environment", async ({ page }) => { await page.goto(`file:${filePath}`); - const failures = await page.evaluate(() => (window as any).mochaFailures); + await page.waitForTimeout(10000); - chai.expect(failures).to.equal(0); chai.expect(hasPageError).to.be.false; + + const failures = await page.locator('li.failures em').innerText(); + + chai.expect(failures).to.equal('0'); + + const passes = await page.locator('li.passes em').innerText(); + + chai.expect(passes).to.equal('9'); }); diff --git a/test/browser/index.html b/test/browser/index.html index 57df7fd..fc78fe2 100644 --- a/test/browser/index.html +++ b/test/browser/index.html @@ -19,9 +19,7 @@ \ No newline at end of file diff --git a/test/browser/testcases.js b/test/browser/testcases.js index ba865ed..55d2e90 100644 --- a/test/browser/testcases.js +++ b/test/browser/testcases.js @@ -1,63 +1,109 @@ const ConfigurationObjectFeatureFlagProvider = FeatureManagement.ConfigurationObjectFeatureFlagProvider; const FeatureManager = FeatureManagement.FeatureManager; -describe("feature manager", () => { - it("should load from json string", - async () => { - const jsonObject = { - "feature_management": { - "feature_flags": [ +const jsonObject = { + "feature_management": { + "feature_flags": [ + { + "id": "ComplexTargeting", + "description": "A feature flag using a targeting filter, that will return true for Alice, Stage1, and 50% of Stage2. Dave and Stage3 are excluded. The default rollout percentage is 25%.", + "enabled": true, + "conditions": { + "client_filters": [ { - "id": "ComplexTargeting", - "description": "A feature flag using a targeting filter, that will return true for Alice, Stage1, and 50% of Stage2. Dave and Stage3 are excluded. The default rollout percentage is 25%.", - "enabled": true, - "conditions": { - "client_filters": [ - { - "name": "Microsoft.Targeting", - "parameters": { - "Audience": { - "Users": [ - "Alice" - ], - "Groups": [ - { - "Name": "Stage1", - "RolloutPercentage": 100 - }, - { - "Name": "Stage2", - "RolloutPercentage": 50 - } - ], - "DefaultRolloutPercentage": 25, - "Exclusion": { - "Users": ["Dave"], - "Groups": ["Stage3"] - } - } + "name": "Microsoft.Targeting", + "parameters": { + "Audience": { + "Users": [ + "Alice" + ], + "Groups": [ + { + "Name": "Stage1", + "RolloutPercentage": 100 + }, + { + "Name": "Stage2", + "RolloutPercentage": 50 } + ], + "DefaultRolloutPercentage": 25, + "Exclusion": { + "Users": ["Dave"], + "Groups": ["Stage3"] } - ] + } } } ] } - }; - - const provider = new ConfigurationObjectFeatureFlagProvider(jsonObject); - const featureManager = new FeatureManager(provider); + } + ] + } +}; + +const provider = new ConfigurationObjectFeatureFlagProvider(jsonObject); +const featureManager = new FeatureManager(provider); + +describe("feature manager", () => { + it("should not target user Aiden in default rollout", + async () => { chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Aiden" })).to.eq(false); + } + ).timeout(1000);; + + it("should target user Bloosom in default rollout", + async () => { chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Blossom" })).to.eq(true); + } + ).timeout(1000);; + + it("should target user Alice", + async () => { chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Alice" })).to.eq(true); + } + ).timeout(1000);; + + it("should target user Aiden in group Stage1", + async () => { chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Aiden", groups: ["Stage1"] })).to.eq(true); + } + ).timeout(1000);; + + it("should not target user Dave in group Stage1", + async () => { + chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Dave", groups: ["Stage1"] })).to.eq(false); + } + ).timeout(1000);; + + it("should not target empty user in group Stage2", + async () => { chai.expect(await featureManager.isEnabled("ComplexTargeting", { groups: ["Stage2"] })).to.eq(false); + } + ).timeout(1000);; + + it("should target user Aiden in group Stage2", + async () => { chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Aiden", groups: ["Stage2"] })).to.eq(true); + } + ).timeout(1000);; + + it("should not target user Chris in group Stage2", + async () => { chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Chris", groups: ["Stage2"] })).to.eq(false); - chai.expect(await featureManager.isEnabled("ComplexTargeting", { groups: ["Stage3"] })).to.eq(false), + } + ).timeout(1000);; + + it("should not target group Stage3", + async () => { + function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + await sleep(5000); + + chai.expect(await featureManager.isEnabled("ComplexTargeting", { groups: ["Stage3"] })).to.eq(false); chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Alice", groups: ["Stage3"] })).to.eq(false); chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Blossom", groups: ["Stage3"] })).to.eq(false); - chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Dave", groups: ["Stage1"] })).to.eq(false); } - ); + ).timeout(10000); }); From c17b4dd2679bb2eea5bcd2d728d1fe4d944a9816 Mon Sep 17 00:00:00 2001 From: Zhiyuan Liang Date: Wed, 25 Sep 2024 14:04:49 +0800 Subject: [PATCH 2/5] fix lint --- test/browser/browser.test.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/browser/browser.test.ts b/test/browser/browser.test.ts index a8dc678..4c94443 100644 --- a/test/browser/browser.test.ts +++ b/test/browser/browser.test.ts @@ -1,4 +1,4 @@ -import { test, expect } from "@playwright/test"; +import { test } from "@playwright/test"; import chai from "chai"; import path from "path"; @@ -18,11 +18,11 @@ test("Testcase can pass in browser environment", async ({ page }) => { chai.expect(hasPageError).to.be.false; - const failures = await page.locator('li.failures em').innerText(); + const failures = await page.locator("li.failures em").innerText(); - chai.expect(failures).to.equal('0'); + chai.expect(failures).to.equal("0"); - const passes = await page.locator('li.passes em').innerText(); + const passes = await page.locator("li.passes em").innerText(); - chai.expect(passes).to.equal('9'); + chai.expect(passes).to.equal("9"); }); From 7313b3d745a65888e2117e3437e14ac2bb51d51e Mon Sep 17 00:00:00 2001 From: Zhiyuan Liang Date: Wed, 25 Sep 2024 14:10:01 +0800 Subject: [PATCH 3/5] update --- test/browser/testcases.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/test/browser/testcases.js b/test/browser/testcases.js index 55d2e90..a31cd04 100644 --- a/test/browser/testcases.js +++ b/test/browser/testcases.js @@ -96,14 +96,9 @@ describe("feature manager", () => { it("should not target group Stage3", async () => { - function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); - } - await sleep(5000); - chai.expect(await featureManager.isEnabled("ComplexTargeting", { groups: ["Stage3"] })).to.eq(false); chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Alice", groups: ["Stage3"] })).to.eq(false); chai.expect(await featureManager.isEnabled("ComplexTargeting", { userId: "Blossom", groups: ["Stage3"] })).to.eq(false); } - ).timeout(10000); + ).timeout(1000); }); From 8cc898eeb253c05a39de4d63a40ca7aaba78e964 Mon Sep 17 00:00:00 2001 From: Zhiyuan Liang Date: Wed, 25 Sep 2024 14:49:04 +0800 Subject: [PATCH 4/5] update --- test/browser/browser.test.ts | 8 ++------ test/browser/index.html | 4 +++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/test/browser/browser.test.ts b/test/browser/browser.test.ts index 4c94443..7ff18ff 100644 --- a/test/browser/browser.test.ts +++ b/test/browser/browser.test.ts @@ -18,11 +18,7 @@ test("Testcase can pass in browser environment", async ({ page }) => { chai.expect(hasPageError).to.be.false; - const failures = await page.locator("li.failures em").innerText(); + const failures = await page.evaluate(() => (window as any).mochaFailures); - chai.expect(failures).to.equal("0"); - - const passes = await page.locator("li.passes em").innerText(); - - chai.expect(passes).to.equal("9"); + chai.expect(failures).to.equal(0); }); diff --git a/test/browser/index.html b/test/browser/index.html index fc78fe2..5c07b7e 100644 --- a/test/browser/index.html +++ b/test/browser/index.html @@ -19,7 +19,9 @@ \ No newline at end of file From 58c374ea9b507f3afac5f757260fd8d8a7354ca0 Mon Sep 17 00:00:00 2001 From: Zhiyuan Liang Date: Wed, 25 Sep 2024 14:49:57 +0800 Subject: [PATCH 5/5] update --- test/browser/browser.test.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/browser/browser.test.ts b/test/browser/browser.test.ts index 7ff18ff..b7e71be 100644 --- a/test/browser/browser.test.ts +++ b/test/browser/browser.test.ts @@ -13,12 +13,9 @@ test("Testcase can pass in browser environment", async ({ page }) => { }); await page.goto(`file:${filePath}`); - await page.waitForTimeout(10000); - chai.expect(hasPageError).to.be.false; - const failures = await page.evaluate(() => (window as any).mochaFailures); - chai.expect(failures).to.equal(0); + chai.expect(hasPageError).to.be.false; });