-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathnormalise-input.spec.js
150 lines (118 loc) · 4.03 KB
/
normalise-input.spec.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict'
/* eslint-env mocha */
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const normalise = require('../../src/files/normalise-input')
const { supportsFileReader } = require('../../src/supports')
const { Buffer } = require('buffer')
const all = require('it-all')
const globalThis = require('../../src/globalthis')
chai.use(dirtyChai)
const expect = chai.expect
const STRING = () => 'hello world'
const BUFFER = () => Buffer.from(STRING())
const ARRAY = () => Array.from(BUFFER())
const TYPEDARRAY = () => Uint8Array.from(ARRAY())
let BLOB
let WINDOW_READABLE_STREAM
if (supportsFileReader) {
BLOB = () => new globalThis.Blob([
STRING()
])
WINDOW_READABLE_STREAM = () => new globalThis.ReadableStream({
start (controller) {
controller.enqueue(BUFFER())
controller.close()
}
})
}
async function verifyNormalisation (input) {
expect(input.length).to.equal(1)
if (!input[0].content[Symbol.asyncIterator] && !input[0].content[Symbol.iterator]) {
chai.assert.fail('Content should have been an iterable or an async iterable')
}
expect(await all(input[0].content)).to.deep.equal([BUFFER()])
expect(input[0].path).to.equal('')
}
async function testContent (input) {
const result = await all(normalise(input))
await verifyNormalisation(result)
}
function iterableOf (thing) {
return [thing]
}
function asyncIterableOf (thing) {
return (async function * () { // eslint-disable-line require-await
yield thing
}())
}
describe('normalise-input', function () {
function testInputType (content, name, isBytes) {
it(name, async function () {
await testContent(content())
})
if (isBytes) {
it(`Iterable<${name}>`, async function () {
await testContent(iterableOf(content()))
})
it(`AsyncIterable<${name}>`, async function () {
await testContent(asyncIterableOf(content()))
})
}
it(`{ path: '', content: ${name} }`, async function () {
await testContent({ path: '', content: content() })
})
if (isBytes) {
it(`{ path: '', content: Iterable<${name}> }`, async function () {
await testContent({ path: '', content: iterableOf(content()) })
})
it(`{ path: '', content: AsyncIterable<${name}> }`, async function () {
await testContent({ path: '', content: asyncIterableOf(content()) })
})
}
it(`Iterable<{ path: '', content: ${name} }`, async function () {
await testContent(iterableOf({ path: '', content: content() }))
})
it(`AsyncIterable<{ path: '', content: ${name} }`, async function () {
await testContent(asyncIterableOf({ path: '', content: content() }))
})
if (isBytes) {
it(`Iterable<{ path: '', content: Iterable<${name}> }>`, async function () {
await testContent(iterableOf({ path: '', content: iterableOf(content()) }))
})
it(`Iterable<{ path: '', content: AsyncIterable<${name}> }>`, async function () {
await testContent(iterableOf({ path: '', content: asyncIterableOf(content()) }))
})
it(`AsyncIterable<{ path: '', content: Iterable<${name}> }>`, async function () {
await testContent(asyncIterableOf({ path: '', content: iterableOf(content()) }))
})
it(`AsyncIterable<{ path: '', content: AsyncIterable<${name}> }>`, async function () {
await testContent(asyncIterableOf({ path: '', content: asyncIterableOf(content()) }))
})
}
}
describe('String', () => {
testInputType(STRING, 'String', false)
})
describe('Buffer', () => {
testInputType(BUFFER, 'Buffer', true)
})
describe('Blob', () => {
if (!supportsFileReader) {
return
}
testInputType(BLOB, 'Blob', false)
})
describe('window.ReadableStream', () => {
if (!supportsFileReader) {
return
}
testInputType(WINDOW_READABLE_STREAM, 'window.ReadableStream', false)
})
describe('Iterable<Number>', () => {
testInputType(ARRAY, 'Iterable<Number>', false)
})
describe('TypedArray', () => {
testInputType(TYPEDARRAY, 'TypedArray', true)
})
})