Skip to content

Commit

Permalink
fix: change methods to test get user data hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-prud committed Oct 8, 2023
1 parent 4318767 commit 8c50884
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/services/UserService/__tests__/UserService.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import {act, mockUser} from '@tests';

import {UserService} from '../UserService';

describe('UserService', () => {
describe('Me method', () => {
it('should be able to return the user data', async () => {
const userResponse = await UserService().me();
const {me} = UserService();

let userResponse;

await act(async () => {
userResponse = await me();
});

expect(userResponse).toEqual(userResponse);
expect(userResponse).toEqual(mockUser);
});

it.todo('should throw an error when the request fails');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('UsePostList', () => {
});

const {postList, isLoading, error} = result.current;
expect(fetchDataMock).toHaveBeenCalledTimes(1);

expect(postList).toEqual(mockPostList);
expect(isLoading).toBe(false);
expect(error).toBe(false);
Expand Down
2 changes: 0 additions & 2 deletions src/useCases/Post/usePostList/usePostList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {PostService} from '@services';
import {PostProps} from '@types';

export function usePostList() {
// const {listAll} = PostService();

const [postList, setPostList] = useState<PostProps[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ describe('UseGetUserData', () => {
it('should seek user data and update correct states', async () => {
const {result} = renderHook(() => useGetUserData());

const fetchUserDataMock = jest.spyOn(result.current, 'getUserData');

await act(async () => {
await result.current.getUserData();
});

waitFor(() => {
expect(result.current.getUserData).toHaveBeenCalledTimes(1);
await waitFor(() => {
expect(fetchUserDataMock).toHaveBeenCalledTimes(1);
});

const {userData, isLoading, error} = result.current;

expect(userData).toEqual(mockUser); // Verifica a igualdade entre userData e mockUser
expect(userData).toEqual(mockUser);
expect(isLoading).toBe(false);
expect(error).toBe(false);
});
Expand Down
6 changes: 2 additions & 4 deletions src/useCases/User/useGetUserData/useGetUserData.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {useCallback, useEffect, useState} from 'react';

import UserService from '../../../services/UserService/UserService';
import {UserService} from '@services';

export function useGetUserData() {
// const {me} = UserService();

const [userData, setUserData] = useState({} as any);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(false);
Expand All @@ -13,7 +11,7 @@ export function useGetUserData() {
try {
setIsLoading(true);

const response = await UserService.me();
const response = await UserService().me();

setUserData(response);
} catch (erro) {
Expand Down

0 comments on commit 8c50884

Please sign in to comment.