-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement rebac feature flag via query params
- Loading branch information
1 parent
10e6d25
commit 32d9e8f
Showing
6 changed files
with
215 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
|
||
import useFeatureFlags from "./useFeatureFlags"; | ||
import * as LocalStorage from "./useLocalStorage"; | ||
import * as QueryParams from "./useQueryParams"; | ||
|
||
vi.mock("./useLocalStorage"); | ||
vi.mock("./useQueryParams"); | ||
|
||
describe("useFeatureFlags", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("should initialize with empty flags from local storage", () => { | ||
vi.spyOn(LocalStorage, "default").mockReturnValue([[], vi.fn()]); | ||
vi.spyOn(QueryParams, "useQueryParams").mockReturnValue([ | ||
{ "enable-flag": null, "disable-flag": null }, | ||
vi.fn(), | ||
]); | ||
|
||
renderHook(() => useFeatureFlags()); | ||
|
||
expect(LocalStorage.default).toHaveBeenCalledWith("flags", []); | ||
}); | ||
|
||
it("should enable flags from query parameters", () => { | ||
const setLocalStorage = vi.fn(); | ||
vi.spyOn(LocalStorage, "default").mockReturnValue([[], setLocalStorage]); | ||
vi.spyOn(QueryParams, "useQueryParams").mockReturnValue([ | ||
{ "enable-flag": ["featureA", "featureB"], "disable-flag": null }, | ||
vi.fn(), | ||
]); | ||
|
||
renderHook(() => useFeatureFlags()); | ||
|
||
expect(setLocalStorage).toHaveBeenCalledWith(["featureA", "featureB"]); | ||
}); | ||
|
||
it("should disable flags from query parameters", () => { | ||
const setLocalStorage = vi.fn(); | ||
vi.spyOn(LocalStorage, "default").mockReturnValue([ | ||
["featureA", "featureB", "featureC"], | ||
setLocalStorage, | ||
]); | ||
vi.spyOn(QueryParams, "useQueryParams").mockReturnValue([ | ||
{ "enable-flag": null, "disable-flag": ["featureB"] }, | ||
vi.fn(), | ||
]); | ||
|
||
renderHook(() => useFeatureFlags()); | ||
|
||
expect(setLocalStorage).toHaveBeenCalledWith(["featureA", "featureC"]); | ||
}); | ||
|
||
it("should handle both enable and disable flags", () => { | ||
const setLocalStorage = vi.fn(); | ||
vi.spyOn(LocalStorage, "default").mockReturnValue([ | ||
["featureA", "featureB", "featureC"], | ||
setLocalStorage, | ||
]); | ||
vi.spyOn(QueryParams, "useQueryParams").mockReturnValue([ | ||
{ "enable-flag": ["featureD"], "disable-flag": ["featureB"] }, | ||
vi.fn(), | ||
]); | ||
|
||
renderHook(() => useFeatureFlags()); | ||
|
||
expect(setLocalStorage).toHaveBeenCalledWith([ | ||
"featureA", | ||
"featureC", | ||
"featureD", | ||
]); | ||
}); | ||
|
||
it("should not update local storage when flags are the same", () => { | ||
const setLocalStorage = vi.fn(); | ||
vi.spyOn(LocalStorage, "default").mockReturnValue([ | ||
["featureA", "featureB"], | ||
setLocalStorage, | ||
]); | ||
vi.spyOn(QueryParams, "useQueryParams").mockReturnValue([ | ||
{ "enable-flag": ["featureA", "featureB"], "disable-flag": null }, | ||
vi.fn(), | ||
]); | ||
|
||
renderHook(() => useFeatureFlags()); | ||
|
||
expect(setLocalStorage).not.toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("should handle null query params", () => { | ||
const setLocalStorage = vi.fn(); | ||
vi.spyOn(LocalStorage, "default").mockReturnValue([ | ||
["featureA"], | ||
setLocalStorage, | ||
]); | ||
vi.spyOn(QueryParams, "useQueryParams").mockReturnValue([ | ||
{ "enable-flag": null, "disable-flag": null }, | ||
vi.fn(), | ||
]); | ||
|
||
renderHook(() => useFeatureFlags()); | ||
|
||
expect(setLocalStorage).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should handle empty array query params", () => { | ||
const setLocalStorage = vi.fn(); | ||
vi.spyOn(LocalStorage, "default").mockReturnValue([ | ||
["featureA"], | ||
setLocalStorage, | ||
]); | ||
vi.spyOn(QueryParams, "useQueryParams").mockReturnValue([ | ||
{ "enable-flag": [], "disable-flag": [] }, | ||
vi.fn(), | ||
]); | ||
|
||
renderHook(() => useFeatureFlags()); | ||
|
||
expect(setLocalStorage).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useCallback, useEffect, useMemo } from "react"; | ||
|
||
import { ENABLED_FLAGS } from "consts"; | ||
|
||
import useLocalStorage from "./useLocalStorage"; | ||
import { useQueryParams } from "./useQueryParams"; | ||
|
||
type FlagQueryParams = { | ||
"enable-flag": string[] | null; | ||
"disable-flag": string[] | null; | ||
}; | ||
|
||
export default function useFeatureFlags() { | ||
const [queryParams] = useQueryParams<FlagQueryParams>({ | ||
"enable-flag": [], | ||
"disable-flag": [], | ||
}); | ||
const [enabledFlags, setEnabledFlags] = useLocalStorage<string[]>( | ||
ENABLED_FLAGS, | ||
[], | ||
); | ||
|
||
const getFinalFlags = useCallback(() => { | ||
const enabledParams = queryParams["enable-flag"] ?? []; | ||
const disabledParams = queryParams["disable-flag"] ?? []; | ||
|
||
// remove disabled params from flag list | ||
let result = enabledFlags.filter((flag) => !disabledParams.includes(flag)); | ||
|
||
// append enabled params to flag list | ||
result = enabledParams.reduce((acc, param) => { | ||
if (!disabledParams.includes(param) && !acc.includes(param)) { | ||
return [...acc, param]; | ||
} | ||
return acc; | ||
}, result); | ||
return result; | ||
}, [queryParams, enabledFlags]); | ||
|
||
const finalFlags = useMemo(() => getFinalFlags(), [getFinalFlags]); | ||
|
||
useEffect(() => { | ||
// deep comparison to prevent unnecessary re-renders | ||
if (JSON.stringify(finalFlags) !== JSON.stringify(enabledFlags)) { | ||
setEnabledFlags(finalFlags); | ||
} | ||
}, [setEnabledFlags, finalFlags, enabledFlags]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters