-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from zhumeisongsong/feature/add-attributes-to-…
…user feat: ✨ add new attributes to user
- Loading branch information
Showing
15 changed files
with
222 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,12 @@ describe('GetUserUseCase', () => { | |
|
||
describe('execute', () => { | ||
it('should return user when found', async () => { | ||
const mockUser = { id: '1', name: 'John Doe' }; | ||
const mockUser = { | ||
id: '1', | ||
email: '[email protected]', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
}; | ||
(usersRepository.findById as jest.Mock).mockResolvedValue(mockUser); | ||
|
||
const result = await getUserUseCase.execute('1'); | ||
|
@@ -32,4 +37,4 @@ describe('GetUserUseCase', () => { | |
expect(usersRepository.findById).toHaveBeenCalledWith('1'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,12 @@ describe('UsersService', () => { | |
|
||
describe('findById', () => { | ||
it('should return a user if found', async () => { | ||
const user: User = { id: '1', name: 'John Doe' }; | ||
const user: User = { | ||
id: '1', | ||
email: '[email protected]', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
}; | ||
jest.spyOn(getUserUseCase, 'execute').mockResolvedValue(user); | ||
|
||
const result = await service.findById('1'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,46 @@ | ||
import { User } from './user.entity'; | ||
|
||
describe('User Entity', () => { | ||
it('should create a user with id and name', () => { | ||
const user = new User('1', 'John Doe'); | ||
expect(user.id).toBe('1'); | ||
expect(user.name).toBe('John Doe'); | ||
it('should create a user with all required fields', () => { | ||
const user = new User('123', '[email protected]', 'John', 'Doe'); | ||
|
||
expect(user.id).toBe('123'); | ||
expect(user.email).toBe('[email protected]'); | ||
expect(user.firstName).toBe('John'); | ||
expect(user.lastName).toBe('Doe'); | ||
}); | ||
|
||
it('should allow updating first name', () => { | ||
const user = new User('123', '[email protected]', 'John', 'Doe'); | ||
user.firstName = 'Jane'; | ||
expect(user.firstName).toBe('Jane'); | ||
}); | ||
|
||
it('should allow updating last name', () => { | ||
const user = new User('123', '[email protected]', 'John', 'Doe'); | ||
user.lastName = 'Smith'; | ||
expect(user.lastName).toBe('Smith'); | ||
}); | ||
|
||
it('should allow updating first name and last name', () => { | ||
const user = new User('123', '[email protected]', 'John', 'Doe'); | ||
user.firstName = 'Jane'; | ||
user.lastName = 'Smith'; | ||
expect(user.firstName).toBe('Jane'); | ||
expect(user.lastName).toBe('Smith'); | ||
}); | ||
|
||
it('should allow null values for first name and last name', () => { | ||
const user = new User('123', '[email protected]', null, null); | ||
expect(user.firstName).toBeNull(); | ||
expect(user.lastName).toBeNull(); | ||
}); | ||
|
||
it('should allow updating the name', () => { | ||
const user = new User('1', 'John Doe'); | ||
user.name = 'Jane Doe'; | ||
expect(user.name).toBe('Jane Doe'); | ||
it('should allow setting first name and last name to null', () => { | ||
const user = new User('123', '[email protected]', 'John', 'Doe'); | ||
user.firstName = null; | ||
user.lastName = null; | ||
expect(user.firstName).toBeNull(); | ||
expect(user.lastName).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
// Ensure that the values of all attributes entering the domain layer are valid. | ||
|
||
export class User { | ||
// Class attributes can be declared and initialized directly in the constructor arguments | ||
constructor( | ||
public readonly id: string, | ||
public name: string, | ||
public readonly email: string, | ||
public firstName: string | null, | ||
public lastName: string | null, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,13 @@ import { User } from './user.entity'; | |
|
||
class MockUsersRepository implements UsersRepository { | ||
private users: User[] = [ | ||
{ id: '1', name: 'John Doe' }, | ||
{ id: '2', name: 'Jane Doe' }, | ||
{ id: '1', email: '[email protected]', firstName: 'John', lastName: 'Doe' }, | ||
{ | ||
id: '2', | ||
email: '[email protected]', | ||
firstName: 'Jane', | ||
lastName: 'Smith', | ||
}, | ||
]; | ||
|
||
async findById(id: string): Promise<User | null> { | ||
|
@@ -21,7 +26,12 @@ describe('UsersRepository', () => { | |
|
||
test('findById should return a user by id', async () => { | ||
const user = await usersRepository.findById('1'); | ||
expect(user).toEqual({ id: '1', name: 'John Doe' }); | ||
expect(user).toEqual({ | ||
id: '1', | ||
email: '[email protected]', | ||
firstName: 'John', | ||
lastName: 'Doe' | ||
}); | ||
}); | ||
|
||
test('findById should return null if user not found', async () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,17 @@ describe('MongooseUsersRepository', () => { | |
}).compile(); | ||
|
||
repository = module.get<MongooseUsersRepository>(MongooseUsersRepository); | ||
userModel = module.get<Model<UserDocument>>(getModelToken(UserDocument.name)); | ||
userModel = module.get<Model<UserDocument>>( | ||
getModelToken(UserDocument.name), | ||
); | ||
}); | ||
|
||
it('should return a user when found', async () => { | ||
const mockUser = { | ||
id: '507f1f77bcf86cd799439011', | ||
name: 'Test User', | ||
email: '[email protected]', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
}; | ||
|
||
jest.spyOn(userModel, 'findById').mockReturnValue({ | ||
|
@@ -39,7 +43,9 @@ describe('MongooseUsersRepository', () => { | |
const user = await repository.findById(mockUser.id); | ||
expect(user).toBeInstanceOf(User); | ||
expect(user?.id).toBe(mockUser.id); | ||
expect(user?.name).toBe(mockUser.name); | ||
expect(user?.email).toBe(mockUser.email); | ||
expect(user?.firstName).toBe(mockUser.firstName); | ||
expect(user?.lastName).toBe(mockUser.lastName); | ||
}); | ||
|
||
it('should return null when user is not found', async () => { | ||
|
@@ -50,4 +56,4 @@ describe('MongooseUsersRepository', () => { | |
const user = await repository.findById('507f1f77bcf86cd799439011'); | ||
expect(user).toBeNull(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,37 @@ import { UserDto } from './user.dto'; | |
describe('UserDto', () => { | ||
it('should create a new UserDto instance', () => { | ||
const id = '123'; | ||
const name = 'Test User'; | ||
const userDto = new UserDto(id, name); | ||
const email = '[email protected]'; | ||
const firstName = 'John'; | ||
const lastName = 'Doe'; | ||
const userDto = new UserDto(id, email, firstName, lastName); | ||
|
||
expect(userDto).toBeDefined(); | ||
expect(userDto.id).toBe(id); | ||
expect(userDto.name).toBe(name); | ||
expect(userDto.email).toBe(email); | ||
expect(userDto.firstName).toBe(firstName); | ||
}); | ||
|
||
it('should handle empty firstName and lastName', () => { | ||
const id = '456'; | ||
const email = '[email protected]'; | ||
const userDto = new UserDto(id, email, null, null); | ||
|
||
expect(userDto).toBeDefined(); | ||
expect(userDto.id).toBe(id); | ||
expect(userDto.email).toBe(email); | ||
expect(userDto.firstName).toBeNull(); | ||
expect(userDto.lastName).toBeNull(); | ||
}); | ||
|
||
it('should handle special characters in names', () => { | ||
const id = '101'; | ||
const email = '[email protected]'; | ||
const firstName = 'Jean-Pierre'; | ||
const lastName = "O'Connor"; | ||
const userDto = new UserDto(id, email, firstName, lastName); | ||
|
||
expect(userDto.firstName).toBe(firstName); | ||
expect(userDto.lastName).toBe(lastName); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
libs/users/interface-adapters/src/lib/resolver/users.resolver.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.