aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/utils/result.test.ts
blob: ef33e74e9d13d5422d5fdc807481632e7e331a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { describe, expect, test } from 'vitest'
import {
  err,
  isErr,
  isOk,
  mapErr,
  mapResult,
  ok,
  unwrap,
  unwrapOr,
  type Result
} from 'utils/result'

describe('Result constructors and predicates', () => {
  test('ok wraps a value', () => {
    const r = ok(42)
    expect(r).toEqual({ ok: true, value: 42 })
    expect(isOk(r)).toBe(true)
    expect(isErr(r)).toBe(false)
  })

  test('err wraps an error', () => {
    const e = new Error('boom')
    const r = err(e)
    expect(r).toEqual({ ok: false, error: e })
    expect(isErr(r)).toBe(true)
    expect(isOk(r)).toBe(false)
  })
})

describe('mapResult / mapErr', () => {
  test('mapResult transforms the value of an ok', () => {
    const r: Result<number> = ok(3)
    expect(mapResult(r, (n) => n * 2)).toEqual(ok(6))
  })

  test('mapResult leaves an error untouched', () => {
    const e = new Error('x')
    const r: Result<number> = err(e)
    expect(mapResult(r, (n: number) => n * 2)).toEqual(err(e))
  })

  test('mapErr transforms the error of a failure', () => {
    const r: Result<number, string> = err('oops')
    expect(mapErr(r, (s: string) => s.length)).toEqual(err(4))
  })

  test('mapErr leaves an ok untouched', () => {
    const r: Result<number, string> = ok(5)
    expect(mapErr(r, (s: string) => s.length)).toEqual(ok(5))
  })
})

describe('unwrap / unwrapOr', () => {
  test('unwrap returns the value when ok', () => {
    expect(unwrap(ok('hello'))).toBe('hello')
  })

  test('unwrap throws when error', () => {
    expect(() => unwrap(err(new Error('bad')))).toThrow(/bad/)
  })

  test('unwrapOr returns value when ok', () => {
    expect(unwrapOr(ok(1), 0)).toBe(1)
  })

  test('unwrapOr returns fallback when error', () => {
    expect(unwrapOr(err<Error>(new Error('e')), 0)).toBe(0)
  })
})

describe('type-narrowing', () => {
  test('isOk narrows to Ok<T>', () => {
    const r: Result<number, string> = ok(10)
    if (isOk(r)) {
      // Type-level check — if isOk doesn't narrow, this line fails to compile.
      const n: number = r.value
      expect(n).toBe(10)
    } else {
      expect.fail('isOk should narrow to ok branch')
    }
  })
})