Skip to content

Commit

Permalink
fix: set percentage to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Dec 21, 2022
1 parent 2f07fc8 commit 728e5ad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
48 changes: 37 additions & 11 deletions packages/sdk/src/admin.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Redis } from "@upstash/redis";
import { describe, test } from "node:test";
import { test } from "node:test";
import { randomUUID } from "node:crypto";
import { Admin } from "./admin";
import assert from "node:assert";
Expand All @@ -9,8 +9,8 @@ import { environments } from "./environment";
config();
const admin = new Admin({ redis: Redis.fromEnv() });

describe("Create a flag", async () => {
test("can load the flag after creating it", async () => {
test("Create a flag", async (t) => {
await t.test("can load the flag after creating it", async () => {
const name = randomUUID();
const flag = await admin.createFlag({ name });
assert.equal(flag.development.name, name);
Expand All @@ -25,14 +25,14 @@ describe("Create a flag", async () => {
});
});

describe("Update a flag", async () => {
test("set percentage to null", async () => {
test("Update a flag", async (t) => {
await t.test("set percentage to null", async () => {
const name = randomUUID();
await admin.createFlag({ name });

for (const env of environments) {
const percentage1 = Math.random() * 100;
const percentage2 = Math.random() * 100;
const percentage1 = Math.ceil(Math.random() * 100);
const percentage2 = Math.ceil(Math.random() * 100);
await admin.updateFlag(name, env, {
percentage: percentage1,
});
Expand All @@ -45,20 +45,20 @@ describe("Update a flag", async () => {
});

const f2 = await admin.getFlag(name, env);
assert.equal(f2!.percentage, percentage1);
assert.equal(f2!.percentage, percentage2);

await admin.updateFlag(name, env, {
percentage: null,
});

const f3 = await admin.getFlag(name, env);
assert.equal(f2!.percentage, null);
assert.equal(f3!.percentage, null);
}
});
});

describe("Rename a flag", async () => {
test("renames all environments", async () => {
test("Rename a flag", async (t) => {
await t.test("renames all environments", async () => {
let name = randomUUID();

await admin.createFlag({ name });
Expand All @@ -76,3 +76,29 @@ describe("Rename a flag", async () => {
}
});
});

test("Setting percentage to 0", async (t) => {
await t.test("sets it to null", async () => {
const {
production: { name, environment },
} = await admin.createFlag({ name: randomUUID() });

await admin.updateFlag(name, environment, {
percentage: 10,
});

const f1 = await admin.getFlag(name, environment);
assert.equal(f1!.percentage, 10, "percentage should be 10 now");

await admin.updateFlag(name, environment, {
percentage: 0,
});

const f2 = await admin.getFlag(name, environment);
assert.equal(
f2!.percentage,
null,
"percentage should be disabled and set to null",
);
});
});
4 changes: 3 additions & 1 deletion packages/sdk/src/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ export class Admin {
enabled: data.enabled ?? flag.enabled,
rules: data.rules ?? flag.rules,
percentage:
data.percentage === null ? null : data.percentage ?? flag.percentage,
data.percentage === null || data.percentage === 0
? null
: data.percentage ?? flag.percentage,
};
await this.redis.set(key, updated, { xx: true });
return updated;
Expand Down

1 comment on commit 728e5ad

@vercel
Copy link

@vercel vercel bot commented on 728e5ad Dec 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.