Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/eps-prescription-tracker-ui.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
{
"name": "packages/common/lambdaUtils",
"path": "../packages/common/lambdaUtils"
}
},
{
"name": "packages/common/testing",
"path": "../packages/common/testing"
Expand Down
2 changes: 0 additions & 2 deletions packages/common/commonTypes/src/trackerUserInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ export type TrackerUserInfo = {
export type TrackerUserInfoResult = {
rolesWithAccess: Array<RoleDetails>,
rolesWithoutAccess: Array<RoleDetails>,
hasNoAccess: boolean
selectedRole: RoleDetails | undefined,
userDetails: UserDetails | undefined,
hasSingleRoleAccess: boolean,
isConcurrentSession: boolean,
invalidSessionCause: string | undefined,
sessionId: string | undefined,
Expand Down
10 changes: 1 addition & 9 deletions packages/cpt-ui/__tests__/AuthProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ const userDetails = {
const mockUserInfo = {
rolesWithAccess: rolesWithAccess,
rolesWithoutAccess: [],
hasNoAccess: false,
selectedRole: currentlySelectedRole,
userDetails: userDetails,
hasSingleRoleAccess: true,
error: undefined
}

Expand Down Expand Up @@ -142,10 +140,8 @@ const TestConsumer = () => {
<div data-testid="isSigningIn">{auth.isSigningIn.toString()}</div>
<div data-testid="rolesWithAccess">{JSON.stringify(auth.rolesWithAccess, null, 2)}</div>
<div data-testid="rolesWithoutAccess">{JSON.stringify(auth.rolesWithoutAccess, null, 2)}</div>
<div data-testid="noAccess">{auth.hasNoAccess.toString()}</div>
<div data-testid="selectedRole">{JSON.stringify(auth.selectedRole, null, 2)}</div>
<div data-testid="userDetails">{JSON.stringify(auth.userDetails, null, 2)}</div>
<div data-testid="singleAccess">{auth.hasSingleRoleAccess.toString()}</div>
</div>
)
}
Expand Down Expand Up @@ -265,10 +261,8 @@ describe("AuthProvider", () => {
expect(screen.getByTestId("user").textContent).toBe("test_user")
expect(screen.getByTestId("rolesWithAccess").textContent).toBe(JSON.stringify(rolesWithAccess, null, 2))
expect(screen.getByTestId("rolesWithoutAccess").textContent).toBe("[]")
expect(screen.getByTestId("noAccess").textContent).toBe("false")
expect(screen.getByTestId("selectedRole").textContent).toBe(JSON.stringify(currentlySelectedRole, null, 2))
expect(screen.getByTestId("userDetails").textContent).toBe(JSON.stringify(userDetails, null, 2))
expect(screen.getByTestId("singleAccess").textContent).toBe("true")
})
})

Expand Down Expand Up @@ -326,10 +320,8 @@ describe("AuthProvider", () => {
expect(screen.getByTestId("error").textContent).toBe("")
expect(screen.getByTestId("rolesWithAccess").textContent).toBe("[]")
expect(screen.getByTestId("rolesWithoutAccess").textContent).toBe("[]")
expect(screen.getByTestId("noAccess").textContent).toBe("true")
expect(screen.getByTestId("selectedRole").textContent).toBe("")
expect(screen.getByTestId("userDetails").textContent).toBe("")
expect(screen.getByTestId("singleAccess").textContent).toBe("false")
})
})

