-
Notifications
You must be signed in to change notification settings - Fork 0
/
nor.test.js
88 lines (76 loc) · 2.08 KB
/
nor.test.js
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
84
85
86
87
88
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import { nor } from './index.js'
describe('nor', () => {
describe('synchronous', () => {
it('returns data when ok', () => {
assert.equal(
nor(() => 1),
1
)
})
it('returns null when not ok', () => {
assert.equal(
nor(() => {
throw 'bad'
}),
null
)
})
it('handles parametered functions via a higher-order, parameterless function', () => {
const a = 1
const b = 2
const mightThrow = (a, b) => {
if (b === 2) {
throw 'bad'
} else return b
}
const actual = nor(() => mightThrow(a, b))
assert.equal(actual, null)
})
})
describe('asynchronous: function-promise', () => {
it('returns data when ok', async () => {
const actual1 = await nor(async () => 1)
assert.equal(actual1, 1)
const actual2 = await nor(() => Promise.resolve(1))
assert.equal(actual2, 1)
const actual3 = await nor(() => Promise.resolve(1).then((r) => r + 1))
assert.equal(actual3, 2)
})
it('returns null when not ok', async () => {
const actual1 = await nor(async () => {
throw 'bad'
})
assert.equal(actual1, null)
const actual2 = await nor(async () =>
Promise.resolve(1).then((r) => {
throw 'bad'
})
)
assert.equal(actual2, null)
const actual3 = await nor(() =>
Promise.resolve(1).then((r) => {
throw 'bad'
})
)
assert.equal(actual3, null)
})
})
describe('asynchronous: promise', () => {
it('returns data when ok', async () => {
const actual1 = await nor(Promise.resolve(1))
assert.equal(actual1, 1)
const actual2 = await nor(Promise.resolve(1).then((r) => r + 1))
assert.equal(actual2, 2)
})
it('return null when not ok', async () => {
const actual1 = await nor(
Promise.resolve(1).then((r) => {
throw 'bad'
})
)
assert.equal(actual1, null)
})
})
})