Skip to content

Commit

Permalink
refactor: ♻️ import User from @user/domain
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Nov 14, 2024
1 parent e2749bf commit 38b4446
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
22 changes: 11 additions & 11 deletions apps/users-application/src/users/users.resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UsersResolver } from './users.resolver';
import { UsersService } from './users.service';
import { User } from './models/user.model';
import { User } from '@user/domain';

describe('UsersResolver', () => {
let resolver: UsersResolver;
Expand Down Expand Up @@ -30,35 +30,35 @@ describe('UsersResolver', () => {

describe('getUser', () => {
it('should return a user by id', () => {
const user: User = { id: 1, name: 'John Doe' };
const user: User = { id: '1', name: 'John Doe' };
jest.spyOn(service, 'findById').mockReturnValue(user);

expect(resolver.getUser(1)).toEqual(user);
expect(service.findById).toHaveBeenCalledWith(1);
expect(resolver.getUser('1')).toEqual(user);
expect(service.findById).toHaveBeenCalledWith('1');
});

it('should return undefined if user not found', () => {
jest.spyOn(service, 'findById').mockReturnValue(undefined);

expect(resolver.getUser(1)).toBeUndefined();
expect(service.findById).toHaveBeenCalledWith(1);
expect(resolver.getUser('2')).toBeUndefined();
expect(service.findById).toHaveBeenCalledWith('2');
});
});

describe('resolveReference', () => {
it('should return a user by reference id', () => {
const user: User = { id: 1, name: 'John Doe' };
const user: User = { id: '1', name: 'John Doe' };
jest.spyOn(service, 'findById').mockReturnValue(user);

expect(resolver.resolveReference({ __typename: 'User', id: 1 })).toEqual(user);
expect(service.findById).toHaveBeenCalledWith(1);
expect(resolver.resolveReference({ __typename: 'User', id: '1' })).toEqual(user);
expect(service.findById).toHaveBeenCalledWith('1');
});

it('should return undefined if user not found by reference id', () => {
jest.spyOn(service, 'findById').mockReturnValue(undefined);

expect(resolver.resolveReference({ __typename: 'User', id: 1 })).toBeUndefined();
expect(service.findById).toHaveBeenCalledWith(1);
expect(resolver.resolveReference({ __typename: 'User', id: '2' })).toBeUndefined();
expect(service.findById).toHaveBeenCalledWith('2');
});
});
});
6 changes: 3 additions & 3 deletions apps/users-application/src/users/users.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Args, ID, Query, Resolver, ResolveReference } from '@nestjs/graphql';
import { User } from '@user/domain';

import { User } from './models/user.model';
import { UsersService } from './users.service';

@Resolver(() => User)
export class UsersResolver {
constructor(private usersService: UsersService) {}

@Query(() => User, { nullable: true })
getUser(@Args({ name: 'id', type: () => ID }) id: number): User | undefined {
getUser(@Args({ name: 'id', type: () => ID }) id: string): User | undefined {
return this.usersService.findById(id);
}

@ResolveReference()
resolveReference(reference: {
__typename: string;
id: number;
id: string;
}): User | undefined {
return this.usersService.findById(reference.id);
}
Expand Down
9 changes: 5 additions & 4 deletions apps/users-application/src/users/users.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { User } from '@user/domain';

import { UsersService } from './users.service';
import { User } from './models/user.model';

describe('UsersService', () => {
let service: UsersService;
Expand All @@ -18,12 +19,12 @@ describe('UsersService', () => {
});

it('should return a user by id', () => {
const user: User = service.findById(1);
expect(user).toEqual({ id: 1, name: 'John Doe' });
const user: User = service.findById('1');
expect(user).toEqual({ id: '1', name: 'John Doe' });
});

it('should return undefined if user is not found', () => {
const user: User = service.findById(3);
const user: User = service.findById('3');
expect(user).toBeUndefined();
});
});
12 changes: 6 additions & 6 deletions apps/users-application/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Injectable } from '@nestjs/common';
import { User } from './models/user.model';
import { User } from '@user/domain';

@Injectable()
export class UsersService {
private users: User[] = [
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Richard Roe' },
{ id: '1', name: 'John Doe' },
{ id: '2', name: 'Richard Roe' },
];

findById(id: number): User | undefined {
return this.users.find((user) => user.id === Number(id));
findById(id: string): User | undefined {
return this.users.find((user) => user.id === id);
}
}
}
2 changes: 1 addition & 1 deletion libs/user/domain/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/domain';
export * from './lib/user.entity';
7 changes: 3 additions & 4 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@libs/config": [
"libs/config/src/index.ts"
],
"@libs/config": ["libs/config/src/index.ts"],
"@prompt/user-prompt-log-entity": [
"libs/prompt/domain/user-prompt-log-entity/src/index.ts"
],
"@prompt/wellbeing-prompt-entity": [
"libs/prompt/domain/wellbeing-prompt-entity/src/index.ts"
]
],
"@user/domain": ["libs/user/domain/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down

0 comments on commit 38b4446

Please sign in to comment.