2
2
3
3
var remove = require ( '..' ) ;
4
4
5
- var it = require ( 'tape' ) ,
5
+ var test = require ( 'tape' ) ,
6
6
u = require ( 'unist-builder' ) ;
7
7
8
8
9
- it ( 'should compare nodes by partial properties' , function ( t ) {
9
+ test ( 'should compare nodes by partial properties' , function ( t ) {
10
10
var ast = u ( 'node' , [
11
11
u ( 'node' , 'foo' ) ,
12
12
u ( 'node' , 'bar' )
@@ -24,7 +24,7 @@ it('should compare nodes by partial properties', function (t) {
24
24
} ) ;
25
25
26
26
27
- it ( 'should remove nodes with children' , function ( t ) {
27
+ test ( 'should remove nodes with children' , function ( t ) {
28
28
var ast = u ( 'root' , [
29
29
u ( 'node' , [
30
30
u ( 'leaf' , 1 )
@@ -46,7 +46,7 @@ it('should remove nodes with children', function (t) {
46
46
} ) ;
47
47
48
48
49
- it ( 'should return `null` if root node is removed' , function ( t ) {
49
+ test ( 'should return `null` if root node is removed' , function ( t ) {
50
50
var ast = u ( 'root' , [
51
51
u ( 'node' , [
52
52
u ( 'leaf' , 1 )
@@ -59,7 +59,7 @@ it('should return `null` if root node is removed', function (t) {
59
59
} ) ;
60
60
61
61
62
- it ( 'should cascade remove parent nodes' , function ( t ) {
62
+ test ( 'should cascade remove parent nodes' , function ( t ) {
63
63
t . test ( function ( t ) {
64
64
var ast = u ( 'root' , [
65
65
u ( 'node' , [
@@ -97,7 +97,7 @@ it('should cascade remove parent nodes', function (t) {
97
97
} ) ;
98
98
99
99
100
- it ( 'should not cascade-remove nodes that were empty initially' , function ( t ) {
100
+ test ( 'should not cascade-remove nodes that were empty initially' , function ( t ) {
101
101
var ast = u ( 'node' , [
102
102
u ( 'node' , [ ] ) ,
103
103
u ( 'node' , [
@@ -114,7 +114,7 @@ it('should not cascade-remove nodes that were empty initially', function (t) {
114
114
} ) ;
115
115
116
116
117
- it ( 'should support type tests and predicate functions' , function ( t ) {
117
+ test ( 'should support type tests and predicate functions' , function ( t ) {
118
118
t . test ( function ( t ) {
119
119
var ast = u ( 'node' , [
120
120
u ( 'node' , [
@@ -169,3 +169,76 @@ it('should support type tests and predicate functions', function (t) {
169
169
t . end ( ) ;
170
170
} ) ;
171
171
} ) ;
172
+
173
+ test ( 'opts.cascade' , function ( t ) {
174
+ t . test ( 'opts.cascade = true' , function ( t ) {
175
+ var ast = u ( 'root' , [
176
+ u ( 'node' , [
177
+ u ( 'leaf' , 1 )
178
+ ] ) ,
179
+ u ( 'leaf' , 2 )
180
+ ] ) ;
181
+
182
+ var newAst = remove ( ast , { cascade : true } , function ( node ) {
183
+ return node === ast . children [ 0 ] . children [ 0 ] || node === ast . children [ 1 ] ;
184
+ } ) ;
185
+
186
+ t . equal ( newAst , null ) ;
187
+ t . end ( ) ;
188
+ } ) ;
189
+
190
+ t . test ( 'opts.cascade = false' , function ( t ) {
191
+ var ast = u ( 'root' , [
192
+ u ( 'node' , [
193
+ u ( 'leaf' , 1 )
194
+ ] ) ,
195
+ u ( 'leaf' , 2 )
196
+ ] ) ;
197
+ var children = ast . children ;
198
+ var innerNode = ast . children [ 0 ] ;
199
+ var grandChildren = ast . children [ 0 ] . children ;
200
+
201
+ var newAst = remove ( ast , { cascade : false } , function ( node ) {
202
+ return node === ast . children [ 0 ] . children [ 0 ] || node === ast . children [ 1 ] ;
203
+ } ) ;
204
+
205
+ t . equal ( newAst , ast ) ;
206
+ t . deepEqual ( ast , u ( 'root' , [
207
+ u ( 'node' , [ ] )
208
+ ] ) ) ;
209
+ t . equal ( ast . children , children ) ;
210
+ t . equal ( ast . children [ 0 ] , innerNode ) ;
211
+ t . equal ( ast . children [ 0 ] . children , grandChildren ) ;
212
+ t . end ( ) ;
213
+ } ) ;
214
+ } ) ;
215
+
216
+ test ( 'example from README' , function ( t ) {
217
+ var ast = u ( 'root' , [
218
+ u ( 'leaf' , 1 ) ,
219
+ u ( 'node' , [
220
+ u ( 'leaf' , 2 ) ,
221
+ u ( 'node' , [
222
+ u ( 'leaf' , 3 ) ,
223
+ u ( 'other' , 4 )
224
+ ] ) ,
225
+ u ( 'node' , [
226
+ u ( 'leaf' , 5 ) ,
227
+ ] )
228
+ ] ) ,
229
+ u ( 'leaf' , 6 )
230
+ ] ) ;
231
+
232
+ t . deepEqual (
233
+ remove ( ast , 'leaf' ) ,
234
+ u ( 'root' , [
235
+ u ( 'node' , [
236
+ u ( 'node' , [
237
+ u ( 'other' , 4 )
238
+ ] )
239
+ ] )
240
+ ] )
241
+ ) ;
242
+
243
+ t . end ( ) ;
244
+ } ) ;
0 commit comments