-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) O3-2484: The application should revert to default locale on lo…
…gout (#782)
- Loading branch information
1 parent
fd113a4
commit e18c5dd
Showing
5 changed files
with
137 additions
and
2 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
106 changes: 106 additions & 0 deletions
106
packages/apps/esm-login-app/src/redirect-logout/redirect-logout.test.tsx
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,106 @@ | ||
import React from "react"; | ||
import { cleanup, render, waitFor } from "@testing-library/react"; | ||
import RedirectLogout from "./redirect-logout.component"; | ||
import { performLogout } from "./logout.resource"; | ||
import { | ||
Session, | ||
clearCurrentUser, | ||
navigate, | ||
openmrsFetch, | ||
refetchCurrentUser, | ||
setUserLanguage, | ||
useConfig, | ||
useConnectivity, | ||
useSession, | ||
} from "@openmrs/esm-framework"; | ||
import { mutate } from "swr"; | ||
|
||
jest.mock("@openmrs/esm-framework", () => ({ | ||
navigate: jest.fn(), | ||
setUserLanguage: jest.fn(), | ||
useConfig: jest.fn(), | ||
useConnectivity: jest.fn(), | ||
useSession: jest.fn(), | ||
clearCurrentUser: jest.fn(), | ||
openmrsFetch: jest.fn(), | ||
refetchCurrentUser: jest.fn(), | ||
})); | ||
|
||
jest.mock("swr", () => ({ | ||
mutate: jest.fn(), | ||
})); | ||
|
||
Object.defineProperty(document, "documentElement", { | ||
value: { | ||
getAttribute: jest.fn().mockReturnValue("km"), | ||
}, | ||
}); | ||
|
||
describe("Testing Logout", () => { | ||
beforeEach(() => { | ||
cleanup(); | ||
jest.clearAllMocks(); | ||
(useConnectivity as jest.Mock).mockReturnValue(true); | ||
(openmrsFetch as jest.Mock).mockResolvedValue({}); | ||
(useSession as jest.Mock).mockReturnValue({ | ||
authenticated: true, | ||
sessionId: "xyz", | ||
} as Session); | ||
(useConfig as jest.Mock).mockReturnValue({ | ||
provider: { | ||
type: "", | ||
}, | ||
}); | ||
}); | ||
it("should render Logout and redirect to login page", async () => { | ||
render(<RedirectLogout />); | ||
expect(openmrsFetch).toBeCalledWith("/ws/rest/v1/session", { | ||
method: "DELETE", | ||
}); | ||
await waitFor(() => expect(mutate).toBeCalled()); | ||
expect(clearCurrentUser).toBeCalled(); | ||
expect(refetchCurrentUser).toBeCalled(); | ||
expect(setUserLanguage).toBeCalledWith({ | ||
locale: "km", | ||
authenticated: false, | ||
sessionId: "", | ||
}); | ||
expect(navigate).toBeCalledWith({ to: "${openmrsSpaBase}/login" }); | ||
}); | ||
|
||
it("should render Logout and redirect to provider.logoutUrl if provider.type === oauth2", async () => { | ||
(useConfig as jest.Mock).mockReturnValue({ | ||
provider: { | ||
type: "oauth2", | ||
logoutUrl: "/oauth/logout", | ||
}, | ||
}); | ||
render(<RedirectLogout />); | ||
expect(openmrsFetch).toBeCalledWith("/ws/rest/v1/session", { | ||
method: "DELETE", | ||
}); | ||
await waitFor(() => expect(mutate).toBeCalled()); | ||
expect(clearCurrentUser).toBeCalled(); | ||
expect(refetchCurrentUser).toBeCalled(); | ||
expect(setUserLanguage).toBeCalledWith({ | ||
locale: "km", | ||
authenticated: false, | ||
sessionId: "", | ||
}); | ||
expect(navigate).toBeCalledWith({ to: "/oauth/logout" }); | ||
}); | ||
|
||
it("should redirect to login if the session is already unauthenticated", async () => { | ||
(useSession as jest.Mock).mockReturnValue({ | ||
authenticated: false, | ||
} as Session); | ||
render(<RedirectLogout />); | ||
expect(navigate).toBeCalledWith({ to: "${openmrsSpaBase}/login" }); | ||
}); | ||
|
||
it("should redirect to login if the application is Offline", async () => { | ||
(useConnectivity as jest.Mock).mockReturnValue(false); | ||
render(<RedirectLogout />); | ||
expect(navigate).toBeCalledWith({ to: "${openmrsSpaBase}/login" }); | ||
}); | ||
}); |
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
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
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