Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
Adapted test
Browse files Browse the repository at this point in the history
  • Loading branch information
maxschwinghammer committed May 29, 2024
1 parent fe903a6 commit ba94050
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main/web/src/services/tests/ProfilePictureService.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import ProfilePictureService from '../ProfilePictureService';
import { getJWT, getUserId } from '../AuthService';
import { getJWT, getUserId, isUserLoggedIn } from '../AuthService';
import config from '../../config/config';

jest.mock('../AuthService', () => ({
getJWT: jest.fn(),
getUserId: jest.fn(),
isUserLoggedIn: jest.fn(),
}));

global.console.log = jest.fn();
@@ -18,18 +19,20 @@ describe('fetchUserImage', (): void => {
jest.clearAllMocks();
});

it('should fetch user image successfully', async (): Promise<void> => {
it('should fetch user image successfully when user is logged in', async (): Promise<void> => {
const mockData = { image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz3' };
const mockResponse = {
ok: true,
json: jest.fn().mockResolvedValue(mockData),
};
(getJWT as jest.Mock).mockReturnValue('mock-jwt');
(getUserId as jest.Mock).mockReturnValue(1);
(isUserLoggedIn as jest.Mock).mockReturnValue(true);
(global.fetch as jest.Mock).mockResolvedValue(mockResponse);

await ProfilePictureService.fetchUserImage(setUserImage);

expect(isUserLoggedIn).toHaveBeenCalled();
expect(getJWT).toHaveBeenCalled();
expect(getUserId).toHaveBeenCalled();
expect(global.fetch).toHaveBeenCalledWith(`${config.apiUrl}picture/find/1`, {
@@ -41,17 +44,30 @@ describe('fetchUserImage', (): void => {
expect(setUserImage).toHaveBeenCalledWith(mockData);
});

it('should not fetch user image if user is not logged in', async (): Promise<void> => {
(isUserLoggedIn as jest.Mock).mockReturnValue(false);

await ProfilePictureService.fetchUserImage(setUserImage);

expect(isUserLoggedIn).toHaveBeenCalled();
expect(global.fetch).not.toHaveBeenCalled();
expect(setUserImage).not.toHaveBeenCalled();
expect(console.log).toHaveBeenCalledWith("User is not logged in: cannot fetch user image.");
});

it('should handle fetch failure', async (): Promise<void> => {
const mockResponse = {
ok: false,
statusText: 'Not Found',
};
(getJWT as jest.Mock).mockReturnValue('mock-jwt');
(getUserId as jest.Mock).mockReturnValue(1);
(isUserLoggedIn as jest.Mock).mockReturnValue(true);
(global.fetch as jest.Mock).mockResolvedValue(mockResponse);

await ProfilePictureService.fetchUserImage(setUserImage);

expect(isUserLoggedIn).toHaveBeenCalled();
expect(global.fetch).toHaveBeenCalledWith(`${config.apiUrl}picture/find/1`, {
headers: {
...config.headers,
@@ -66,10 +82,12 @@ describe('fetchUserImage', (): void => {
const mockError = new Error('Network Error');
(getJWT as jest.Mock).mockReturnValue('mock-jwt');
(getUserId as jest.Mock).mockReturnValue(1);
(isUserLoggedIn as jest.Mock).mockReturnValue(true);
(global.fetch as jest.Mock).mockRejectedValue(mockError);

await ProfilePictureService.fetchUserImage(setUserImage);

expect(isUserLoggedIn).toHaveBeenCalled();
expect(global.fetch).toHaveBeenCalledWith(`${config.apiUrl}picture/find/1`, {
headers: {
...config.headers,

0 comments on commit ba94050

Please sign in to comment.