sinProject-Inc/talk

View on GitHub
src/lib/view/message.test.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { expect, test } from 'vitest'
import { Message } from './message'

type Spec = {
    text: string
    expected?: string
    expected_error?: string
}

const specs: Spec[] = [
    { text: '', expected_error: 'text is empty' },
    { text: ' ', expected_error: 'text is empty' },
    { text: 'text', expected: 'text' },
    { text: ' text ', expected: 'text' },
    { text: ' text text ', expected: 'text text' },
    { text: '    text    ', expected: 'text' },
    { text: '    text    text    ', expected: 'text    text' },
]

test.each(specs)('new Message($text) -> ($expected) : (expected_error)', (spec: Spec) => {
    const { text, expected, expected_error } = spec

    if (expected_error) {
        expect(() => new Message(text)).toThrow(expected_error)

        return
    }

    expect(new Message(text).text).toEqual(expected)
})