aboutsummaryrefslogtreecommitdiff
path: root/node_modules/unique-stream/test/index.js
blob: fca02b7e1f8a0acd7e9a6b75626ca8d038653193 (plain)
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
var expect = require('chai').expect
  , unique = require('..')
  , Stream = require('stream')
  , after = require('after')
  , setImmediate = global.setImmediate || process.nextTick;

describe('unique stream', function() {

  function makeStream(type) {
    var s = new Stream();
    s.readable = true;

    var n = 10;
    var next = after(n, function () {
      setImmediate(function () {
        s.emit('end');
      });
    });

    for (var i = 0; i < n; i++) {
      var o = {
        type: type,
        name: 'name ' + i,
        number: i * 10
      };

      (function (o) {
        setImmediate(function () {
          s.emit('data', o);
          next();
        });
      })(o);
    }
    return s;
  }

  it('should be able to uniqueify objects based on JSON data', function(done) {
    var aggregator = unique();
    makeStream('a')
      .pipe(aggregator);
    makeStream('a')
      .pipe(aggregator);

    var n = 0;
    aggregator
      .on('data', function () {
        n++;
      })
      .on('end', function () {
        expect(n).to.equal(10);
        done();
      });
  });

  it('should be able to uniqueify objects based on a property', function(done) {
    var aggregator = unique('number');
    makeStream('a')
      .pipe(aggregator);
    makeStream('b')
      .pipe(aggregator);

    var n = 0;
    aggregator
      .on('data', function () {
        n++;
      })
      .on('end', function () {
        expect(n).to.equal(10);
        done();
      });
  });

  it('should be able to uniqueify objects based on a function', function(done) {
    var aggregator = unique(function (data) {
      return data.name;
    });

    makeStream('a')
      .pipe(aggregator);
    makeStream('b')
      .pipe(aggregator);

    var n = 0;
    aggregator
      .on('data', function () {
        n++;
      })
      .on('end', function () {
        expect(n).to.equal(10);
        done();
      });
  });

  it('should be able to handle uniqueness when not piped', function(done) {
    var stream = unique();
    var count = 0;
    stream.on('data', function (data) {
      expect(data).to.equal('hello');
      count++;
    });
    stream.on('end', function() {
      expect(count).to.equal(1);
      done();
    });
    stream.write('hello');
    stream.write('hello');
    stream.end();
  });
});