-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add resolvedByUsername to case list query (#719)
* update logic and write test * refactor logic and update test * refactor function to helper file * WIP * WIP * add test for formatName * WIP * complete test
- Loading branch information
1 parent
8391826
commit 162538d
Showing
5 changed files
with
213 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function formatName(name: string) { | ||
let splitName = name.replace(/\*|\s+/g, "%") | ||
|
||
if (!name.includes("%") && !splitName.endsWith("%")) { | ||
splitName = `${splitName}%` | ||
} | ||
|
||
return splitName | ||
} |
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
107 changes: 107 additions & 0 deletions
107
test/services/listCourtCases/filterByResolvedByUsername.integration.test.ts
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,107 @@ | ||
import CourtCase from "services/entities/CourtCase" | ||
import User from "services/entities/User" | ||
import getDataSource from "services/getDataSource" | ||
import listCourtCases from "services/listCourtCases" | ||
import courtCasesByOrganisationUnitQuery from "services/queries/courtCasesByOrganisationUnitQuery" | ||
import leftJoinAndSelectTriggersQuery from "services/queries/leftJoinAndSelectTriggersQuery" | ||
import { DataSource } from "typeorm" | ||
import { ListCourtCaseResult } from "types/ListCourtCasesResult" | ||
import { isError } from "../../../src/types/Result" | ||
import { hasAccessToAll } from "../../helpers/hasAccessTo" | ||
import deleteFromEntity from "../../utils/deleteFromEntity" | ||
import { insertCourtCasesWithFields } from "../../utils/insertCourtCases" | ||
import Trigger from "../../../src/services/entities/Trigger" | ||
import Note from "services/entities/Note" | ||
|
||
jest.mock("services/queries/courtCasesByOrganisationUnitQuery") | ||
jest.mock("services/queries/leftJoinAndSelectTriggersQuery") | ||
|
||
jest.setTimeout(100000) | ||
describe("listCourtCases", () => { | ||
let dataSource: DataSource | ||
const forceCode = "036" | ||
const testUser = { | ||
visibleForces: [forceCode], | ||
visibleCourts: [], | ||
hasAccessTo: hasAccessToAll | ||
} as Partial<User> as User | ||
|
||
beforeAll(async () => { | ||
dataSource = await getDataSource() | ||
}) | ||
|
||
beforeEach(async () => { | ||
await deleteFromEntity(CourtCase) | ||
await deleteFromEntity(Trigger) | ||
await deleteFromEntity(Note) | ||
jest.resetAllMocks() | ||
jest.clearAllMocks() | ||
;(courtCasesByOrganisationUnitQuery as jest.Mock).mockImplementation( | ||
jest.requireActual("services/queries/courtCasesByOrganisationUnitQuery").default | ||
) | ||
;(leftJoinAndSelectTriggersQuery as jest.Mock).mockImplementation( | ||
jest.requireActual("services/queries/leftJoinAndSelectTriggersQuery").default | ||
) | ||
}) | ||
|
||
afterAll(async () => { | ||
if (dataSource) { | ||
await dataSource.destroy() | ||
} | ||
}) | ||
|
||
describe("search by resolvedByUsername", () => { | ||
it("Should list cases that match the partial username search", async () => { | ||
await insertCourtCasesWithFields([ | ||
{ errorResolvedBy: "User Name01" }, | ||
{ triggerResolvedBy: "User Name02" }, | ||
{ errorResolvedBy: "User Name03" } | ||
]) | ||
|
||
const result = await listCourtCases(dataSource, { maxPageItems: 100, resolvedByUsername: "User" }, testUser) | ||
expect(isError(result)).toBe(false) | ||
const { result: cases } = result as ListCourtCaseResult | ||
|
||
expect(cases).toHaveLength(3) | ||
expect(cases[0].errorResolvedBy).toStrictEqual("User Name01") | ||
expect(cases[1].triggerResolvedBy).toStrictEqual("User Name02") | ||
expect(cases[2].errorResolvedBy).toStrictEqual("User Name03") | ||
}) | ||
|
||
it("Should list cases that match the full username search", async () => { | ||
await insertCourtCasesWithFields([ | ||
{ errorResolvedBy: "User Name01" }, | ||
{ triggerResolvedBy: "User Name02" }, | ||
{ errorResolvedBy: "User Name03" } | ||
]) | ||
|
||
const result = await listCourtCases( | ||
dataSource, | ||
{ maxPageItems: 100, resolvedByUsername: "User Name01" }, | ||
testUser | ||
) | ||
expect(isError(result)).toBe(false) | ||
const { result: cases } = result as ListCourtCaseResult | ||
|
||
expect(cases).toHaveLength(1) | ||
expect(cases[0].errorResolvedBy).toStrictEqual("User Name01") | ||
}) | ||
|
||
it("Should handle wildcard searches for partial usernames", async () => { | ||
await insertCourtCasesWithFields([ | ||
{ errorResolvedBy: "User Name01" }, | ||
{ triggerResolvedBy: "User Name02" }, | ||
{ errorResolvedBy: "User Name03" } | ||
]) | ||
|
||
const result = await listCourtCases(dataSource, { maxPageItems: 100, resolvedByUsername: "%Name0%" }, testUser) | ||
expect(isError(result)).toBe(false) | ||
const { result: cases } = result as ListCourtCaseResult | ||
|
||
expect(cases).toHaveLength(3) | ||
expect(cases[0].errorResolvedBy).toStrictEqual("User Name01") | ||
expect(cases[1].triggerResolvedBy).toStrictEqual("User Name02") | ||
expect(cases[2].errorResolvedBy).toStrictEqual("User Name03") | ||
}) | ||
}) | ||
}) |
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,33 @@ | ||
import { formatName } from "../../../src/helpers/formatName" | ||
|
||
describe("formatName", () => { | ||
it('should replace spaces with "%" and add "%" at the end', () => { | ||
expect(formatName("John Doe")).toBe("John%Doe%") | ||
}) | ||
|
||
it('should replace "*" with "%" and add "%" at the end', () => { | ||
expect(formatName("John*Doe")).toBe("John%Doe%") | ||
}) | ||
|
||
it('should not add another "%" if already present at the end', () => { | ||
expect(formatName("John Doe%")).toBe("John%Doe%") | ||
}) | ||
|
||
it('should add "%" if there are no spaces or special characters', () => { | ||
expect(formatName("John")).toBe("John%") | ||
}) | ||
|
||
it("should handle empty string", () => { | ||
expect(formatName("")).toBe("%") | ||
}) | ||
|
||
it("should handle a wildcard character anywhere in the string", () => { | ||
expect(formatName("Name0%")).toBe("Name0%") | ||
expect(formatName("User%Name")).toBe("User%Name") | ||
expect(formatName("%Name")).toBe("%Name") | ||
}) | ||
|
||
it('should still replace spaces and append "%" at the end if no wildcard is present', () => { | ||
expect(formatName("Name 0")).toBe("Name%0%") | ||
}) | ||
}) |
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