-
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.
blog-template: server: add more unit tests
* also add decorator for request.res and update user create method
- Loading branch information
Showing
14 changed files
with
10,866 additions
and
8,284 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ dist | |
dist-ssr | ||
*.local | ||
|
||
coverage | ||
|
||
.vscode |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
|
||
export const ReqRes = createParamDecorator((_: unknown, ctx: ExecutionContext) => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
return request.res; | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { CryptoService } from '@server/crypto/CryptoService'; | ||
|
||
const comparisonResult = true; | ||
const hashedString = 'hashedString'; | ||
|
||
vi.mock('bcrypt', () => ({ | ||
compare: () => comparisonResult, | ||
hash: () => hashedString, | ||
})); | ||
|
||
describe('CryptoService', () => { | ||
let cryptoService: CryptoService; | ||
|
||
beforeEach(() => { | ||
cryptoService = new CryptoService(); | ||
}); | ||
|
||
describe('compare', () => { | ||
it('should return comparison result', async () => { | ||
expect(cryptoService.compare('a', 'b')).toEqual(comparisonResult); | ||
}); | ||
}); | ||
|
||
describe('hash', () => { | ||
it('should return hashed string', async () => { | ||
expect(cryptoService.hash('a', 10)).toEqual(hashedString); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ExecutionContext, HttpArgumentsHost } from '@nestjs/common/interfaces'; | ||
|
||
import { getParamDecoratorFactory } from '@test/utils/get-param-decorator-factory'; | ||
import { ReqRes } from '@server/decorators/ReqResDecorator'; | ||
|
||
const responseMock = 'response'; | ||
|
||
const switchToHttpMock = (): HttpArgumentsHost => ({ | ||
getRequest: vi.fn().mockReturnValue({ | ||
res: responseMock, | ||
}), | ||
getResponse: vi.fn(), | ||
getNext: vi.fn(), | ||
}); | ||
|
||
const ctxMock = <ExecutionContext>{ | ||
switchToHttp: switchToHttpMock, | ||
}; | ||
|
||
describe('ReqResDecorator', function () { | ||
it('should return response', function () { | ||
const factory = getParamDecoratorFactory(ReqRes); | ||
const result = factory(null, ctxMock); | ||
|
||
expect(result).toBe(responseMock); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ExecutionContext, HttpArgumentsHost } from '@nestjs/common/interfaces'; | ||
|
||
import { User } from '@server/decorators/UserDecorator'; | ||
import { UserDocument } from '@server/user/schemas/UserSchema'; | ||
import { getParamDecoratorFactory } from '@test/utils/get-param-decorator-factory'; | ||
|
||
const userMock = <UserDocument>{ | ||
id: '1', | ||
username: 'username', | ||
password: 'password', | ||
}; | ||
|
||
const switchToHttpMock = (): HttpArgumentsHost => ({ | ||
getRequest: vi.fn().mockReturnValue({ | ||
user: userMock, | ||
}), | ||
getResponse: vi.fn(), | ||
getNext: vi.fn(), | ||
}); | ||
|
||
const ctxMock = <ExecutionContext>{ | ||
switchToHttp: switchToHttpMock, | ||
}; | ||
|
||
describe('UserDecorator', function () { | ||
it('should return user', function () { | ||
const factory = getParamDecoratorFactory(User); | ||
const result = factory(null, ctxMock); | ||
|
||
expect(result).toBe(userMock); | ||
}); | ||
}); |
Oops, something went wrong.