Skip to content

Commit

Permalink
🧪 chore: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rezk2ll committed Jul 9, 2024
1 parent e6f08ac commit 54d0c54
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/tom-server/src/vault-api/controllers/vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe('Vault controllers', () => {
const dbManager: Partial<TwakeDB> = {
get: jest.fn(),
insert: jest.fn(),
deleteWhere: jest.fn()
deleteWhere: jest.fn(),
update: jest.fn()
}
let mockRequest: ITestRequest
let mockResponse: Partial<Response>
Expand Down Expand Up @@ -69,6 +70,7 @@ describe('Vault controllers', () => {
// Testing saveRecoveryWords
it('should return response with status code 201 on save success', async () => {
jest.spyOn(dbManager, 'insert').mockResolvedValue([{ words }])
jest.spyOn(dbManager, 'get').mockResolvedValue([])
const handler: expressAppHandler = saveRecoveryWords(dbManager as TwakeDB)
handler(mockRequest as Request, mockResponse as Response, nextFunction)
await new Promise(process.nextTick)
Expand All @@ -78,12 +80,26 @@ describe('Vault controllers', () => {
it('should call next function to throw error on saving failed', async () => {
const errorMsg = 'Insert failed'
jest.spyOn(dbManager, 'insert').mockRejectedValue(new Error(errorMsg))
jest.spyOn(dbManager, 'get').mockResolvedValue([])
const handler: expressAppHandler = saveRecoveryWords(dbManager as TwakeDB)
handler(mockRequest as Request, mockResponse as Response, nextFunction)
await new Promise(process.nextTick)
expect(nextFunction).toHaveBeenCalledWith(new Error(errorMsg))
})

it('should update the recoverywords when it already exists', async () => {
jest
.spyOn(dbManager, 'get')
.mockResolvedValue([{ words: 'Another sentence for the same user' }])
jest.spyOn(dbManager, 'update').mockResolvedValue([{ words }])
const handler: expressAppHandler = saveRecoveryWords(dbManager as TwakeDB)
handler(mockRequest as Request, mockResponse as Response, nextFunction)
await new Promise(process.nextTick)
expect(mockResponse.statusCode).toEqual(200)
expect(dbManager.update).toHaveBeenCalled()
expect(dbManager.insert).not.toHaveBeenCalled()
})

// Testing getRecoveryWords

it('should return response with status code 200 on get success', async () => {
Expand Down

0 comments on commit 54d0c54

Please sign in to comment.