Skip to content

Commit f38459f

Browse files
committed
Refactor tests
1 parent f405886 commit f38459f

File tree

2 files changed

+36
-87
lines changed

2 files changed

+36
-87
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@
2424
"object-assign": "^4.0.1"
2525
},
2626
"devDependencies": {
27-
"mocha": "^6.0.0",
2827
"nyc": "^14.0.0",
2928
"prettier": "^1.0.0",
3029
"remark-cli": "^6.0.0",
3130
"remark-preset-wooorm": "^5.0.0",
31+
"tape": "^4.10.1",
32+
"unist-builder": "^1.0.3",
3233
"xo": "^0.24.0"
3334
},
3435
"scripts": {
3536
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
36-
"test-api": "mocha",
37-
"test-coverage": "nyc --reporter lcov mocha test.js",
37+
"test-api": "node test",
38+
"test-coverage": "nyc --reporter lcov tape test.js",
3839
"test": "npm run format && npm run test-coverage"
3940
},
4041
"nyc": {

test.js

Lines changed: 32 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,40 @@
1-
// LICENSE : MIT
21
'use strict'
32

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('.')
57

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+
)
914

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+
)
2820

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+
)
6026

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()
7728

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+
}
9240
})

0 commit comments

Comments
 (0)