|
1 |
| -// LICENSE : MIT |
2 | 1 | 'use strict'
|
3 | 2 |
|
4 |
| -/* eslint-env mocha */ |
| 3 | +var test = require('tape') |
| 4 | +var assign = require('object-assign') |
| 5 | +var u = require('unist-builder') |
| 6 | +var map = require('.') |
5 | 7 |
|
6 |
| -const assert = require('assert') |
7 |
| -const assign = require('object-assign') |
8 |
| -const map = require('.') |
| 8 | +test('unist-util-map', function(t) { |
| 9 | + t.deepEqual( |
| 10 | + map(u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]), changeLeaf), |
| 11 | + u('root', [u('node', [u('leaf', 'CHANGED')]), u('leaf', 'CHANGED')]), |
| 12 | + 'should map the specified node' |
| 13 | + ) |
9 | 14 |
|
10 |
| -describe('should not traverse into children of filtered out nodes', function() { |
11 |
| - it('should map specified node', function() { |
12 |
| - const ast = { |
13 |
| - type: 'root', |
14 |
| - children: [ |
15 |
| - { |
16 |
| - type: 'node', |
17 |
| - children: [{type: 'leaf', value: '1'}] |
18 |
| - }, |
19 |
| - {type: 'leaf', value: '2'} |
20 |
| - ] |
21 |
| - } |
22 |
| - const actual = map(ast, function(node) { |
23 |
| - if (node.type === 'leaf') { |
24 |
| - return assign({}, node, { |
25 |
| - value: 'CHANGED' |
26 |
| - }) |
27 |
| - } |
| 15 | + t.deepEqual( |
| 16 | + map(u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]), nullLeaf), |
| 17 | + u('root', [u('node', [{}]), {}]), |
| 18 | + 'should work when retuning an empty object' |
| 19 | + ) |
28 | 20 |
|
29 |
| - // No change |
30 |
| - return node |
31 |
| - }) |
32 |
| - const expected = { |
33 |
| - type: 'root', |
34 |
| - children: [ |
35 |
| - { |
36 |
| - type: 'node', |
37 |
| - children: [{type: 'leaf', value: 'CHANGED'}] |
38 |
| - }, |
39 |
| - {type: 'leaf', value: 'CHANGED'} |
40 |
| - ] |
41 |
| - } |
42 |
| - assert.deepStrictEqual(actual, expected) |
43 |
| - }) |
44 |
| - context('when return null', function() { |
45 |
| - it('should map as empty object', function() { |
46 |
| - const ast = { |
47 |
| - type: 'root', |
48 |
| - children: [ |
49 |
| - { |
50 |
| - type: 'node', |
51 |
| - children: [{type: 'leaf', value: '1'}] |
52 |
| - }, |
53 |
| - {type: 'leaf', value: '2'} |
54 |
| - ] |
55 |
| - } |
56 |
| - const actual = map(ast, function(node) { |
57 |
| - if (node.type === 'leaf') { |
58 |
| - return null |
59 |
| - } |
| 21 | + t.deepEqual( |
| 22 | + map({}, addValue), |
| 23 | + {value: 'test'}, |
| 24 | + 'should work when passing an empty object' |
| 25 | + ) |
60 | 26 |
|
61 |
| - // No change |
62 |
| - return node |
63 |
| - }) |
64 |
| - const expected = { |
65 |
| - type: 'root', |
66 |
| - children: [ |
67 |
| - { |
68 |
| - type: 'node', |
69 |
| - children: [{}] |
70 |
| - }, |
71 |
| - {} |
72 |
| - ] |
73 |
| - } |
74 |
| - assert.deepStrictEqual(actual, expected) |
75 |
| - }) |
76 |
| - }) |
| 27 | + t.end() |
77 | 28 |
|
78 |
| - context('when pass empty object', function() { |
79 |
| - it('should work map', function() { |
80 |
| - const ast = {} |
81 |
| - const actual = map(ast, function() { |
82 |
| - return { |
83 |
| - value: 'test' |
84 |
| - } |
85 |
| - }) |
86 |
| - const expected = { |
87 |
| - value: 'test' |
88 |
| - } |
89 |
| - assert.deepStrictEqual(actual, expected) |
90 |
| - }) |
91 |
| - }) |
| 29 | + function changeLeaf(node) { |
| 30 | + return node.type === 'leaf' ? assign({}, node, {value: 'CHANGED'}) : node |
| 31 | + } |
| 32 | + |
| 33 | + function nullLeaf(node) { |
| 34 | + return node.type === 'leaf' ? null : node |
| 35 | + } |
| 36 | + |
| 37 | + function addValue() { |
| 38 | + return {value: 'test'} |
| 39 | + } |
92 | 40 | })
|
0 commit comments