Skip to content

Commit

Permalink
Update browser test (#45)
Browse files Browse the repository at this point in the history
* update browser test

* fix lint

* update

* update

* update
  • Loading branch information
zhiyuanliang-ms authored Sep 25, 2024
1 parent 4e711c9 commit 9108ea1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 45 deletions.
3 changes: 1 addition & 2 deletions test/browser/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ 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}`);
});

await page.goto(`file:${filePath}`);
await page.waitForTimeout(10000);

const failures = await page.evaluate(() => (window as any).mochaFailures);

chai.expect(failures).to.equal(0);
chai.expect(hasPageError).to.be.false;
});
2 changes: 1 addition & 1 deletion test/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<script src="../../dist/umd/index.js"></script>
<script src="./testcases.js"></script>
<script class="mocha-exec">
mocha.run(function (failures) {
mocha.run(failures => {
window.mochaFailures = failures;
});
</script>
Expand Down
125 changes: 83 additions & 42 deletions test/browser/testcases.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,104 @@
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 () => {
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(1000);
});

0 comments on commit 9108ea1

Please sign in to comment.