Skip to content

Commit

Permalink
(feat) O3-2484: The application should revert to default locale on lo…
Browse files Browse the repository at this point in the history
…gout (#782)
  • Loading branch information
vasharma05 authored Oct 29, 2023
1 parent fd113a4 commit e18c5dd
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect } from "react";
import {
navigate,
setUserLanguage,
useConfig,
useConnectivity,
useSession,
Expand All @@ -19,6 +20,13 @@ const RedirectLogout: React.FC<RedirectLogoutProps> = () => {
navigate({ to: "${openmrsSpaBase}/login" });
} else {
performLogout().then(() => {
const defaultLang =
document.documentElement.getAttribute("data-default-lang");
setUserLanguage({
locale: defaultLang,
authenticated: false,
sessionId: "",
});
if (config.provider.type === "oauth2") {
navigate({ to: config.provider.logoutUrl });
} else {
Expand Down
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" });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function isValidLocale(locale: unknown): locale is string {
return true;
}

function setUserLanguage(data: Session) {
export function setUserLanguage(data: Session) {
const locale = data?.user?.userProperties?.defaultLocale ?? data.locale;
const htmlLang = document.documentElement.getAttribute("lang");

Expand Down
21 changes: 21 additions & 0 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [saveVisit](API.md#savevisit)
- [setCurrentVisit](API.md#setcurrentvisit)
- [setSessionLocation](API.md#setsessionlocation)
- [setUserLanguage](API.md#setuserlanguage)
- [toLocationObject](API.md#tolocationobject)
- [toVisitTypeObject](API.md#tovisittypeobject)
- [updateVisit](API.md#updatevisit)
Expand Down Expand Up @@ -1360,6 +1361,26 @@ ___

___

### setUserLanguage

**setUserLanguage**(`data`): `void`

#### Parameters

| Name | Type |
| :------ | :------ |
| `data` | [`Session`](interfaces/Session.md) |

#### Returns

`void`

#### Defined in

[packages/framework/esm-api/src/shared-api-objects/current-user.ts:131](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L131)

___

### toLocationObject

**toLocationObject**(`openmrsRestForm`): [`Location`](interfaces/Location.md)
Expand Down
2 changes: 1 addition & 1 deletion packages/shell/esm-app-shell/src/index.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="<%= openmrsDefaultLocale %>">
<html lang="<%= openmrsDefaultLocale %>" data-default-lang="<%= openmrsDefaultLocale %>">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
Expand Down

0 comments on commit e18c5dd

Please sign in to comment.