Skip to content

Commit

Permalink
Merge pull request #31 from zhumeisongsong/feature/user-entity
Browse files Browse the repository at this point in the history
refactor: ♻️ import User from @user/domain
  • Loading branch information
zhumeisongsong authored Nov 14, 2024
2 parents 63355c4 + 38b4446 commit 3b7e7e2
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 28 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);
}
}
}
7 changes: 7 additions & 0 deletions libs/user/domain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# domain

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test domain` to execute the unit tests via [Jest](https://jestjs.io).
3 changes: 3 additions & 0 deletions libs/user/domain/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const baseConfig = require('../../../eslint.config.js');

module.exports = [...baseConfig];
10 changes: 10 additions & 0 deletions libs/user/domain/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
displayName: 'domain',
preset: '../../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../../coverage/libs/user/domain',
};
9 changes: 9 additions & 0 deletions libs/user/domain/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "domain",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/user/domain/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project domain --web",
"targets": {}
}
1 change: 1 addition & 0 deletions libs/user/domain/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/user.entity';
15 changes: 15 additions & 0 deletions libs/user/domain/src/lib/user.entity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 allow updating the name', () => {
const user = new User('1', 'John Doe');
user.name = 'Jane Doe';
expect(user.name).toBe('Jane Doe');
});
});
6 changes: 6 additions & 0 deletions libs/user/domain/src/lib/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class User {
constructor(
public readonly id: string,
public name: string,
) {}
}
22 changes: 22 additions & 0 deletions libs/user/domain/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noPropertyAccessFromIndexSignature": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
11 changes: 11 additions & 0 deletions libs/user/domain/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
14 changes: 14 additions & 0 deletions libs/user/domain/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
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 3b7e7e2

Please sign in to comment.