Skip to content
Open
23 changes: 22 additions & 1 deletion __mocks__/db/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const usersData = [
'incompleteUserDetails': false,
'website': '.',
'roles': {
'archived': false
'archived': false,
},
'linkedin_id': '.',
'last_name': 'Musab',
Expand Down Expand Up @@ -193,6 +193,27 @@ const usersData = [
'first_name': 'Arpit',
'username': 'Arpit_02'
},
{
'id': 'iMxYUdnETvu8JOmDq03p',
'incompleteUserDetails': false,
'website': '.',
'roles': {
'archived': false,
'admin': true,
'super_user': true,
},
'linkedin_id': '.',
'last_name': 'Musab',
'yoe': 1,
'instagram_id': '.',
'github_display_name': null,
'company': 'NUST',
'github_id': 'Muhammad-Musab',
'designation': 'student',
'twitter_id': 'direksh',
'first_name': 'Muhammad',
'username': 'muhammadmusab'
},
];

export default usersData;
2 changes: 0 additions & 2 deletions __mocks__/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import levelsHandler from './handlers/levels.handler';
import usersHandler from './handlers/users.handler';
import taskTagsHandler from './handlers/taskTags.handler';
import { taskDetailsHandler } from './handlers/task-details.handler';
import userHandler from './handlers/user.handler';
import issuesHandler from './handlers/issues.handler';
import standupHandler from './handlers/standup.handler';
import { prsHandler } from './handlers/pull-requests.handler';
Expand All @@ -24,7 +23,6 @@ const handlers = [
...usersHandler,
...taskTagsHandler,
...taskDetailsHandler,
...userHandler,
...issuesHandler,
...standupHandler,
...prsHandler,
Expand Down
8 changes: 8 additions & 0 deletions __mocks__/handlers/self.handler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { USER_SELF } from '@/constants/url';
import usersData from '../../__mocks__/db/users';
import { rest } from 'msw';
const URL = process.env.NEXT_PUBLIC_BASE_URL;

Expand Down Expand Up @@ -29,4 +31,10 @@ const selfHandler = [
}),
];

export const adminUserHandler = [
rest.get(USER_SELF, (_, res, ctx) => {
return res(ctx.status(200), ctx.json(usersData[11]));
}),
];

export default selfHandler;
11 changes: 0 additions & 11 deletions __mocks__/handlers/user.handler.ts

This file was deleted.

35 changes: 19 additions & 16 deletions __tests__/Unit/Components/Tasks/Card.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fireEvent, screen } from '@testing-library/react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { screen } from '@testing-library/react';
import { act } from '@testing-library/react-hooks';
import Card from '@/components/tasks/card/index';
import { store } from '@/app/store';
Expand All @@ -10,6 +11,11 @@ import {
} from '@/test_utils/createMockRouter';
import { NextRouter } from 'next/router';
import { TASK_STATUS } from '@/interfaces/task-status';
import useUserData from '@/hooks/useUserData';
import { setupServer } from 'msw/node';
import { adminUserHandler } from '../../../../__mocks__/handlers/self.handler';

const server = setupServer();

const DEFAULT_PROPS = {
content: {
Expand Down Expand Up @@ -40,19 +46,6 @@ const DEFAULT_PROPS = {
onContentChange: jest.fn(),
};

jest.mock('@/hooks/useUserData', () => {
return () => ({
data: {
roles: {
admin: true,
super_user: true,
},
},
isUserAuthorized: true,
isSuccess: true,
});
});

const getFirestoreDateNDaysBefore = (n = 1) => {
const d = new Date();
d.setDate(d.getDate() - n);
Expand All @@ -61,6 +54,13 @@ const getFirestoreDateNDaysBefore = (n = 1) => {

jest.useFakeTimers();
describe('Task card', () => {
beforeAll(() => {
server.listen();
});

afterEach(() => server.resetHandlers());
afterAll(() => server.close());

test('Should render card', () => {
const { getByText } = renderWithRouter(
<Provider store={store()}>
Expand Down Expand Up @@ -123,7 +123,8 @@ describe('Task card', () => {
expect(queryByTestId('task-card')).toBeInTheDocument();
});

test('should show edit button when ALT key is long pressed', () => {
test('should show edit button when ALT key is long pressed', async () => {
server.use(...adminUserHandler);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using aminUserHandler here when we are not doing any testing related to it?

Copy link
Contributor Author

@Shah-Arsalan Shah-Arsalan Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beacuse the code is written as {isUserAuthorized && showEditButton && <EditButton />} . So to get the isUserAuthorized values as true we need to use adminUserHandler. The long press of ALT key is just turning the boolean showEditButton from false to true.

const { getByTestId, queryByTestId } = renderWithRouter(
<Provider store={store()}>
<Card {...DEFAULT_PROPS} />
Expand All @@ -138,7 +139,9 @@ describe('Task card', () => {
jest.advanceTimersByTime(300);
});

expect(queryByTestId('edit-button')).toBeInTheDocument();
await waitFor(() => {
expect(queryByTestId('edit-button')).toBeInTheDocument();
});
});
test('task should be editable if edit button clicked', async () => {
const { getByTestId, queryByTestId } = renderWithRouter(
Expand Down
Loading