Skip to content

Commit

Permalink
test: setRowHeight handler test
Browse files Browse the repository at this point in the history
  • Loading branch information
WanShufen committed Aug 30, 2023
1 parent 0d62700 commit 0cf2364
Showing 1 changed file with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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<ITableRepository>()
repo.findOneById.mockResolvedValue(Some(table))
let table: Table
let repo: MockProxy<ITableRepository>
let spy: SpyInstance
let command: SetRowHeightCommand
let handler: SetRowHeightCommandHandler

beforeEach(() => {
table = createTestTable()

repo = mock<ITableRepository>()

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)
Expand All @@ -26,14 +38,7 @@ describe('SetRowHeightCommandHandler', () => {
})

test('execute failed', async () => {
const table = createTestTable()
const repo = mock<ITableRepository>()
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)"',
Expand All @@ -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)
})
})

0 comments on commit 0cf2364

Please sign in to comment.