1
- const { insertPluginByStage, topologicalSorting } = require ( '../lib/pluginOrder.js' )
1
+ const { insertPluginByStage, topologicalSorting, arrangePlugins } = require ( '../lib/pluginOrder.js' )
2
2
3
3
/**
4
4
*
@@ -20,7 +20,7 @@ function plugin (id, order) {
20
20
}
21
21
22
22
describe ( 'insertPluginByStage' , ( ) => {
23
- test ( 'No stage specified' , ( ) => {
23
+ test ( `using default 'stage' will preserve sort order` , ( ) => {
24
24
const plugins = [
25
25
plugin ( 'foo' ) ,
26
26
plugin ( 'bar' ) ,
@@ -34,27 +34,47 @@ describe('insertPluginByStage', () => {
34
34
expect ( orderPlugins ) . toEqual ( plugins )
35
35
} )
36
36
37
- test ( 'stage specified' , ( ) => {
37
+ test ( `using same 'stage' will preserve sort order` , ( ) => {
38
+ const plugins = [
39
+ plugin ( 'foo' , { stage : 200 } ) ,
40
+ plugin ( 'bar' , { stage : 200 } ) ,
41
+ plugin ( 'baz' , { stage : 200 } )
42
+ ]
43
+ const orderPlugins = [ ]
44
+ insertPluginByStage ( orderPlugins , plugins [ 0 ] )
45
+ insertPluginByStage ( orderPlugins , plugins [ 1 ] )
46
+ insertPluginByStage ( orderPlugins , plugins [ 2 ] )
47
+
48
+ expect ( orderPlugins ) . toEqual ( plugins )
49
+ } )
50
+
51
+ test ( `several different 'stage'` , ( ) => {
38
52
const plugins = [
39
53
plugin ( 'foo' ) ,
54
+ plugin ( 'zot' , { stage : 200 } ) ,
55
+ plugin ( 'fum' , { stage : 100 } ) ,
40
56
plugin ( 'bar' , { stage : 0 } ) ,
41
57
plugin ( 'baz' , { stage : 200 } )
42
58
]
43
59
const orderPlugins = [ ]
44
60
insertPluginByStage ( orderPlugins , plugins [ 0 ] )
45
61
insertPluginByStage ( orderPlugins , plugins [ 1 ] )
46
62
insertPluginByStage ( orderPlugins , plugins [ 2 ] )
63
+ insertPluginByStage ( orderPlugins , plugins [ 3 ] )
64
+ insertPluginByStage ( orderPlugins , plugins [ 4 ] )
47
65
48
66
expect ( orderPlugins ) . toEqual ( [
49
67
plugin ( 'bar' , { stage : 0 } ) ,
50
68
plugin ( 'foo' ) ,
69
+ plugin ( 'fum' , { stage : 100 } ) ,
70
+ plugin ( 'zot' , { stage : 200 } ) ,
51
71
plugin ( 'baz' , { stage : 200 } )
52
72
] )
53
73
} )
54
74
} )
55
75
56
76
describe ( 'topologicalSorting' , ( ) => {
57
- test ( ' no after specified' , ( ) => {
77
+ test ( ` no specifying 'after' will preserve sort order` , ( ) => {
58
78
const plugins = [
59
79
plugin ( 'foo' ) ,
60
80
plugin ( 'bar' ) ,
@@ -64,7 +84,7 @@ describe('topologicalSorting', () => {
64
84
expect ( orderPlugins ) . toEqual ( plugins )
65
85
} )
66
86
67
- test ( 'after specified' , ( ) => {
87
+ test ( ` 'after' specified` , ( ) => {
68
88
const plugins = [
69
89
plugin ( 'foo' , { after : 'bar' } ) ,
70
90
plugin ( 'bar' , { after : 'baz' } ) ,
@@ -78,7 +98,7 @@ describe('topologicalSorting', () => {
78
98
] )
79
99
} )
80
100
81
- test ( 'after multiple' , ( ) => {
101
+ test ( ` 'after' can be Array<string>` , ( ) => {
82
102
const plugins = [
83
103
plugin ( 'foo' , { after : [ 'bar' , 'baz' ] } ) ,
84
104
plugin ( 'bar' ) ,
@@ -92,17 +112,50 @@ describe('topologicalSorting', () => {
92
112
] )
93
113
} )
94
114
95
- test ( 'it is not possible to order plugins because of cyclic graph, return plugins directly' , ( ) => {
96
- const plugins = [
115
+ test ( 'it is not possible to order plugins because of cyclic graph, return original plugins directly' , ( ) => {
116
+ let plugins = [
97
117
plugin ( 'foo' , { after : 'bar' } ) ,
98
118
plugin ( 'bar' , { after : 'baz' } ) ,
99
119
plugin ( 'baz' , { after : 'foo' } )
100
120
]
101
- const orderPlugins = topologicalSorting ( plugins )
121
+ let orderPlugins = topologicalSorting ( plugins )
122
+ expect ( orderPlugins ) . toEqual ( plugins )
123
+
124
+ plugins = [
125
+ plugin ( 'foo' , { after : 'bar' } ) ,
126
+ plugin ( 'bar' , { after : 'foo' } ) ,
127
+ plugin ( 'baz' )
128
+ ]
129
+ orderPlugins = topologicalSorting ( plugins )
102
130
expect ( orderPlugins ) . toEqual ( plugins )
103
131
} )
104
132
} )
105
133
106
134
describe ( 'arrangePlugins' , ( ) => {
107
- // TODO:
135
+ test ( `arrange plugins which already ordered by 'stage'` , ( ) => {
136
+ const plugins = [
137
+ plugin ( 'bar' , { stage : 100 , after : 'foo' } ) ,
138
+ plugin ( 'foo' , { stage : 100 } ) ,
139
+ plugin ( 'fum' , { stage : 200 , after : 'baz' } ) ,
140
+ plugin ( 'zot' , { stage : 200 , after : 'baz' } ) ,
141
+ plugin ( 'baz' , { stage : 200 } )
142
+ ]
143
+ const orderPlugins = arrangePlugins ( plugins )
144
+ expect ( orderPlugins ) . toEqual ( [
145
+ plugin ( 'foo' , { stage : 100 } ) ,
146
+ plugin ( 'bar' , { stage : 100 , after : 'foo' } ) ,
147
+ plugin ( 'baz' , { stage : 200 } ) ,
148
+ plugin ( 'fum' , { stage : 200 , after : 'baz' } ) ,
149
+ plugin ( 'zot' , { stage : 200 , after : 'baz' } )
150
+ ] )
151
+ } )
152
+
153
+ test ( `'stage' has a higher priority than 'after'` , ( ) => {
154
+ const plugins = [
155
+ plugin ( 'bar' , { stage : 0 , after : 'foo' } ) ,
156
+ plugin ( 'foo' , { stage : 100 } )
157
+ ]
158
+ const orderPlugins = arrangePlugins ( plugins )
159
+ expect ( orderPlugins ) . toEqual ( plugins )
160
+ } )
108
161
} )
0 commit comments