Repository navigation
Expand file tree
/
Copy pathutil.test.ts
More file actions
105 lines (96 loc) · 3.66 KB
/
Copy pathutil.test.ts
File metadata and controls
105 lines (96 loc) · 3.66 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as assert from 'assert'
import { Subject } from 'rxjs'
import { compact, flatten, isEqual, isPromise, isSubscribable, tryCatchPromise } from './util'
describe('flatten', () => {
it('flattens arrays 1 level deep', () => {
assert.deepStrictEqual(flatten([]), [])
assert.deepStrictEqual(flatten([1]), [1])
assert.deepStrictEqual(flatten([1, 'a']), [1, 'a'])
assert.deepStrictEqual(flatten([[1], 2, [3]]), [1, 2, 3])
assert.deepStrictEqual(flatten([[1, 2], [[3], 4], [5, [6]]]), [1, 2, [3], 4, 5, [6]])
})
})
describe('compact', () => {
it('removes falsey values from arrays', () => {
assert.deepStrictEqual(compact([]), [])
assert.deepStrictEqual(compact([0, 1, '', true, false, NaN]), [1, true])
})
})
describe('isEqual', () => {
const TESTS: { a: any; b: any; want: boolean }[] = [
{ a: 1, b: 1, want: true },
{ a: 1, b: 2, want: false },
{ a: {}, b: undefined, want: false },
{ a: undefined, b: {}, want: false },
{ a: [1], b: [1], want: true },
{ a: [1], b: [2], want: false },
{ a: [1], b: [1, 1], want: false },
{ a: { a: 1 }, b: { a: 1 }, want: true },
{ a: { a: 1 }, b: { b: 1 }, want: false },
{ a: { a: 1 }, b: { a: 2 }, want: false },
{ a: { a: 1 }, b: { a: 1, b: 2 }, want: false },
{ a: { a: [1] }, b: { a: [1] }, want: true },
{ a: { a: [1] }, b: { a: [2] }, want: false },
]
for (const { a, b, want } of TESTS) {
it(`reports ${JSON.stringify(a)} ${want ? '==' : '!='} ${JSON.stringify(b)}`, () => {
assert.strictEqual(isEqual(a, b), want)
})
}
})
describe('tryCatchPromise', () => {
it('returns a resolved promise with the synchronous result', async () =>
assert.strictEqual(await tryCatchPromise(() => 1), 1))
it('returns a resolved promise with the asynchronous result', async () =>
assert.strictEqual(await tryCatchPromise(() => Promise.resolve(1)), 1))
const ERROR = new Error('x')
it('returns a rejected promise with the synchronous exception', () => {
const p = tryCatchPromise(() => {
throw ERROR
})
let rejected: any
return p.then(undefined, v => (rejected = v)).then(() => {
assert.strictEqual(rejected, ERROR)
})
})
it('returns a rejected promise with the asynchronous error', () => {
const p = tryCatchPromise(
() => Promise.reject(ERROR) // tslint:disable-line:no-floating-promises
)
let rejected: any
return p.then(undefined, v => (rejected = v)).then(() => {
assert.strictEqual(rejected, ERROR)
})
})
})
describe('isPromise', () => {
it('returns true for promises', () =>
assert.strictEqual(
isPromise(
new Promise<any>(() => {
/* noop */
})
),
true
))
it('returns true for non-promises', () => {
assert.strictEqual(isPromise(1), false)
assert.strictEqual(isPromise({ then: 1 }), false)
assert.strictEqual(isPromise(new Subject<any>()), false)
})
})
describe('isSubscribable', () => {
it('returns true for subscribables', () => assert.strictEqual(isSubscribable(new Subject<any>()), true))
it('returns true for non-subscribables', () => {
assert.strictEqual(isSubscribable(1), false)
assert.strictEqual(isSubscribable({ subscribe: 1 }), false)
assert.strictEqual(
isSubscribable(
new Promise<any>(() => {
/* noop */
})
),
false
)
})
})