-
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.
- Loading branch information
1 parent
82fc438
commit ce93b79
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
editor.planx.uk/src/@planx/components/Pay/Public/FeeBreakdown/useFeeBreakdown.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,116 @@ | ||
import { logger } from "airbrake"; | ||
import { useStore } from "pages/FlowEditor/lib/store"; | ||
import { vi } from "vitest"; | ||
|
||
import { useFeeBreakdown } from "./useFeeBreakdown"; | ||
|
||
vi.mock("pages/FlowEditor/lib/store", () => ({ | ||
useStore: vi.fn(), | ||
})); | ||
|
||
vi.mock("airbrake", () => ({ | ||
logger: { | ||
notify: vi.fn(), | ||
}, | ||
})); | ||
|
||
describe("useFeeBreakdown() hook", () => { | ||
describe("valid data", () => { | ||
it("returns a fee breakdown for number inputs", () => { | ||
const mockPassportData = { | ||
"application.fee.calculated": 1000, | ||
"application.fee.payable": 800, | ||
"application.fee.payable.vat": 160, | ||
}; | ||
|
||
vi.mocked(useStore).mockReturnValue([mockPassportData, "test-session"]); | ||
|
||
const result = useFeeBreakdown(); | ||
|
||
expect(result).toEqual({ | ||
applicationFee: 1000, | ||
total: 800, | ||
reduction: 200, | ||
vat: 160, | ||
}); | ||
}); | ||
|
||
it("returns a fee breakdown for number tuple inputs", () => { | ||
const mockPassportData = { | ||
"application.fee.calculated": [1000], | ||
"application.fee.payable": [800], | ||
"application.fee.payable.vat": [160], | ||
}; | ||
|
||
vi.mocked(useStore).mockReturnValue([mockPassportData, "test-session"]); | ||
|
||
const result = useFeeBreakdown(); | ||
|
||
expect(result).toEqual({ | ||
applicationFee: 1000, | ||
total: 800, | ||
reduction: 200, | ||
vat: 160, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("invalid inputs", () => { | ||
it("returns undefined for missing data", () => { | ||
const mockPassportData = {}; | ||
|
||
vi.mocked(useStore).mockReturnValue([mockPassportData, "test-session"]); | ||
|
||
const result = useFeeBreakdown(); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it("returns undefined for partial data", () => { | ||
const mockPassportData = { | ||
"application.fee.calculated": [1000], | ||
"application.fee.payable.vat": [160], | ||
}; | ||
|
||
vi.mocked(useStore).mockReturnValue([mockPassportData, "test-session"]); | ||
|
||
const result = useFeeBreakdown(); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it("returns undefined for incorrect data", () => { | ||
const mockPassportData = { | ||
"application.fee.calculated": "some string", | ||
"application.fee.payable": [800, 700], | ||
"application.fee.payable.vat": false, | ||
}; | ||
|
||
vi.mocked(useStore).mockReturnValue([mockPassportData, "test-session"]); | ||
|
||
const result = useFeeBreakdown(); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it("calls Airbrake if invalid inputs are provided", () => { | ||
const mockPassportData = {}; | ||
const mockSessionId = "test-session"; | ||
|
||
vi.mocked(useStore).mockReturnValue([mockPassportData, mockSessionId]); | ||
const loggerSpy = vi.spyOn(logger, "notify"); | ||
|
||
const result = useFeeBreakdown(); | ||
|
||
expect(result).toBeUndefined(); | ||
|
||
expect(loggerSpy).toHaveBeenCalledWith( | ||
expect.stringContaining(mockSessionId), | ||
); | ||
|
||
expect(loggerSpy).toHaveBeenCalledWith( | ||
expect.stringContaining("ZodError"), | ||
); | ||
}); | ||
}); | ||
}); |