Blame view

node_modules/bfj-node4/test/unit/stringify.js 4.47 KB
aaac7fed   liuqimichale   add
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  'use strict'
  
  const assert = require('chai').assert
  const proxyquire = require('proxyquire')
  const spooks = require('spooks')
  const Promise = require('bluebird')
  
  const modulePath = '../../src/stringify'
  
  suite('stringify:', () => {
    test('require does not throw', () => {
      assert.doesNotThrow(() => {
        require(modulePath)
      })
    })
  
    test('require returns function', () => {
      assert.isFunction(require(modulePath))
    })
  
    suite('require:', () => {
      let log, stringify
  
      setup(() => {
        log = {}
  
        stringify = proxyquire(modulePath, {
          './streamify': spooks.fn({
            name: 'streamify',
            log: log,
            results: [
              { on: spooks.fn({ name: 'on', log: log }) }
            ]
          })
        })
      })
  
      test('stringify expects two arguments', () => {
        assert.lengthOf(stringify, 2)
      })
  
      test('stringify does not throw', () => {
        assert.doesNotThrow(() => {
          stringify()
        })
      })
  
      test('stringify returns promise', () => {
        assert.instanceOf(stringify(), Promise)
      })
  
      test('streamify was not called', () => {
        assert.strictEqual(log.counts.streamify, 0)
      })
  
      suite('stringify:', () => {
        let data, options, resolved, rejected, result, done
  
        setup(() => {
          data = {}
          options = {}
          stringify(data, options)
            .then(res => {
              resolved = res
              done()
            })
            .catch(rej => {
              rejected = rej
              done()
            })
        })
  
        teardown(() => {
          resolved = rejected = undefined
        })
  
        test('streamify was called once', () => {
          assert.strictEqual(log.counts.streamify, 1)
          assert.isUndefined(log.these.streamify[0])
        })
  
        test('streamify was called correctly', () => {
          assert.lengthOf(log.args.streamify[0], 2)
          assert.strictEqual(log.args.streamify[0][0], data)
          assert.lengthOf(Object.keys(log.args.streamify[0][0]), 0)
          assert.strictEqual(log.args.streamify[0][1], options)
          assert.lengthOf(Object.keys(log.args.streamify[0][1]), 0)
        })
  
        test('stream.on was called three times', () => {
          assert.strictEqual(log.counts.on, 3)
        })
  
        test('stream.on was called correctly first time', () => {
          assert.lengthOf(log.args.on[0], 2)
          assert.strictEqual(log.args.on[0][0], 'data')
          assert.isFunction(log.args.on[0][1])
        })
  
        test('stream.on was called correctly second time', () => {
          assert.strictEqual(log.args.on[1][0], 'end')
          assert.isFunction(log.args.on[1][1])
          assert.notStrictEqual(log.args.on[1][1], log.args.on[0][1])
        })
  
        test('stream.on was called correctly third time', () => {
          assert.strictEqual(log.args.on[2][0], 'dataError')
          assert.isFunction(log.args.on[2][1])
          assert.notStrictEqual(log.args.on[2][1], log.args.on[0][1])
          assert.notStrictEqual(log.args.on[2][1], log.args.on[1][1])
        })
  
        test('promise is unfulfilled', () => {
          assert.isUndefined(resolved)
          assert.isUndefined(rejected)
        })
  
        suite('data event:', () => {
          setup(() => {
            log.args.on[0][1]('foo')
          })
  
          test('promise is unfulfilled', () => {
            assert.isUndefined(resolved)
            assert.isUndefined(rejected)
          })
  
          suite('end event:', () => {
            setup(d => {
              done = d
              log.args.on[1][1]()
            })
  
            test('promise is resolved', () => {
              assert.strictEqual(resolved, 'foo')
            })
  
            test('promise is not rejected', () => {
              assert.isUndefined(rejected)
            })
          })
  
          suite('data event:', () => {
            setup(() => {
              log.args.on[0][1]('bar')
            })
  
            test('promise is unfulfilled', () => {
              assert.isUndefined(resolved)
              assert.isUndefined(rejected)
            })
  
            suite('end event:', () => {
              setup(d => {
                done = d
                log.args.on[1][1]()
              })
  
              test('promise is resolved', () => {
                assert.strictEqual(resolved, 'foobar')
              })
            })
  
            suite('dataError event:', () => {
              setup(d => {
                done = d
                log.args.on[2][1]('wibble')
              })
  
              test('promise is rejected', () => {
                assert.strictEqual(rejected, 'wibble')
              })
            })
          })
        })
      })
    })
  })