From cdb8d49b4010b0c0ee1c01d1c0fd1ad267945dba Mon Sep 17 00:00:00 2001 From: Richard Race <1273965+richrace@users.noreply.github.com> Date: Wed, 4 Sep 2024 11:34:31 +0100 Subject: [PATCH] Extract of enabled to its own file and add tests --- .../Offences/Offence/OffenceMatching.tsx | 16 +---- src/utils/offenceMatcher/isEnabled.test.ts | 59 +++++++++++++++++++ src/utils/offenceMatcher/isEnabled.ts | 15 +++++ 3 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 src/utils/offenceMatcher/isEnabled.test.ts create mode 100644 src/utils/offenceMatcher/isEnabled.ts diff --git a/src/features/CourtCaseDetails/Tabs/Panels/Offences/Offence/OffenceMatching.tsx b/src/features/CourtCaseDetails/Tabs/Panels/Offences/Offence/OffenceMatching.tsx index 06b83a48f..d4f1ac31b 100644 --- a/src/features/CourtCaseDetails/Tabs/Panels/Offences/Offence/OffenceMatching.tsx +++ b/src/features/CourtCaseDetails/Tabs/Panels/Offences/Offence/OffenceMatching.tsx @@ -6,27 +6,15 @@ import ExceptionFieldTableRow from "components/ExceptionFieldTableRow" import { useCourtCase } from "context/CourtCaseContext" import { useCurrentUser } from "context/CurrentUserContext" import { findExceptions } from "types/ErrorMessages" -import { DisplayFullUser } from "types/display/Users" import { Exception } from "types/exceptions" import getExceptionDefinition from "utils/getExceptionDefinition" import { getOffenceMatchingException } from "utils/offenceMatcher/getOffenceMatchingException" +import isEnabled from "utils/offenceMatcher/isEnabled" import offenceMatchingExceptions from "utils/offenceMatcher/offenceMatchingExceptions" import findCandidates from "../../../../../../utils/offenceMatcher/findCandidates" import { TableRow } from "../../TableRow" import OffenceMatcher from "./OffenceMatcher" -const enabled = (user: DisplayFullUser) => { - const enabledInProduction = true // change this if we need to disable in production for everyone - const { exceptionsEnabled, offenceMatchingEnabled } = user.featureFlags - const featureFlagsEnabled = exceptionsEnabled && offenceMatchingEnabled - - const isProduction = process.env.WORKSPACE === "production" - if (!isProduction) { - return featureFlagsEnabled - } - return enabledInProduction && featureFlagsEnabled -} - type OffenceMatchingProps = { offenceIndex: number offence: Offence @@ -58,7 +46,7 @@ export const OffenceMatching = ({ ) || getExceptionDefinition(findExceptionByOffenceNumber[0]?.code)?.shortDescription const displayOffenceMatcher = - enabled(currentUser) && exceptions.some((e) => offenceMatchingExceptions.offenceNotMatched.includes(e.code)) + isEnabled(currentUser) && exceptions.some((e) => offenceMatchingExceptions.offenceNotMatched.includes(e.code)) const userCanMatchOffence = courtCase.errorLockedByUsername === currentUser?.username && courtCase.errorStatus === "Unresolved" diff --git a/src/utils/offenceMatcher/isEnabled.test.ts b/src/utils/offenceMatcher/isEnabled.test.ts new file mode 100644 index 000000000..1144b15b0 --- /dev/null +++ b/src/utils/offenceMatcher/isEnabled.test.ts @@ -0,0 +1,59 @@ +import type { DisplayFullUser } from "types/display/Users" +import isEnabled from "./isEnabled" + +describe("isEnabled", () => { + let user: DisplayFullUser + + beforeEach(() => { + user = { + username: "GeneralHandler", + visibleForces: [], + email: "generalhandler@example.com", + visibleCourts: [], + excludedTriggers: [], + groups: [], + hasAccessTo: { 0: false, 1: false, 2: false, 3: false, 4: false, 5: false, 6: false }, + featureFlags: {} + } + }) + + it("returns false with no feature flags", () => { + const result = isEnabled(user) + expect(result).toBe(false) + }) + + it("returns false with only exceptionsEnabled feature flag enabled", () => { + user.featureFlags = { exceptionsEnabled: true } + + const result = isEnabled(user) + expect(result).toBe(false) + }) + + it("returns false with only offenceMatchingEnabled feature flag enabled", () => { + user.featureFlags = { offenceMatchingEnabled: true } + + const result = isEnabled(user) + expect(result).toBe(false) + }) + + it("returns true with exceptionsEnabled and offenceMatchingEnabled feature flag enabled", () => { + user.featureFlags = { exceptionsEnabled: true, offenceMatchingEnabled: true } + + const result = isEnabled(user) + expect(result).toBe(true) + }) + + it("returns false with exceptionsEnabled disabled and offenceMatchingEnabled enabled", () => { + user.featureFlags = { exceptionsEnabled: false, offenceMatchingEnabled: true } + + const result = isEnabled(user) + expect(result).toBe(false) + }) + + it("returns false with exceptionsEnabled enabled and offenceMatchingEnabled disabled", () => { + user.featureFlags = { exceptionsEnabled: true, offenceMatchingEnabled: false } + + const result = isEnabled(user) + expect(result).toBe(false) + }) +}) diff --git a/src/utils/offenceMatcher/isEnabled.ts b/src/utils/offenceMatcher/isEnabled.ts new file mode 100644 index 000000000..b76b1766b --- /dev/null +++ b/src/utils/offenceMatcher/isEnabled.ts @@ -0,0 +1,15 @@ +import type { DisplayFullUser } from "types/display/Users" + +const isEnabled = (user: DisplayFullUser): boolean => { + const enabledInProduction = true // change this if we need to disable in production for everyone + const { exceptionsEnabled, offenceMatchingEnabled } = user.featureFlags + const featureFlagsEnabled: boolean = (exceptionsEnabled ?? false) && (offenceMatchingEnabled ?? false) + + const isProduction = process.env.WORKSPACE === "production" + if (!isProduction) { + return featureFlagsEnabled + } + return enabledInProduction && featureFlagsEnabled +} + +export default isEnabled