source: node_modules/traverse/test/json.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.0 KB
Line 
1'use strict';
2
3var test = require('tape');
4var traverse = require('../');
5
6test('json test', function (t) {
7 var id = 54;
8 var callbacks = {};
9 var obj = { moo: function () {}, foo: [2, 3, 4, function () {}] };
10
11 var scrubbed = traverse(obj).map(function (x) {
12 if (typeof x === 'function') {
13 callbacks[id] = { id: id, f: x, path: this.path };
14 this.update('[Function]');
15 id += 1;
16 }
17 });
18
19 t.equal(
20 scrubbed.moo,
21 '[Function]',
22 'obj.moo replaced with "[Function]"'
23 );
24
25 t.equal(
26 scrubbed.foo[3],
27 '[Function]',
28 'obj.foo[3] replaced with "[Function]"'
29 );
30
31 t.same(scrubbed, {
32 moo: '[Function]',
33 foo: [2, 3, 4, '[Function]'],
34 }, 'Full JSON string matches');
35
36 t.same(
37 typeof obj.moo,
38 'function',
39 'Original obj.moo still a function'
40 );
41
42 t.same(
43 typeof obj.foo[3],
44 'function',
45 'Original obj.foo[3] still a function'
46 );
47
48 t.same(callbacks, {
49 54: { id: 54, f: obj.moo, path: ['moo'] },
50 55: { id: 55, f: obj.foo[3], path: ['foo', '3'] },
51 }, 'Check the generated callbacks list');
52
53 t.end();
54});
55
Note: See TracBrowser for help on using the repository browser.