Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

MF-56: Add logout to primary nav bar #5

Merged
merged 3 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions __mocks__/openmrs-esm-api.mock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { of } from "rxjs";

export function openmrsFetch() {
return new Promise(() => {});
}

export function getCurrentUser() {
return of({ authenticated: false });
}
1 change: 1 addition & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"^.+\\.tsx?$": "babel-jest"
},
"moduleNameMapper": {
"@openmrs/esm-api": "<rootDir>/__mocks__/openmrs-esm-api.mock.tsx",
"\\.(css)$": "identity-obj-proxy"
}
}
116 changes: 107 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
"kremling": "^2.0.1",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-router-dom": "^5.0.1",
"rxjs": "^6.5.3",
"single-spa-react": "^2.10.2"
}
}
1 change: 1 addition & 0 deletions src/openmrs-esm-primary-navigation.d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

declare module "@openmrs/esm-api" {
export function openmrsFetch(url: string, fetchInit: any): any;
export function getCurrentUser(opts?: any): any;
}

declare module "*.css";
73 changes: 71 additions & 2 deletions src/root.component.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,80 @@
import React from "react";
import { render, cleanup } from "@testing-library/react";
import { render, cleanup, fireEvent, wait } from "@testing-library/react";
import Root from "./root.component";
import { of } from "rxjs";
import { getCurrentUser } from "@openmrs/esm-api";
import { act } from "react-dom/test-utils";
import { createMemoryHistory } from "history";
import { Router } from "react-router";

function renderWithRouter(
ui,
{
route = "/",
history = createMemoryHistory({ initialEntries: [route] }),
match = { params: {}, isExact: true, path: "", url: "" }
} = {}
) {
return {
...render(<Router history={history}>{ui}</Router>),
history,
match
};
}

const mockGetCurrentUser = getCurrentUser as jest.Mock;
window["getOpenmrsSpaBase"] = jest.fn().mockImplementation(() => "/");
afterAll(cleanup);

const mockUser = {
authenticated: true,
user: {
uuid: "uuid",
display: "admin",
person: { uuid: "uuid", display: "Test User" },
privileges: [],
roles: [{ uuid: "uuid", display: "System Developer" }]
}
};

jest.mock("@openmrs/esm-api", () => ({
openmrsFetch: jest.fn().mockResolvedValue({}),
getCurrentUser: jest
.fn()
.mockImplementation(() => ({ subscribe: () => {}, unsubscribe: () => {} }))
}));

describe(`<Root />`, () => {
it(`renders without dying`, () => {
const wrapper = render(<Root />);
let wrapper;
act(() => {
wrapper = render(<Root />);
});
});

it(`renders avatar`, async () => {
mockGetCurrentUser.mockImplementation(() => of(mockUser));
let wrapper;
act(() => {
wrapper = render(<Root />);
});
await wait(() => expect(wrapper.getByText("TU")).not.toBeNull());
});

it(`logs out patient`, async () => {
mockGetCurrentUser.mockImplementation(() => of(mockUser));
let wrapper;
act(() => {
wrapper = renderWithRouter(<Root />);
});
const userMenuBtn = wrapper.container.querySelector(".avatar");
act(() => {
fireEvent.click(userMenuBtn);
});
const logoutBtn = wrapper.getByText(/Logout/i);
expect(logoutBtn).not.toBeNull();
act(() => {
fireEvent.click(logoutBtn);
});
});
});