UNPKG

1.93 kBJavaScriptView Raw
1// Copyright IBM Corp. 2016,2019. All Rights Reserved.
2// Node module: loopback-connector
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6'use strict';
7
8const JSONStringPacker = require('../lib/json-string-packer');
9const expect = require('chai').expect;
10
11describe('JSONStringPacker', function() {
12 let packer;
13
14 beforeEach(function createPacker() {
15 packer = new JSONStringPacker();
16 });
17
18 describe('encode()', function() {
19 it('supports invocation with a callback', function(done) {
20 packer.encode('a-value', done);
21 });
22 });
23
24 describe('decode()', function() {
25 it('supports invocation with a callback', function(done) {
26 packer.encode('a-value', function(err, jsonString) {
27 if (err) return done(err);
28 packer.decode(jsonString, function(err, result) {
29 if (err) return done(err);
30 expect(result).to.eql('a-value');
31 done();
32 });
33 });
34 });
35 });
36
37 describe('roundtrip', function() {
38 const TEST_CASES = {
39 String: 'a-value',
40 Object: {a: 1, b: 2},
41 Buffer: new Buffer([1, 2, 3]),
42 Date: new Date('2016-08-03T11:53:03.470Z'),
43 Integer: 12345,
44 Float: 12.345,
45 Boolean: false,
46 };
47
48 Object.keys(TEST_CASES).forEach(function(tc) {
49 it('works for ' + tc + ' values', function() {
50 const value = TEST_CASES[tc];
51 return encodeAndDecode(value)
52 .then(function(result) {
53 expect(result).to.eql(value);
54 });
55 });
56 });
57
58 it('works for nested properties', function() {
59 return encodeAndDecode(TEST_CASES)
60 .then(function(result) {
61 expect(result).to.eql(TEST_CASES);
62 });
63 });
64
65 function encodeAndDecode(value) {
66 return packer.encode(value)
67 .then(function(binary) {
68 return packer.decode(binary);
69 });
70 }
71 });
72});