source: frontend/node_modules/bfj/test/unit/stringify.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 5.0 KB
Line 
1'use strict'
2
3const assert = require('chai').assert
4const proxyquire = require('proxyquire')
5const spooks = require('spooks')
6const Promise = require('bluebird')
7
8const modulePath = '../../src/stringify'
9
10suite('stringify:', () => {
11 test('require does not throw', () => {
12 assert.doesNotThrow(() => {
13 require(modulePath)
14 })
15 })
16
17 test('require returns function', () => {
18 assert.isFunction(require(modulePath))
19 })
20
21 suite('require:', () => {
22 let log, stringify
23
24 setup(() => {
25 log = {}
26
27 stringify = proxyquire(modulePath, {
28 './streamify': spooks.fn({
29 name: 'streamify',
30 log: log,
31 results: [
32 { on: spooks.fn({ name: 'on', log: log }) }
33 ]
34 })
35 })
36 })
37
38 test('stringify expects two arguments', () => {
39 assert.lengthOf(stringify, 2)
40 })
41
42 test('stringify does not throw', () => {
43 assert.doesNotThrow(() => {
44 stringify()
45 })
46 })
47
48 test('stringify returns promise', () => {
49 assert.instanceOf(stringify(), Promise)
50 })
51
52 test('streamify was not called', () => {
53 assert.strictEqual(log.counts.streamify, 0)
54 })
55
56 suite('stringify:', () => {
57 let data, options, resolved, rejected, result, done
58
59 setup(() => {
60 data = {}
61 options = {}
62 stringify(data, options)
63 .then(res => {
64 resolved = res
65 done()
66 })
67 .catch(rej => {
68 rejected = rej
69 done()
70 })
71 })
72
73 teardown(() => {
74 resolved = rejected = undefined
75 })
76
77 test('streamify was called once', () => {
78 assert.strictEqual(log.counts.streamify, 1)
79 assert.isUndefined(log.these.streamify[0])
80 })
81
82 test('streamify was called correctly', () => {
83 assert.lengthOf(log.args.streamify[0], 2)
84 assert.strictEqual(log.args.streamify[0][0], data)
85 assert.lengthOf(Object.keys(log.args.streamify[0][0]), 0)
86 assert.strictEqual(log.args.streamify[0][1], options)
87 assert.lengthOf(Object.keys(log.args.streamify[0][1]), 0)
88 })
89
90 test('stream.on was called four times', () => {
91 assert.strictEqual(log.counts.on, 4)
92 })
93
94 test('stream.on was called correctly first time', () => {
95 assert.lengthOf(log.args.on[0], 2)
96 assert.strictEqual(log.args.on[0][0], 'data')
97 assert.isFunction(log.args.on[0][1])
98 })
99
100 test('stream.on was called correctly second time', () => {
101 assert.strictEqual(log.args.on[1][0], 'end')
102 assert.isFunction(log.args.on[1][1])
103 assert.notStrictEqual(log.args.on[1][1], log.args.on[0][1])
104 })
105
106 test('stream.on was called correctly third time', () => {
107 assert.strictEqual(log.args.on[2][0], 'error')
108 assert.isFunction(log.args.on[2][1])
109 assert.notStrictEqual(log.args.on[2][1], log.args.on[0][1])
110 assert.notStrictEqual(log.args.on[2][1], log.args.on[1][1])
111 })
112
113 test('stream.on was called correctly fourth time', () => {
114 assert.strictEqual(log.args.on[3][0], 'dataError')
115 assert.isFunction(log.args.on[3][1])
116 assert.strictEqual(log.args.on[3][1], log.args.on[2][1])
117 })
118
119 test('promise is unfulfilled', () => {
120 assert.isUndefined(resolved)
121 assert.isUndefined(rejected)
122 })
123
124 suite('data event:', () => {
125 setup(() => {
126 log.args.on[0][1]('foo')
127 })
128
129 test('promise is unfulfilled', () => {
130 assert.isUndefined(resolved)
131 assert.isUndefined(rejected)
132 })
133
134 suite('end event:', () => {
135 setup(d => {
136 done = d
137 log.args.on[1][1]()
138 })
139
140 test('promise is resolved', () => {
141 assert.strictEqual(resolved, 'foo')
142 })
143
144 test('promise is not rejected', () => {
145 assert.isUndefined(rejected)
146 })
147 })
148
149 suite('data event:', () => {
150 setup(() => {
151 log.args.on[0][1]('bar')
152 })
153
154 test('promise is unfulfilled', () => {
155 assert.isUndefined(resolved)
156 assert.isUndefined(rejected)
157 })
158
159 suite('end event:', () => {
160 setup(d => {
161 done = d
162 log.args.on[1][1]()
163 })
164
165 test('promise is resolved', () => {
166 assert.strictEqual(resolved, 'foobar')
167 })
168 })
169
170 suite('error event:', () => {
171 setup(d => {
172 done = d
173 log.args.on[2][1]('wibble')
174 })
175
176 test('promise is rejected', () => {
177 assert.strictEqual(rejected, 'wibble')
178 })
179 })
180
181 suite('dataError event:', () => {
182 setup(d => {
183 done = d
184 log.args.on[3][1]('wibble')
185 })
186
187 test('promise is rejected', () => {
188 assert.strictEqual(rejected, 'wibble')
189 })
190 })
191 })
192 })
193 })
194 })
195})
Note: See TracBrowser for help on using the repository browser.