-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathtodomstream-spec.ts
87 lines (82 loc) · 3.41 KB
/
todomstream-spec.ts
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
import '../iterablehelpers';
import '../asynciterablehelpers.js';
import { as } from 'ix/iterable/index.js';
import { map, toDOMStream } from 'ix/iterable/operators/index.js';
// eslint-disable-next-line consistent-return
(() => {
if (!toDOMStream || process.env.TEST_DOM_STREAMS !== 'true') {
return test('not testing node streams because process.env.TEST_DOM_STREAMS !== "true"', () => {
/**/
});
}
const stringsItr = () => as([1, 2, 3]).pipe(map((i) => `${i}`));
const buffersItr = () => stringsItr().pipe(map((val) => Buffer.from(val)));
const objectsItr = () => stringsItr().pipe(map((val) => ({ val })));
const compare = <T>(a: T, b: T) => {
const aVal = ArrayBuffer.isView(a) ? `${Buffer.from(a.buffer, a.byteOffset, a.byteLength)}` : a;
const bVal = ArrayBuffer.isView(b) ? `${Buffer.from(b.buffer, b.byteOffset, b.byteLength)}` : b;
// poor man's deep-equals
try {
expect(aVal).toEqual(bVal);
} catch (e) {
return false;
}
return true;
};
describe('Iterable#toDOMStream', () => {
describe('DefaultController', () => {
const expectedStrings = ['1', '2', '3'];
const expectedObjects = expectedStrings.map((val) => ({ val }));
const expectedBuffers = expectedStrings.map((x) => Buffer.from(x));
test('yields Strings', async () => {
const expected = as(expectedStrings);
const actual = stringsItr().pipe(toDOMStream());
await expect(actual).toEqualStream(expected, compare);
});
test('yields Buffers', async () => {
const expected = as(expectedBuffers);
const actual = buffersItr().pipe(toDOMStream());
await expect(actual).toEqualStream(expected, compare);
});
test('yields Objects', async () => {
const expected = as(expectedObjects);
const actual = objectsItr().pipe(toDOMStream());
await expect(actual).toEqualStream(expected, compare);
});
});
describe('ReadableByteStreamController (byobRequest)', () => {
const expectedStrings = ['123'];
const expectedBuffers = expectedStrings.map((x) => Buffer.from(x));
test('yields Strings', async () => {
const expected = as(expectedBuffers);
const actual = stringsItr()
.pipe(map((x) => Buffer.from(x)))
.pipe(toDOMStream({ type: 'bytes' }));
await expect(actual).toEqualStream(expected, compare);
});
test('yields Buffers', async () => {
const expected = as(expectedBuffers);
const actual = buffersItr().pipe(toDOMStream({ type: 'bytes' }));
await expect(actual).toEqualStream(expected, compare);
});
});
describe('ReadableByteStreamController (autoAllocateChunkSize)', () => {
const expectedStrings = ['123'];
const expectedBuffers = expectedStrings.map((x) => Buffer.from(x));
test('yields Strings', async () => {
const expected = as(expectedBuffers);
const actual = stringsItr()
.pipe(map((x) => Buffer.from(x)))
.pipe(toDOMStream({ type: 'bytes', autoAllocateChunkSize: 1024 }));
await expect(actual).toEqualStream(expected, compare);
});
test('yields Buffers', async () => {
const expected = as(expectedBuffers);
const actual = buffersItr().pipe(
toDOMStream({ type: 'bytes', autoAllocateChunkSize: 1024 })
);
await expect(actual).toEqualStream(expected, compare);
});
});
});
})();