File: test.js

package info (click to toggle)
node-object-visit 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 112 kB
  • sloc: makefile: 4; sh: 2
file content (73 lines) | stat: -rw-r--r-- 1,524 bytes parent folder | download | duplicates (2)
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
'use strict';

require('mocha');
var assert = require('assert');
var visit = require('./');

var obj = {
  a: 'a',
  b: 'b',
  c: 'c',
  d: { e: 'f' }
};

var ctx = {
  data: {},
  cwd: null,
  set: function(key, value) {
    if (typeof key === 'object') {
      if (value && value.cwd) {
        this.cwd = value.cwd;
      }
      visit(ctx, 'set', key, value);
    } else {
      ctx.data[key] = value;
    }
  }
};

describe('object-visit', function() {
  describe('visit', function() {
    beforeEach(function() {
      ctx.data = {};
    });

    it('should call visit on every value in the given object:', function() {
      ctx.set('a', 'a');
      ctx.set('b', 'b');
      ctx.set('c', 'c');
      ctx.set({d: {e: 'f'}});
      assert.deepEqual(ctx.data, obj);
    });

    it('should expose additional arguments to the method', function() {
      ctx.set('a', 'a');
      ctx.set('b', 'b');
      ctx.set('c', 'c');
      ctx.set({d: {e: 'f'}}, {cwd: process.cwd()});

      assert.equal(ctx.cwd, process.cwd());
      assert.deepEqual(ctx.data, obj);
    });
  });

  describe('errors', function() {
    it('should throw an error when invalid args are passed:', function() {
      assert.throws(function() {
        visit();
      }, /thisArg/);

      assert.throws(function() {
        visit({}, {}, 'foo');
      }, /method/);

      assert.throws(function() {
        visit('foo', 'bar');
      }, /thisArg/);

      assert.throws(function() {
        visit({}, {}, {});
      }, /method/);
    });
  });
});