From 0cf23646d824cf72e904866ff9306c24678d8a2a Mon Sep 17 00:00:00 2001 From: Sophie Date: Wed, 30 Aug 2023 17:53:07 +0800 Subject: [PATCH] test: setRowHeight handler test --- .../set-row-height.command.handler.test.ts | 57 +++++++++++++------ 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/packages/cqrs/src/commands/set-row-height/set-row-height.command.handler.test.ts b/packages/cqrs/src/commands/set-row-height/set-row-height.command.handler.test.ts index 8669641a8..ed84a8d02 100644 --- a/packages/cqrs/src/commands/set-row-height/set-row-height.command.handler.test.ts +++ b/packages/cqrs/src/commands/set-row-height/set-row-height.command.handler.test.ts @@ -1,20 +1,32 @@ -import { ITableRepository, createTestTable } from '@undb/core' -import { None, Some } from 'oxide.ts' -import { mock } from 'vitest-mock-extended' +import { ITableRepository, Table, createTestTable } from '@undb/core' +import { Err, None, Some } from 'oxide.ts' +import { MockProxy, mock } from 'vitest-mock-extended' import { SetRowHeightCommandHandler } from './set-row-height.command.handler.js' import { SetRowHeightCommand } from './set-row-height.command.js' +import { SpyInstance } from 'vitest' describe('SetRowHeightCommandHandler', () => { - test('execute success', async () => { - const table = createTestTable() - const repo = mock() - repo.findOneById.mockResolvedValue(Some(table)) + let table: Table + let repo: MockProxy + let spy: SpyInstance + let command: SetRowHeightCommand + let handler: SetRowHeightCommandHandler + + beforeEach(() => { + table = createTestTable() + + repo = mock() + + spy = vi.spyOn(table, 'setRowHeight') + + command = new SetRowHeightCommand({ tableId: table.id.value, rowHeight: 'short' }) - const spy = vi.spyOn(table, 'setRowHeight') + handler = new SetRowHeightCommandHandler(repo) + }) - const command = new SetRowHeightCommand({ tableId: table.id.value, rowHeight: 'short' }) + test('execute success', async () => { + repo.findOneById.mockResolvedValue(Some(table)) - const handler = new SetRowHeightCommandHandler(repo) await handler.execute(command) expect(repo.findOneById).toHaveBeenCalledWith(table.id.value) @@ -26,14 +38,7 @@ describe('SetRowHeightCommandHandler', () => { }) test('execute failed', async () => { - const table = createTestTable() - const repo = mock() repo.findOneById.mockResolvedValue(None) - const spy = vi.spyOn(table, 'setRowHeight') - - const command = new SetRowHeightCommand({ tableId: table.id.value, rowHeight: 'short' }) - - const handler = new SetRowHeightCommandHandler(repo) await expect(handler.execute(command)).rejects.toThrowErrorMatchingInlineSnapshot( '"Failed to unwrap Option (found None)"', @@ -42,4 +47,22 @@ describe('SetRowHeightCommandHandler', () => { expect(spy).not.toHaveBeenCalled() expect(repo.updateOneById).not.toHaveBeenCalled() }) + + test('findOneById throw error', async () => { + repo.findOneById.mockRejectedValue(new Error('findOneById failed')) + + await expect(handler.execute(command)).rejects.toThrowErrorMatchingInlineSnapshot('"findOneById failed"') + expect(spy).not.toHaveBeenCalled() + expect(repo.updateOneById).not.toHaveBeenCalled() + }) + + test('updateOneById throw error', async () => { + repo.findOneById.mockResolvedValue(Some(table)) + repo.updateOneById.mockRejectedValue(new Error('updateOneById failed')) + + await expect(handler.execute(command)).rejects.toThrowErrorMatchingInlineSnapshot('"updateOneById failed"') + + expect(repo.findOneById).toHaveBeenCalledWith(table.id.value) + expect(spy).toHaveBeenCalledWith(command) + }) })