@vllnt/convex-flags 0.1.0 → 0.1.1-canary.824d5e2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +98 -0
- package/README.md +181 -67
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +77 -23
- package/dist/component/mutations.js.map +1 -1
- package/docs/API.md +245 -0
- package/llms.txt +39 -0
- package/package.json +47 -25
- package/src/component/mutations.ts +107 -28
- package/src/react/index.test.ts +0 -52
- package/src/status-backward-compat.test.ts +0 -51
package/src/react/index.test.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { renderHook } from "@testing-library/react";
|
|
3
|
-
import { makeFunctionReference } from "convex/server";
|
|
4
|
-
import { beforeEach, expect, test, vi } from "vitest";
|
|
5
|
-
import { useFlag, useFlags, type AllRef, type EvaluateRef } from "./index.js";
|
|
6
|
-
|
|
7
|
-
const { useQuerySpy } = vi.hoisted(() => ({ useQuerySpy: vi.fn() }));
|
|
8
|
-
vi.mock("convex/react", () => ({ useQuery: useQuerySpy }));
|
|
9
|
-
|
|
10
|
-
const evalRef: EvaluateRef = makeFunctionReference<"query">("flags:evaluate");
|
|
11
|
-
const allRef: AllRef = makeFunctionReference<"query">("flags:all");
|
|
12
|
-
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
useQuerySpy.mockReset();
|
|
15
|
-
useQuerySpy.mockReturnValue({ value: true, reason: "flag" });
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test("useFlag forwards key + context + default and returns the query result", () => {
|
|
19
|
-
const { result } = renderHook(() =>
|
|
20
|
-
useFlag(evalRef, "new-checkout", {
|
|
21
|
-
context: { subjectRef: "u1", attributes: { plan: "pro" } },
|
|
22
|
-
default: "off",
|
|
23
|
-
}),
|
|
24
|
-
);
|
|
25
|
-
expect(useQuerySpy).toHaveBeenCalledWith(evalRef, {
|
|
26
|
-
key: "new-checkout",
|
|
27
|
-
context: { subjectRef: "u1", attributes: { plan: "pro" } },
|
|
28
|
-
default: "off",
|
|
29
|
-
});
|
|
30
|
-
expect(result.current).toEqual({ value: true, reason: "flag" });
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("useFlag works with no options", () => {
|
|
34
|
-
renderHook(() => useFlag(evalRef, "k"));
|
|
35
|
-
expect(useQuerySpy).toHaveBeenCalledWith(evalRef, {
|
|
36
|
-
key: "k",
|
|
37
|
-
context: undefined,
|
|
38
|
-
default: undefined,
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test("useFlags forwards context", () => {
|
|
43
|
-
useQuerySpy.mockReturnValue({ k: { value: true, reason: "flag" } });
|
|
44
|
-
const { result } = renderHook(() => useFlags(allRef, { subjectRef: "u1" }));
|
|
45
|
-
expect(useQuerySpy).toHaveBeenCalledWith(allRef, { context: { subjectRef: "u1" } });
|
|
46
|
-
expect(result.current).toEqual({ k: { value: true, reason: "flag" } });
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("useFlags works with no context", () => {
|
|
50
|
-
renderHook(() => useFlags(allRef));
|
|
51
|
-
expect(useQuerySpy).toHaveBeenCalledWith(allRef, { context: undefined });
|
|
52
|
-
});
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import { flagFields } from "./component/validators.js";
|
|
3
|
-
import { evaluateFlag, withStatus } from "./shared.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Regression guard for #5 — a required `status` field broke deploys for any
|
|
7
|
-
* consumer with flag rows written before `status` existed. The expand fix makes
|
|
8
|
-
* `status` optional and reads an absent status as `"active"`.
|
|
9
|
-
*/
|
|
10
|
-
describe("status backward compatibility (#5)", () => {
|
|
11
|
-
test("the schema validator accepts a row without `status` (legacy data)", () => {
|
|
12
|
-
expect(flagFields.status.isOptional).toBe("optional");
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("a flag with no `status` is evaluated as active, not disabled", () => {
|
|
16
|
-
expect(evaluateFlag("legacy", { value: true }, {})).toEqual({
|
|
17
|
-
value: true,
|
|
18
|
-
reason: "flag",
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("a status-less flag still runs targeting (absent status !== archived)", () => {
|
|
23
|
-
expect(
|
|
24
|
-
evaluateFlag(
|
|
25
|
-
"legacy",
|
|
26
|
-
{ value: "BASE", rules: [{ conditions: [], value: "RULE" }] },
|
|
27
|
-
{},
|
|
28
|
-
),
|
|
29
|
-
).toEqual({ value: "RULE", reason: "rule" });
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("an explicitly archived flag is still disabled", () => {
|
|
33
|
-
expect(evaluateFlag("k", { value: "BASE", status: "archived" }, {})).toEqual({
|
|
34
|
-
value: "BASE",
|
|
35
|
-
reason: "disabled",
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test("withStatus materializes an absent status as active for returned docs", () => {
|
|
40
|
-
const legacyRow: { key: string; value: boolean; status?: "active" | "archived" } = {
|
|
41
|
-
key: "legacy",
|
|
42
|
-
value: true,
|
|
43
|
-
};
|
|
44
|
-
expect(withStatus(legacyRow).status).toBe("active");
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test("withStatus preserves an explicit status", () => {
|
|
48
|
-
const archivedRow = { key: "k", value: true, status: "archived" as const };
|
|
49
|
-
expect(withStatus(archivedRow).status).toBe("archived");
|
|
50
|
-
});
|
|
51
|
-
});
|