Skip to content

Commit

Permalink
Merge pull request #17 from fga-eps-mds/feat#71/gerenciar-roles
Browse files Browse the repository at this point in the history
Feat#71/gerenciar roles
  • Loading branch information
paulohgontijoo authored Aug 2, 2024
2 parents 78dfb74 + c2e762a commit c002714
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 42 deletions.
8 changes: 2 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts

package-lock.json
/package-lock.json

reports/*

reports

report
/reports/*.xml
14 changes: 5 additions & 9 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import type { Config } from 'jest';
import nextJest from 'next/jest';

import nextJest from 'next/jest.js'

const createJestConfig = nextJest({
dir: './src',
})
});

const config: Config = {
coverageProvider: 'v8',
testEnvironment: 'jsdom',
preset: 'ts-jest',
moduleNameMapper: {
// ...
'^@/components/(.*)$': '<rootDir>/src/app/components/$1',
'^@/(.*)$': '<rootDir>/src/$1',
},
reporters: [
Expand All @@ -25,7 +22,6 @@ const config: Config = {
},
],
],
};

}

export default createJestConfig(config)
export default createJestConfig(config);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/tough-cookie": "^4.0.5",
"autoprefixer": "^10.4.19",
"eslint": "^8",
"eslint-config-next": "14.2.5",
Expand Down
24 changes: 0 additions & 24 deletions src/app/services/apiService.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/enum/userRole.enum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export enum UserRole {
ADMIN = 'admin',
PROFESSOR = 'professor',
USER = 'user',
ALUNO = 'aluno',
}
2 changes: 0 additions & 2 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import api from './api.service';
import { CalculusRequest } from '@/lib/interfaces/request.interface';

export const createUser = async (data: any): Promise<CalculusRequest> => {
console.log(`Data: ${data}`);

try {
const response = await api.post('users', data);
return {
Expand Down
100 changes: 100 additions & 0 deletions test/app/services/user.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import api from '@/services/api.service';
import { createUser, loginWithEmailAndPassword, getUsers, updateUserRole } from '@/services/user.service';
import { UserRole } from '@/lib/enum/userRole.enum';

jest.mock('@/services/api.service');
const mockedApi = api as jest.Mocked<typeof api>;

describe('User Service', () => {
describe('updateUserRole', () => {
it('should update the user role and return the updated user', async () => {
const userId = '1';
const newRole: UserRole = UserRole.PROFESSOR;
const mockResponse = { data: { id: '1', role: newRole } };
mockedApi.patch.mockResolvedValueOnce(mockResponse);

const result = await updateUserRole(userId, newRole);

expect(mockedApi.patch).toHaveBeenCalledWith(`/users/${userId}/role`, { role: newRole });
expect(result).toEqual(mockResponse.data);
});

it('should throw an error when the API call fails', async () => {
const userId = '1';
const newRole: UserRole = UserRole.PROFESSOR;
const mockError = new Error('Failed to update user role');
mockedApi.patch.mockRejectedValueOnce(mockError);

await expect(updateUserRole(userId, newRole)).rejects.toThrow('Failed to update user role');
});
});

describe('createUser', () => {
it('should create a user and return the user data', async () => {
const userData = { name: 'John Doe', email: 'john@example.com', username: 'johndoe', password: 'password123', role: UserRole.ALUNO };
const mockResponse = { data: userData };
mockedApi.post.mockResolvedValueOnce(mockResponse);

const result = await createUser(userData);

expect(mockedApi.post).toHaveBeenCalledWith('users', userData);
expect(result).toEqual({ data: mockResponse.data });
});

it('should handle errors and return an error message', async () => {
const userData = { name: 'John Doe', email: 'john@example.com', username: 'johndoe', password: 'password123', role: UserRole.ALUNO };
const mockError = { response: { data: { message: 'Error creating user' } } };
mockedApi.post.mockRejectedValueOnce(mockError);

const result = await createUser(userData);

expect(mockedApi.post).toHaveBeenCalledWith('users', userData);
expect(result).toEqual({ error: 'Error creating user' });
});
});

describe('loginWithEmailAndPassword', () => {
it('should log in with email and password and return the response', async () => {
const email = 'john@example.com';
const password = 'password123';
const mockResponse = { data: { token: 'abc123' } };
mockedApi.post.mockResolvedValueOnce(mockResponse);

const result = await loginWithEmailAndPassword(email, password);

expect(mockedApi.post).toHaveBeenCalledWith('auth/login', { email, password });
expect(result).toEqual(mockResponse);
});

it('should handle errors and return null', async () => {
const email = 'john@example.com';
const password = 'password123';
const mockError = new Error('Login failed');
mockedApi.post.mockRejectedValueOnce(mockError);

const result = await loginWithEmailAndPassword(email, password);

expect(mockedApi.post).toHaveBeenCalledWith('auth/login', { email, password });
expect(result).toBeNull();
});
});

describe('getUsers', () => {
it('should fetch users and return the data', async () => {
const mockResponse = { data: [{ id: '1', name: 'John Doe' }] };
mockedApi.get.mockResolvedValueOnce(mockResponse);

const result = await getUsers();

expect(mockedApi.get).toHaveBeenCalledWith('/users');
expect(result).toEqual(mockResponse.data);
});

it('should handle errors and throw an error', async () => {
const mockError = new Error('Failed to fetch users');
mockedApi.get.mockRejectedValueOnce(mockError);

await expect(getUsers()).rejects.toThrow('Failed to fetch users');
});
});
});

0 comments on commit c002714

Please sign in to comment.