Skip to content

Commit

Permalink
feat: adding user service (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan2slime authored Jul 13, 2024
1 parent 1020710 commit 12d910d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/app/user/tests/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { FilterQuery, Model } from 'mongoose';

import { User } from '~/schemas/user.schema';
import { UserService } from '../user.service';
import { SignUpDto } from '~/app/auth/auth.dto';
import { Entity } from '~/types';

describe('UserService', () => {
let userModel: Model<User>;
let userService: UserService;

afterEach(() => jest.clearAllMocks());

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [],
providers: [
{
provide: getModelToken(User.name),
useValue: Model,
},
UserService,
],
}).compile();

userModel = app.get<Model<User>>(getModelToken(User.name));
userService = app.get<UserService>(UserService);
});

describe('create', () => {
const payload: SignUpDto = {
email: expect.anything(),
name: expect.anything(),
password: expect.anything(),
};
const user = {} as Entity<User>[];

it('should return user created', async () => {
jest.spyOn(userModel, 'create').mockResolvedValue(user);

const res = await userService.create(payload);

expect(res).toBe(user);
expect(userModel.create).toHaveBeenCalledWith(payload);
expect(userModel.create).toHaveBeenCalledTimes(1);
});
});

describe('getByEmail', () => {
const email = expect.anything();
const user = {} as Entity<User>[];

it('must return user by email', async () => {
jest.spyOn(userModel, 'findOne').mockResolvedValue(user);

const res = await userService.getByEmail(email);

expect(res).toBe(user);
expect(userModel.findOne).toHaveBeenCalledWith({ email });
expect(userModel.findOne).toHaveBeenCalledTimes(1);
});
});

describe('getPassword', () => {
const query: FilterQuery<User> = { email: expect.anything() };
const user = {} as Entity<User>[];

it('must return the user is password through any filter', async () => {
jest.spyOn(userModel, 'findOne').mockResolvedValue(user);

const res = await userService.getPassword(query);

expect(res).toBe(user);
expect(userModel.findOne).toHaveBeenCalledWith(query, { password: true });
expect(userModel.findOne).toHaveBeenCalledTimes(1);
});
});
});
27 changes: 27 additions & 0 deletions src/app/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { FilterQuery, Model } from 'mongoose';

import { User } from '~/schemas/user.schema';
import { SignUpDto } from '~/app/auth/auth.dto';

@Injectable()
export class UserService {
constructor(
@InjectModel(User.name) private readonly userModel: Model<User>,
) {}

async create(data: SignUpDto) {
return await this.userModel.create(data);
}

async getByEmail(email: string) {
return await this.userModel.findOne({ email });
}

async getPassword(query: FilterQuery<User>) {
return await this.userModel.findOne(query, {
password: true,
});
}
}

0 comments on commit 12d910d

Please sign in to comment.