Skip to content

Commit

Permalink
UserType fix part 2 (PalisadoesFoundation#1894)
Browse files Browse the repository at this point in the history
* add tests

* add tests

* fix type and remove userType

* remove userType

* remove userType

* fix type and userType

* code cov

* Fix async issue in Events.test.tsx

* code cov for Users
  • Loading branch information
pranshugupta54 authored Apr 9, 2024
1 parent f810f9c commit 2da9090
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 72 deletions.
2 changes: 0 additions & 2 deletions src/components/OrganizationScreen/OrganizationScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ const clickToggleMenuBtn = (toggleButton: HTMLElement): void => {

describe('Testing LeftDrawer in OrganizationScreen', () => {
test('Testing LeftDrawer in page functionality', async () => {
setItem('UserType', 'SUPERADMIN');

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const MOCKS = [
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'User',
},
{
_id: '64001660a711c62d5b4076a3',
Expand All @@ -74,7 +73,6 @@ const MOCKS = [
image: 'mockImage',
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'User',
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe('SecuredRouteForUser', () => {
test('renders the route when the user is logged in', () => {
// Set the 'IsLoggedIn' value to 'TRUE' in localStorage to simulate a logged-in user and do not set 'AdminFor' so that it remains undefined.
setItem('IsLoggedIn', 'TRUE');
//setItem('UserType', 'USER');

render(
<MemoryRouter initialEntries={['/user/organizations']}>
Expand Down Expand Up @@ -59,7 +58,7 @@ describe('SecuredRouteForUser', () => {
});
});

test('renders the route when the user is logged in and userType is ADMIN', () => {
test('renders the route when the user is logged in and user is ADMIN', () => {
// Set the 'IsLoggedIn' value to 'TRUE' in localStorage to simulate a logged-in user and set 'AdminFor' to simulate ADMIN of some Organization.
setItem('IsLoggedIn', 'TRUE');
setItem('AdminFor', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import OrganizationDashboard from './OrganizationDashboard';
import { EMPTY_MOCKS, ERROR_MOCKS, MOCKS } from './OrganizationDashboardMocks';
import React from 'react';
const { setItem } = useLocalStorage();
import type { InterfaceQueryOrganizationEventListItem } from 'utils/interfaces';

async function wait(ms = 100): Promise<void> {
await act(() => {
Expand Down Expand Up @@ -45,7 +46,6 @@ jest.mock('react-router-dom', () => ({
beforeEach(() => {
setItem('FirstName', 'John');
setItem('LastName', 'Doe');
setItem('UserType', 'SUPERADMIN');
setItem(
'UserImage',
'https://api.dicebear.com/5.x/initials/svg?seed=John%20Doe',
Expand Down Expand Up @@ -179,7 +179,10 @@ describe('Organisation Dashboard Page', () => {
const mockSetState = jest.spyOn(React, 'useState');
jest.doMock('react', () => ({
...jest.requireActual('react'),
useState: (initial: any) => [initial, mockSetState],
useState: (initial: InterfaceQueryOrganizationEventListItem[]) => [
initial,
mockSetState,
],
}));
await act(async () => {
render(
Expand Down
2 changes: 0 additions & 2 deletions src/screens/OrganizationPeople/MockDataTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ type User = {
image: string | null;
_id: string;
email: string;
userType: string;
createdAt: string;
joinedOrganizations: {
__typename: string;
Expand All @@ -30,7 +29,6 @@ type Edge = {
image?: string | null;
email?: string;
createdAt?: string;
userType?: string;
user?: Edge;
};
export type TestMock = {
Expand Down
17 changes: 0 additions & 17 deletions src/screens/OrganizationPeople/OrganizationPeople.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const createMemberMock = (
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'USER',
},
],
},
Expand All @@ -65,7 +64,6 @@ const createMemberMock = (
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'USER',
},
},
],
Expand Down Expand Up @@ -99,7 +97,6 @@ const createAdminMock = (
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'USER',
},
},
],
Expand All @@ -120,7 +117,6 @@ const createAdminMock = (
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'USER',
lol: true,
},
},
Expand Down Expand Up @@ -152,7 +148,6 @@ const createUserMock = (
image: 'tempUrl',
_id: '64001660a711c62d5b4076a2',
email: '[email protected]',
userType: 'SUPERADMIN',
createdAt: '2023-03-02T03:22:08.101Z',
joinedOrganizations: [
{
Expand All @@ -170,7 +165,6 @@ const createUserMock = (
image: 'tempUrl',
_id: '6402030dce8e8406b8f07b0e',
email: '[email protected]',
userType: 'USER',
createdAt: '2023-03-03T14:24:13.084Z',
joinedOrganizations: [
{
Expand Down Expand Up @@ -279,7 +273,6 @@ const MOCKS: TestMock[] = [
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'USER',
},
],
},
Expand All @@ -297,7 +290,6 @@ const MOCKS: TestMock[] = [
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'USER',
},
],
},
Expand Down Expand Up @@ -325,7 +317,6 @@ const MOCKS: TestMock[] = [
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'USER',
},
],
},
Expand All @@ -344,7 +335,6 @@ const MOCKS: TestMock[] = [
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'USER',
lol: true,
},
],
Expand Down Expand Up @@ -373,7 +363,6 @@ const MOCKS: TestMock[] = [
image: 'tempUrl',
_id: '64001660a711c62d5b4076a2',
email: '[email protected]',
userType: 'SUPERADMIN',
createdAt: '2023-03-02T03:22:08.101Z',
joinedOrganizations: [
{
Expand All @@ -391,7 +380,6 @@ const MOCKS: TestMock[] = [
image: 'tempUrl',
_id: '6402030dce8e8406b8f07b0e',
email: '[email protected]',
userType: 'USER',
createdAt: '2023-03-03T14:24:13.084Z',
joinedOrganizations: [
{
Expand Down Expand Up @@ -437,7 +425,6 @@ const MOCKS: TestMock[] = [
image: null,
_id: '65378abd85008f171cf2990d',
email: '[email protected]',
userType: 'ADMIN',
createdAt: '2023-04-13T04:53:17.742Z',
joinedOrganizations: [
{
Expand Down Expand Up @@ -612,7 +599,6 @@ describe('Organization People Page', () => {
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'USER',
},
]);

Expand All @@ -630,7 +616,6 @@ describe('Organization People Page', () => {
image: 'tempUrl',
_id: '64001660a711c62d5b4076a2',
email: '[email protected]',
userType: 'SUPERADMIN',
createdAt: '2023-03-02T03:22:08.101Z',
joinedOrganizations: [
{
Expand All @@ -648,7 +633,6 @@ describe('Organization People Page', () => {
image: 'tempUrl',
_id: '6402030dce8e8406b8f07b0e',
email: '[email protected]',
userType: 'USER',
createdAt: '2023-03-03T14:24:13.084Z',
joinedOrganizations: [
{
Expand All @@ -668,7 +652,6 @@ describe('Organization People Page', () => {
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'USER',
},
]);

Expand Down
3 changes: 0 additions & 3 deletions src/screens/UserPortal/Chat/Chat.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const MOCKS = [
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'User',
},
{
_id: '64001660a711c62d5b4076a3',
Expand All @@ -42,7 +41,6 @@ const MOCKS = [
image: 'mockImage',
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'User',
},
],
},
Expand All @@ -68,7 +66,6 @@ const MOCKS = [
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'User',
},
],
},
Expand Down
8 changes: 7 additions & 1 deletion src/screens/UserPortal/Events/Events.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { ThemeProvider } from 'react-bootstrap';
import { createTheme } from '@mui/material';
import useLocalStorage from 'utils/useLocalstorage';

const { setItem, getItem } = useLocalStorage();

jest.mock('react-toastify', () => ({
toast: {
Expand Down Expand Up @@ -273,7 +276,10 @@ describe('Testing Events Screen [User Portal]', () => {
</BrowserRouter>
</MockedProvider>,
);

setItem('SuperAdmin', true); // testing userRole as Superadmin
await wait();
setItem('SuperAdmin', false);
setItem('AdminFor', ['123']); // testing userRole as Admin
await wait();
});

Expand Down
9 changes: 8 additions & 1 deletion src/screens/UserPortal/Events/Events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,14 @@ export default function events(): JSX.Element {
const [create] = useMutation(CREATE_EVENT_MUTATION);

const userId = getItem('id') as string;
const userRole = getItem('UserType') as string;

const superAdmin = getItem('SuperAdmin');
const adminFor = getItem('AdminFor');
const userRole = superAdmin
? 'SUPERADMIN'
: adminFor?.length > 0
? 'ADMIN'
: 'USER';

const createEvent = async (
e: ChangeEvent<HTMLFormElement>,
Expand Down
4 changes: 0 additions & 4 deletions src/screens/UserPortal/People/People.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const MOCKS = [
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'User',
},
{
_id: '64001660a711c62d5b4076a3',
Expand All @@ -43,7 +42,6 @@ const MOCKS = [
image: 'mockImage',
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'User',
},
],
},
Expand Down Expand Up @@ -71,7 +69,6 @@ const MOCKS = [
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'Admin',
},
],
},
Expand All @@ -98,7 +95,6 @@ const MOCKS = [
image: null,
email: '[email protected]',
createdAt: '2023-03-02T03:22:08.101Z',
userType: 'User',
},
],
},
Expand Down
17 changes: 14 additions & 3 deletions src/screens/UserPortal/People/People.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ interface InterfaceOrganizationCardProps {
sno: string;
}

interface InterfaceMember {
firstName: string;
lastName: string;
image: string;
_id: string;
email: string;
userType: string;
}

export default function people(): JSX.Element {
const { t } = useTranslation('translation', {
keyPrefix: 'userOrganizations',
Expand Down Expand Up @@ -76,9 +85,11 @@ export default function people(): JSX.Element {
});
};

const handleSearchByEnter = (e: any): void => {
const handleSearchByEnter = (
e: React.KeyboardEvent<HTMLInputElement>,
): void => {
if (e.key === 'Enter') {
const { value } = e.target;
const { value } = e.currentTarget;
handleSearch(value);
}
};
Expand Down Expand Up @@ -197,7 +208,7 @@ export default function people(): JSX.Element {
)
: /* istanbul ignore next */
members
).map((member: any, index) => {
).map((member: InterfaceMember, index) => {
const name = `${member.firstName} ${member.lastName}`;

const cardProps: InterfaceOrganizationCardProps = {
Expand Down
Loading

0 comments on commit 2da9090

Please sign in to comment.