Expand Down Expand Up @@ -395,7 +387,7 @@ describe("AuthProvider", () => {
};

(updateRemoteSelectedRole as jest.Mock).mockResolvedValue({
rolesWithAccess: [newRole]
currentlySelectedRole: newRole
})

let contextValue: AuthContextType | null
Expand Down
3 changes: 1 addition & 2 deletions packages/cpt-ui/__tests__/BasicDetailsSearch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ const signedInAuthState: AuthContextType = {
error: null,
rolesWithAccess: [],
rolesWithoutAccess: [],
hasNoAccess: false,
hasSingleRoleAccess: false,
selectedRole: undefined,
userDetails: undefined,
isConcurrentSession: false,
sessionId: "test-session-id",
cognitoSignIn: mockCognitoSignIn,
cognitoSignOut: mockCognitoSignOut,
clearAuthState: mockClearAuthState,
hasSingleRoleAccess: jest.fn().mockReturnValue(false),
updateSelectedRole: jest.fn(),
updateTrackerUserInfo: jest.fn(),
updateInvalidSessionCause: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ const mockAuthContext: AuthContextType = {
invalidSessionCause: undefined,
rolesWithAccess: [],
rolesWithoutAccess: [],
hasNoAccess: false,
hasSingleRoleAccess: false,
selectedRole: undefined,
userDetails: undefined,
isConcurrentSession: false,
sessionId: undefined,
cognitoSignIn: jest.fn(),
cognitoSignOut: jest.fn(),
clearAuthState: jest.fn(),
hasSingleRoleAccess: jest.fn().mockReturnValue(false),
updateSelectedRole: jest.fn(),
updateTrackerUserInfo: jest.fn(),
updateInvalidSessionCause: jest.fn(),
Expand Down
3 changes: 1 addition & 2 deletions packages/cpt-ui/__tests__/EpsHeader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ const defaultAuthContext: AuthContextType = {
invalidSessionCause: undefined,
rolesWithAccess: [],
rolesWithoutAccess: [],
hasNoAccess: false,
hasSingleRoleAccess: false,
selectedRole: undefined,
userDetails: undefined,
isConcurrentSession: false,
sessionId: undefined,
cognitoSignIn: jest.fn(),
cognitoSignOut: jest.fn(),
clearAuthState: jest.fn(),
hasSingleRoleAccess: jest.fn().mockReturnValue(false),
updateSelectedRole: jest.fn(),
updateTrackerUserInfo: jest.fn(),
updateInvalidSessionCause: jest.fn(),
Expand Down
3 changes: 1 addition & 2 deletions packages/cpt-ui/__tests__/EpsPrescriptionList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,14 @@ const signedInAuthState: AuthContextType = {
error: null,
rolesWithAccess: [],
rolesWithoutAccess: [],
hasNoAccess: false,
hasSingleRoleAccess: false,
selectedRole: undefined,
userDetails: undefined,
isConcurrentSession: false,
sessionId: undefined,
cognitoSignIn: mockCognitoSignIn,
cognitoSignOut: mockCognitoSignOut,
clearAuthState: jest.fn(),
hasSingleRoleAccess: jest.fn().mockReturnValue(false),
updateSelectedRole: jest.fn(),
updateTrackerUserInfo: jest.fn(),
updateInvalidSessionCause: jest.fn(),
Expand Down
83 changes: 48 additions & 35 deletions packages/cpt-ui/__tests__/EpsRoleSelectionPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {FRONTEND_PATHS} from "@/constants/environment"
import {getSearchParams} from "@/helpers/getSearchParams"
import {handleRestartLogin} from "@/helpers/logout"
import axios from "axios"
import {RoleDetails} from "@cpt-ui-common/common-types"

jest.mock("@/context/AuthProvider")
jest.mock("@/helpers/getSearchParams")
Expand Down Expand Up @@ -89,11 +90,11 @@ describe("RoleSelectionPage", () => {
it("renders loading spinner if redirecting during sign in", () => {
mockUseAuth.mockReturnValue({
isSigningIn: true,
hasNoAccess: false,
rolesWithAccess: [],
rolesWithoutAccess: [],
error: null,
clearAuthState: jest.fn()
clearAuthState: jest.fn(),
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
})
mockGetSearchParams.mockReturnValue({
codeParams: "foo",
Expand All @@ -107,24 +108,24 @@ describe("RoleSelectionPage", () => {
it("renders error message if auth.error exists", () => {
mockUseAuth.mockReturnValue({
isSigningIn: false,
hasNoAccess: false,
rolesWithAccess: [],
rolesWithoutAccess: [],
error: "Something went wrong"
error: "Something went wrong",
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
})

render(<RoleSelectionPage contentText={defaultContentText} />)
expect(screen.getByText("There was an error selecting your role")).toBeInTheDocument()
expect(screen.getByText("Something went wrong")).toBeInTheDocument()
})

it("renders titleNoAccess and captionNoAccess when hasNoAccess is true", () => {
it("renders titleNoAccess and captionNoAccess when rolesWithAccess is empty", () => {
mockUseAuth.mockReturnValue({
isSigningIn: false,
hasNoAccess: true,
rolesWithAccess: [],
rolesWithoutAccess: [],
error: null
error: null,
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
})

render(<RoleSelectionPage contentText={defaultContentText} />)
Expand All @@ -139,13 +140,12 @@ describe("RoleSelectionPage", () => {
mockUseAuth.mockReturnValue({
isSigningIn: true,
isSignedIn: false,
hasSingleRoleAccess: true,
hasNoAccess: false,
rolesWithAccess: [],
rolesWithoutAccess: [],
selectedRole: null,
error: null,
clearAuthState: jest.fn()
clearAuthState: jest.fn(),
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
})
mockGetSearchParams.mockReturnValue({
codeParams: undefined,
Expand All @@ -157,19 +157,25 @@ describe("RoleSelectionPage", () => {
expect(navigateMock).toHaveBeenCalledWith(FRONTEND_PATHS.LOGIN)
})

it("redirects if user hasSingleRoleAccess", () => {
it("redirects if user has single roleWithAccess", () => {
const navigateMock = jest.fn()
mockNavigate.mockReturnValue(navigateMock)

mockUseAuth.mockReturnValue({
isSigningIn: false,
isSignedIn: true,
hasSingleRoleAccess: true,
hasNoAccess: false,
rolesWithAccess: [],
rolesWithAccess: [
{
role_id: "1",
role_name: "Pharmacist",
org_code: "ABC",
org_name: "Pharmacy Org"
}
],
rolesWithoutAccess: [],
selectedRole: null,
error: null
error: null,
hasSingleRoleAccess: jest.fn().mockReturnValue(true)
})

render(<RoleSelectionPage contentText={defaultContentText} />)
Expand All @@ -180,8 +186,6 @@ describe("RoleSelectionPage", () => {
it("renders login info when selectedRole is present", () => {
mockUseAuth.mockReturnValue({
isSigningIn: false,
hasSingleRoleAccess: false,
hasNoAccess: false,
selectedRole: {
org_name: "Test Org",
org_code: "TEST123",
Expand All @@ -190,7 +194,8 @@ describe("RoleSelectionPage", () => {
},
rolesWithAccess: [],
rolesWithoutAccess: [],
error: null
error: null,
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
})

render(<RoleSelectionPage contentText={defaultContentText} />)
Expand All @@ -202,7 +207,6 @@ describe("RoleSelectionPage", () => {
it("renders roles without access in table", () => {
mockUseAuth.mockReturnValue({
isSigningIn: false,
hasNoAccess: false,
rolesWithAccess: [],
rolesWithoutAccess: [
{
Expand All @@ -211,7 +215,8 @@ describe("RoleSelectionPage", () => {
org_code: "NO123"
}
],
error: null
error: null,
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
})

render(<RoleSelectionPage contentText={defaultContentText} />)
Expand All @@ -224,7 +229,6 @@ describe("RoleSelectionPage", () => {
it("renders EpsCard components for roles with access", () => {
mockUseAuth.mockReturnValue({
isSigningIn: false,
hasNoAccess: false,
selectedRole: {
role_id: "1"
},
Expand All @@ -249,7 +253,8 @@ describe("RoleSelectionPage", () => {
}
],
rolesWithoutAccess: [],
error: null
error: null,
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
})

render(<RoleSelectionPage contentText={defaultContentText} />)
Expand All @@ -265,7 +270,6 @@ describe("RoleSelectionPage", () => {
it("navigates on confirm and continue button click", async () => {
mockUseAuth.mockReturnValue({
isSigningIn: false,
hasNoAccess: false,
selectedRole: {
role_id: "1",
org_name: "Pharmacy A",
Expand All @@ -274,7 +278,8 @@ describe("RoleSelectionPage", () => {
},
rolesWithAccess: [],
rolesWithoutAccess: [],
error: null
error: null,
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
})

render(<RoleSelectionPage contentText={defaultContentText} />)
Expand All @@ -284,7 +289,6 @@ describe("RoleSelectionPage", () => {
expect(button).toBeEnabled()
fireEvent.click(button)

expect(mockNavigate.mock.calls.length).toBe(5)
expect(mockNavigate).toHaveBeenCalledWith("/continue")
})

Expand All @@ -293,13 +297,12 @@ describe("RoleSelectionPage", () => {
const authState = {
isSigningIn: true,
isSignedIn: false,
hasSingleRoleAccess: false,
hasNoAccess: false,
rolesWithAccess: [],
rolesWithAccess: [] as Array<RoleDetails>,
rolesWithoutAccess: [],
selectedRole: null,
selectedRole: undefined as RoleDetails | undefined,
error: null,
clearAuthState: jest.fn()
clearAuthState: jest.fn(),
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
}

mockUseAuth.mockReturnValue(authState)
Expand All @@ -317,10 +320,17 @@ describe("RoleSelectionPage", () => {
// Step 2: Simulate login complete and role assignment
act(() => {
authState.isSigningIn = false
authState.isSignedIn = true
authState.hasSingleRoleAccess = true
const role = {
role_id: "2",
role_name: "Pharmacist",
org_code: "ABC",
org_name: "Pharmacy Org"
}
authState.rolesWithAccess = [role]
authState.selectedRole = role
authState.isSignedIn = true
mockUseAuth.mockReturnValue(authState)
authState.hasSingleRoleAccess = jest.fn().mockReturnValue(true)
})

rerender(<RoleSelectionPage contentText={defaultContentText} />)
Expand All @@ -346,7 +356,8 @@ describe("RoleSelectionPage", () => {
rolesWithAccess: [roleWithAccess],
rolesWithoutAccess: [],
error: null,
updateSelectedRole: mockUpdateSelectedRole
updateSelectedRole: mockUpdateSelectedRole,
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
})
})

Expand Down Expand Up @@ -506,7 +517,8 @@ describe("RoleSelectionPage", () => {
rolesWithAccess: [incompleteRole],
rolesWithoutAccess: [],
error: null,
updateSelectedRole: mockUpdateSelectedRole
updateSelectedRole: mockUpdateSelectedRole,
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
})

render(<RoleSelectionPage contentText={defaultContentText} />)
Expand All @@ -532,7 +544,8 @@ describe("RoleSelectionPage", () => {
rolesWithAccess: [selectedRole, roleWithAccess],
rolesWithoutAccess: [],
error: null,
updateSelectedRole: mockUpdateSelectedRole
updateSelectedRole: mockUpdateSelectedRole,
hasSingleRoleAccess: jest.fn().mockReturnValue(false)
})

render(<RoleSelectionPage contentText={defaultContentText} />)
Expand Down
Loading
Loading