Skip to content

Commit

Permalink
Extract of enabled to its own file and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
richrace committed Sep 4, 2024
1 parent a13081c commit cdb8d49
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand Down
59 changes: 59 additions & 0 deletions src/utils/offenceMatcher/isEnabled.test.ts
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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)
})
})
15 changes: 15 additions & 0 deletions src/utils/offenceMatcher/isEnabled.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cdb8d49

Please sign in to comment.