Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: unit tests for users module #133

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
lint: run lint code
mathd1as committed May 17, 2024
commit f6e5bb104fc3586b0f7fa8560e652c0c8b303e10
75 changes: 36 additions & 39 deletions src/users/users.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -14,14 +14,14 @@ describe('UsersController', () => {
provide: UsersService,
useValue: {
store: jest.fn(),
update: jest.fn()
}
}
]
update: jest.fn(),
},
},
],
}).compile();

controller = module.get<UsersController>(UsersController);
userService = module.get<UsersService>(UsersService)
userService = module.get<UsersService>(UsersService);
});

it('should be defined', () => {
@@ -31,40 +31,39 @@ describe('UsersController', () => {

describe('store function', () => {
const body = {
name: "Matheus",
lastName: "Silva",
phone: "44999998311",
}
name: 'Matheus',
lastName: 'Silva',
phone: '44999998311',
};

it('checks if the function has been called 1 time', async () => {
await controller.store(body)
expect(userService.store).toHaveBeenCalledTimes(1)
await controller.store(body);
expect(userService.store).toHaveBeenCalledTimes(1);
});

it('checks the return of the function', async () => {
const expectedResult = {
message: "Successfully created user",
message: 'Successfully created user',
respData: undefined,
statusCode: 201,
}
const result = await controller.store(body)
expect(expectedResult).toEqual(result)

};
const result = await controller.store(body);
expect(expectedResult).toEqual(result);
});

it('checks if the function fails', async () => {
jest.spyOn(userService, 'store').mockRejectedValueOnce(new Error());
expect(controller.store(body)).rejects.toThrow()
})
expect(controller.store(body)).rejects.toThrow();
});
});

describe('update function', () => {
const body = {
name: "Matheus",
lastName: "Silva",
phone: "44999998311",
}
const id = 'test_user_id'
name: 'Matheus',
lastName: 'Silva',
phone: '44999998311',
};
const id = 'test_user_id';

it('checks if the function has been called 1 time', async () => {
await controller.update(body, id);
@@ -73,30 +72,29 @@ describe('UsersController', () => {

it('checks the return of the function', async () => {
const expectedResult = {
message: "Successfully updated user",
message: 'Successfully updated user',
respData: undefined,
statusCode: 201,
}
};
const result = await controller.update(body, id);
expect(expectedResult).toEqual(result)

expect(expectedResult).toEqual(result);
});

it('checks if the function fails', async () => {
jest.spyOn(userService, 'update').mockRejectedValueOnce(new Error());
expect(controller.update(body, id)).rejects.toThrow()
expect(controller.update(body, id)).rejects.toThrow();
});
});

describe('selfUpdate function', () => {
const body = {
name: "Matheus",
lastName: "Silva",
phone: "44999998311",
}
name: 'Matheus',
lastName: 'Silva',
phone: '44999998311',
};
const req = {
user: 'test_user_id'
}
user: 'test_user_id',
};

it('checks if the function has been called 1 time', async () => {
await controller.selfUpdate(body, req);
@@ -105,18 +103,17 @@ describe('UsersController', () => {

it('checks the return of the function', async () => {
const expectedResult = {
message: "Successfully updated",
message: 'Successfully updated',
respData: undefined,
statusCode: 201,
}
};
const result = await controller.selfUpdate(body, req);
expect(expectedResult).toEqual(result)

expect(expectedResult).toEqual(result);
});

it('checks if the function fails', async () => {
jest.spyOn(userService, 'update').mockRejectedValueOnce(new Error());
expect(controller.selfUpdate(body, req)).rejects.toThrow()
expect(controller.selfUpdate(body, req)).rejects.toThrow();
});
});
});
52 changes: 25 additions & 27 deletions src/users/users.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UsersService } from './users.service';
import { PrismaService } from '../../src/prisma/prisma.service';
import { UpdateUserSchema } from './types';

describe('UsersService', () => {
let userService: UsersService;

const mockPrismaService = {
user: {
create: jest.fn(),
update: jest.fn()
}
}
update: jest.fn(),
},
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
UsersService,
PrismaService,
],
providers: [UsersService, PrismaService],
})
.overrideProvider(PrismaService)
.useValue(mockPrismaService)
.compile();
.overrideProvider(PrismaService)
.useValue(mockPrismaService)
.compile();

userService = module.get<UsersService>(UsersService);
});
@@ -33,37 +29,39 @@ describe('UsersService', () => {

describe('store function', () => {
const userPayload = {
name: 'matheus', lastName: 'silva', phone:'44999998311'
}
name: 'matheus',
lastName: 'silva',
phone: '44999998311',
};

it('shold call the store function 1 time', async () => {
userService.store(userPayload)
expect(mockPrismaService.user.create).toHaveBeenCalledTimes(1)
userService.store(userPayload);
expect(mockPrismaService.user.create).toHaveBeenCalledTimes(1);
});

it('shold call the store function with the parameters', async () => {
userService.store(userPayload)
userService.store(userPayload);
const data = {
...userPayload,
password: userPayload.phone,
login: userPayload.phone,
createdAt: new Date().toISOString(),
}
expect(mockPrismaService.user.create).toHaveBeenCalledWith({ data })
};
expect(mockPrismaService.user.create).toHaveBeenCalledWith({ data });
});
});

describe('update function', () => {
const id = 'user_id_test';
const body = {
name: 'Matheus',
lastName: 'Silva',
phone:'44999998311',
};
const id = 'user_id_test';
const body = {
name: 'Matheus',
lastName: 'Silva',
phone: '44999998311',
};

it('shuld call the store function 1 time', async () => {
userService.update(id, body)
expect(mockPrismaService.user.update).toHaveBeenCalledTimes(1)
userService.update(id, body);
expect(mockPrismaService.user.update).toHaveBeenCalledTimes(1);
});
})
});
});