Skip to content

Commit

Permalink
Update Progress Fetching Logic to Include dev Query Parameter (#1308)
Browse files Browse the repository at this point in the history
* added dev query in getProgressApi hook

* fix test naming

* add dev=flase test

* improve the progresses test
  • Loading branch information
AnujChhikara authored Jan 9, 2025
1 parent 00519da commit 3f71ebb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
21 changes: 17 additions & 4 deletions __mocks__/handlers/progresses.handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { rest } from 'msw';
import { mockGetTaskProgress } from '../db/progresses';

const URL = process.env.NEXT_PUBLIC_BASE_URL;

export const progressHandler = [
rest.get(`${URL}/progresses?taskId=OxYqJgf6Tyl90uci1mzs`, (_, res, ctx) => {
return res(ctx.status(200), ctx.json(mockGetTaskProgress));
}),
rest.get(
`${URL}/progresses?taskId=OxYqJgf6Tyl90uci1mzs`,
(req, res, ctx) => {
const devFlag = req.url.searchParams.get('dev');

const progressData = {
...mockGetTaskProgress,
data: mockGetTaskProgress.data.map((progress) =>
devFlag === 'true'
? { ...progress }
: { ...progress, userData: undefined }
),
};

return res(ctx.status(200), ctx.json(progressData));
}
),
];
52 changes: 52 additions & 0 deletions __tests__/Unit/hooks/progressesApi.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,64 @@ describe('useGetProgressDetails', () => {

await act(() => waitForNextUpdate());

const nextResponse = result.current;
const taskProgressResponse = nextResponse.data;
expect(nextResponse.status).toEqual('fulfilled');
expect(nextResponse.error).toBeUndefined();
expect(nextResponse.isSuccess).toEqual(true);
expect(taskProgressResponse).not.toBeUndefined();
expect(taskProgressResponse?.data[0].userData).toBeUndefined();
});
test('Returns Task Progress with userData in response if dev is true', async () => {
const { result, waitForNextUpdate } = renderHook(
() =>
useGetProgressDetailsQuery({
taskId: 'OxYqJgf6Tyl90uci1mzs',
dev: true,
}),
{ wrapper: Wrapper }
);

const initialResponse = result.current;
expect(initialResponse.data).toBeUndefined();
expect(initialResponse.isLoading).toBe(true);

await act(() => waitForNextUpdate());

const nextResponse = result.current;
const taskProgressResponse = nextResponse.data;
expect(nextResponse.status).toEqual('fulfilled');
expect(nextResponse.error).toBeUndefined();
expect(nextResponse.isSuccess).toEqual(true);
expect(taskProgressResponse).not.toBeUndefined();
expect(taskProgressResponse).toEqual(mockGetTaskProgress);
expect(taskProgressResponse?.data[0].userData).toEqual(
mockGetTaskProgress.data[0].userData
);
});

test('Returns userData undefined in the Task Progress if dev is false', async () => {
const { result, waitForNextUpdate } = renderHook(
() =>
useGetProgressDetailsQuery({
taskId: 'YttqJgf6Tyl35uci1mzs',
dev: false,
}),
{ wrapper: Wrapper }
);

const initialResponse = result.current;
expect(initialResponse.data).toBeUndefined();
expect(initialResponse.isLoading).toBe(true);

await act(() => waitForNextUpdate());

const nextResponse = result.current;
const taskProgressResponse = nextResponse.data;
expect(nextResponse.status).toEqual('fulfilled');
expect(nextResponse.error).toBeUndefined();
expect(nextResponse.isSuccess).toEqual(true);
expect(taskProgressResponse).not.toBeUndefined();
expect(taskProgressResponse?.data[1].userData).toBeUndefined();
});
});
5 changes: 3 additions & 2 deletions src/app/services/progressesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { progressDetails } from '@/types/standup.type';
type queryParamsType = {
userId?: string;
taskId?: string;
dev?: boolean;
};

export const progressesApi = api.injectEndpoints({
Expand All @@ -20,10 +21,10 @@ export const progressesApi = api.injectEndpoints({
}),
}),
getProgressDetails: builder.query<progressDetails, queryParamsType>({
query: ({ userId, taskId }: queryParamsType) => {
query: ({ userId, taskId, dev }: queryParamsType) => {
return {
url: '/progresses',
params: { userId, taskId },
params: { userId, taskId, dev },
};
},
providesTags: ['Progress_Details'],
Expand Down
2 changes: 2 additions & 0 deletions src/components/taskDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Props = {

const TaskDetails: FC<Props> = ({ taskID }) => {
const router = useRouter();
const isDev = router.query.dev === 'true';

const { isUserAuthorized } = useUserData();
const [isEditing, setIsEditing] = useState<boolean>(false);
Expand All @@ -76,6 +77,7 @@ const TaskDetails: FC<Props> = ({ taskID }) => {
const { data: progressData, refetch: refetchProgress } =
useGetProgressDetailsQuery({
taskId: taskID,
dev: isDev,
});

const isExtensionRequestPending = Boolean(
Expand Down

0 comments on commit 3f71ebb

Please sign in to comment.