1
1
import * as unified from 'unified' ;
2
2
import * as markdown from 'remark-parse' ;
3
- import { Ast , Plugin } from '../src/ ' ;
3
+ import { Ast , Plugin } from '../src' ;
4
4
import { setGlobalConfig , getGlobalConfig } from '../src/global' ;
5
5
import { TestPlugin } from './TestPlugin' ;
6
6
7
7
describe ( 'index' , ( ) => {
8
-
9
8
test ( 'exports' , ( ) => {
10
9
expect ( Ast ) . not . toBe ( undefined ) ;
11
10
expect ( Plugin ) . not . toBe ( undefined ) ;
@@ -65,6 +64,7 @@ describe('index', () => {
65
64
expect ( plugin2 . doDelete ) . toHaveBeenCalledTimes ( 0 ) ;
66
65
} ) ;
67
66
67
+
68
68
test ( 'get' , ( ) => {
69
69
const ast = unified ( )
70
70
. use ( markdown )
@@ -77,6 +77,7 @@ describe('index', () => {
77
77
expect ( new Ast ( ast ) . get ( 'aaa.bbb' ) ) . toBe ( undefined ) ;
78
78
} ) ;
79
79
80
+
80
81
test ( 'segment' , ( ) => {
81
82
let md = `\`\`\`js
82
83
cont a = 1;
@@ -103,6 +104,7 @@ const b = 2;
103
104
expect ( ast . get ( 'children.0.children.0' ) . segment ( ) ) . toBe ( 'Hello **world**!' ) ;
104
105
} ) ;
105
106
107
+
106
108
test ( 'throwError call' , ( ) => {
107
109
const throwErrFn = jest . fn ( ) ;
108
110
const testErrorPlugin = new TestPlugin ( {
@@ -121,11 +123,49 @@ const b = 2;
121
123
} ) ;
122
124
} ) ;
123
125
126
+
124
127
test ( 'global configure' , ( ) => {
125
128
expect ( getGlobalConfig ( ) ) . toEqual ( { typeKey : 'type' , childrenKey : 'children' } ) ;
126
129
127
130
setGlobalConfig ( { typeKey : 'type' } ) ;
128
131
129
132
expect ( getGlobalConfig ( ) ) . toEqual ( { typeKey : 'type' } ) ;
133
+
134
+ // 上述操作有副作用,会影响下面的 tests, 需要重新复原
135
+ setGlobalConfig ( { typeKey : 'type' , childrenKey : 'children' } ) ;
136
+ expect ( getGlobalConfig ( ) ) . toEqual ( { typeKey : 'type' , childrenKey : 'children' } ) ;
137
+ } ) ;
138
+
139
+ test ( 'should have parent in child ast' , ( ) => {
140
+ const mock = jest . fn ( ( ast : Ast ) => {
141
+ expect ( ast . parent . node . type ) . toStrictEqual ( 'paragraph' ) ;
142
+ } ) ;
143
+
144
+ class MyPlugin extends Plugin {
145
+ post ( ) : void {
146
+ }
147
+
148
+ pre ( ) : void {
149
+ }
150
+
151
+ visitor ( ) : any {
152
+ return {
153
+ strong : mock
154
+ } ;
155
+ }
156
+ }
157
+
158
+ const a = unified ( )
159
+ . use ( markdown )
160
+ . parse ( 'Hello **world**!' ) ;
161
+
162
+
163
+ const p = new MyPlugin ( {
164
+ throwError : undefined
165
+ } ) ;
166
+
167
+ new Ast ( a ) . traverse ( [ p ] ) ;
168
+
169
+ expect ( mock ) . toBeCalled ( ) ;
130
170
} ) ;
131
171
} ) ;
0 commit comments