-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathstream-frames-test.js
54 lines (51 loc) · 1.42 KB
/
stream-frames-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
import { module, test } from 'qunit';
import { decode } from 'nomad-ui/utils/stream-frames';
import { TextEncoderLite } from 'text-encoder-lite';
import base64js from 'base64-js';
const Encoder = new TextEncoderLite('utf-8');
const encode = str => base64js.fromByteArray(Encoder.encode(str));
module('Unit | Util | stream-frames', function() {
const { btoa } = window;
const decodeTestCases = [
{
name: 'Single frame',
in: `{"Offset":100,"Data":"${btoa('Hello World')}"}`,
out: {
offset: 100,
message: 'Hello World',
},
},
{
name: 'Multiple frames',
// prettier-ignore
in: `{"Offset":1,"Data":"${btoa('One fish,')}"}{"Offset":2,"Data":"${btoa( ' Two fish.')}"}{"Offset":3,"Data":"${btoa(' Red fish, ')}"}{"Offset":4,"Data":"${btoa('Blue fish.')}"}`,
out: {
offset: 4,
message: 'One fish, Two fish. Red fish, Blue fish.',
},
},
{
name: 'Empty frames',
in: '{}{}{}',
out: {},
},
{
name: 'Empty string',
in: '',
out: {},
},
{
name: 'Multi-byte unicode',
in: `{"Offset":1,"Data":"${encode('ワンワン 🐶')}"}`,
out: {
offset: 1,
message: 'ワンワン 🐶',
},
},
];
decodeTestCases.forEach(testCase => {
test(`decode: ${testCase.name}`, function(assert) {
assert.deepEqual(decode(testCase.in), testCase.out);
});
});
});