4
4
import { installBlockType , uninstallBlockType } from '../actions' ;
5
5
6
6
describe ( 'actions' , ( ) => {
7
- const endpoint = '/wp-json/wp/v2/plugins/block/block' ;
7
+ const pluginEndpoint =
8
+ 'https://example.com/wp-json/wp/v2/plugins/block/block' ;
8
9
const item = {
9
10
id : 'block/block' ,
10
11
name : 'Test Block' ,
11
12
assets : [ 'script.js' ] ,
13
+ links : {
14
+ 'wp:install-plugin' : [
15
+ {
16
+ href :
17
+ 'https://example.com/wp-json/wp/v2/plugins?slug=waves' ,
18
+ } ,
19
+ ] ,
20
+ } ,
12
21
} ;
13
22
const plugin = {
14
23
plugin : 'block/block.php' ,
@@ -18,24 +27,25 @@ describe( 'actions', () => {
18
27
_links : {
19
28
self : [
20
29
{
21
- href : endpoint ,
30
+ href : pluginEndpoint ,
22
31
} ,
23
32
] ,
24
33
} ,
25
34
} ;
26
35
27
36
describe ( 'installBlockType' , ( ) => {
37
+ const block = item ;
28
38
it ( 'should install a block successfully' , ( ) => {
29
- const generator = installBlockType ( item ) ;
39
+ const generator = installBlockType ( block ) ;
30
40
31
41
expect ( generator . next ( ) . value ) . toEqual ( {
32
42
type : 'CLEAR_ERROR_NOTICE' ,
33
- blockId : item . id ,
43
+ blockId : block . id ,
34
44
} ) ;
35
45
36
46
expect ( generator . next ( ) . value ) . toEqual ( {
37
47
type : 'SET_INSTALLING_BLOCK' ,
38
- blockId : item . id ,
48
+ blockId : block . id ,
39
49
isInstalling : true ,
40
50
} ) ;
41
51
@@ -47,15 +57,86 @@ describe( 'actions', () => {
47
57
} ,
48
58
} ) ;
49
59
50
- const itemWithEndpoint = { ...item , endpoint } ;
51
60
expect ( generator . next ( plugin ) . value ) . toEqual ( {
52
61
type : 'ADD_INSTALLED_BLOCK_TYPE' ,
53
- item : itemWithEndpoint ,
62
+ item : {
63
+ ...block ,
64
+ links : {
65
+ ...block . links ,
66
+ self : [
67
+ {
68
+ href : pluginEndpoint ,
69
+ } ,
70
+ ] ,
71
+ } ,
72
+ } ,
73
+ } ) ;
74
+
75
+ expect ( generator . next ( ) . value ) . toEqual ( {
76
+ type : 'LOAD_ASSETS' ,
77
+ assets : block . assets ,
78
+ } ) ;
79
+
80
+ expect ( generator . next ( ) . value ) . toEqual ( {
81
+ args : [ ] ,
82
+ selectorName : 'getBlockTypes' ,
83
+ storeKey : 'core/blocks' ,
84
+ type : 'SELECT' ,
85
+ } ) ;
86
+
87
+ expect ( generator . next ( [ block ] ) . value ) . toEqual ( {
88
+ type : 'SET_INSTALLING_BLOCK' ,
89
+ blockId : block . id ,
90
+ isInstalling : false ,
91
+ } ) ;
92
+
93
+ expect ( generator . next ( ) ) . toEqual ( {
94
+ value : true ,
95
+ done : true ,
96
+ } ) ;
97
+ } ) ;
98
+
99
+ it ( 'should activate an inactive block plugin successfully' , ( ) => {
100
+ const inactiveBlock = {
101
+ ...block ,
102
+ links : {
103
+ ...block . links ,
104
+ 'wp:plugin' : [
105
+ {
106
+ href : pluginEndpoint ,
107
+ } ,
108
+ ] ,
109
+ } ,
110
+ } ;
111
+ const generator = installBlockType ( inactiveBlock ) ;
112
+
113
+ expect ( generator . next ( ) . value ) . toEqual ( {
114
+ type : 'CLEAR_ERROR_NOTICE' ,
115
+ blockId : inactiveBlock . id ,
116
+ } ) ;
117
+
118
+ expect ( generator . next ( ) . value ) . toEqual ( {
119
+ type : 'SET_INSTALLING_BLOCK' ,
120
+ blockId : inactiveBlock . id ,
121
+ isInstalling : true ,
122
+ } ) ;
123
+
124
+ expect ( generator . next ( ) . value ) . toMatchObject ( {
125
+ type : 'API_FETCH' ,
126
+ request : {
127
+ url : pluginEndpoint ,
128
+ method : 'PUT' ,
129
+ } ,
130
+ } ) ;
131
+
132
+ expect ( generator . next ( plugin ) . value ) . toEqual ( {
133
+ type : 'ADD_INSTALLED_BLOCK_TYPE' ,
134
+ item : inactiveBlock ,
54
135
} ) ;
55
136
56
137
expect ( generator . next ( ) . value ) . toEqual ( {
57
138
type : 'LOAD_ASSETS' ,
58
- assets : item . assets ,
139
+ assets : inactiveBlock . assets ,
59
140
} ) ;
60
141
61
142
expect ( generator . next ( ) . value ) . toEqual ( {
@@ -65,9 +146,9 @@ describe( 'actions', () => {
65
146
type : 'SELECT' ,
66
147
} ) ;
67
148
68
- expect ( generator . next ( [ item ] ) . value ) . toEqual ( {
149
+ expect ( generator . next ( [ inactiveBlock ] ) . value ) . toEqual ( {
69
150
type : 'SET_INSTALLING_BLOCK' ,
70
- blockId : item . id ,
151
+ blockId : inactiveBlock . id ,
71
152
isInstalling : false ,
72
153
} ) ;
73
154
@@ -78,21 +159,21 @@ describe( 'actions', () => {
78
159
} ) ;
79
160
80
161
it ( 'should set an error if the plugin has no assets' , ( ) => {
81
- const generator = installBlockType ( { ...item , assets : [ ] } ) ;
162
+ const generator = installBlockType ( { ...block , assets : [ ] } ) ;
82
163
83
164
expect ( generator . next ( ) . value ) . toEqual ( {
84
165
type : 'CLEAR_ERROR_NOTICE' ,
85
- blockId : item . id ,
166
+ blockId : block . id ,
86
167
} ) ;
87
168
88
169
expect ( generator . next ( ) . value ) . toMatchObject ( {
89
170
type : 'SET_ERROR_NOTICE' ,
90
- blockId : item . id ,
171
+ blockId : block . id ,
91
172
} ) ;
92
173
93
174
expect ( generator . next ( ) . value ) . toEqual ( {
94
175
type : 'SET_INSTALLING_BLOCK' ,
95
- blockId : item . id ,
176
+ blockId : block . id ,
96
177
isInstalling : false ,
97
178
} ) ;
98
179
@@ -103,16 +184,16 @@ describe( 'actions', () => {
103
184
} ) ;
104
185
105
186
it ( "should set an error if the plugin can't install" , ( ) => {
106
- const generator = installBlockType ( item ) ;
187
+ const generator = installBlockType ( block ) ;
107
188
108
189
expect ( generator . next ( ) . value ) . toEqual ( {
109
190
type : 'CLEAR_ERROR_NOTICE' ,
110
- blockId : item . id ,
191
+ blockId : block . id ,
111
192
} ) ;
112
193
113
194
expect ( generator . next ( ) . value ) . toEqual ( {
114
195
type : 'SET_INSTALLING_BLOCK' ,
115
- blockId : item . id ,
196
+ blockId : block . id ,
116
197
isInstalling : true ,
117
198
} ) ;
118
199
@@ -131,12 +212,12 @@ describe( 'actions', () => {
131
212
} ;
132
213
expect ( generator . throw ( apiError ) . value ) . toMatchObject ( {
133
214
type : 'SET_ERROR_NOTICE' ,
134
- blockId : item . id ,
215
+ blockId : block . id ,
135
216
} ) ;
136
217
137
218
expect ( generator . next ( ) . value ) . toEqual ( {
138
219
type : 'SET_INSTALLING_BLOCK' ,
139
- blockId : item . id ,
220
+ blockId : block . id ,
140
221
isInstalling : false ,
141
222
} ) ;
142
223
@@ -148,16 +229,26 @@ describe( 'actions', () => {
148
229
} ) ;
149
230
150
231
describe ( 'uninstallBlockType' , ( ) => {
151
- const itemWithEndpoint = { ...item , endpoint } ;
232
+ const block = {
233
+ ...item ,
234
+ links : {
235
+ ...item . links ,
236
+ self : [
237
+ {
238
+ href : pluginEndpoint ,
239
+ } ,
240
+ ] ,
241
+ } ,
242
+ } ;
152
243
153
244
it ( 'should uninstall a block successfully' , ( ) => {
154
- const generator = uninstallBlockType ( itemWithEndpoint ) ;
245
+ const generator = uninstallBlockType ( block ) ;
155
246
156
247
// First the deactivation step
157
248
expect ( generator . next ( ) . value ) . toMatchObject ( {
158
249
type : 'API_FETCH' ,
159
250
request : {
160
- url : endpoint ,
251
+ url : pluginEndpoint ,
161
252
method : 'PUT' ,
162
253
} ,
163
254
} ) ;
@@ -166,14 +257,14 @@ describe( 'actions', () => {
166
257
expect ( generator . next ( ) . value ) . toMatchObject ( {
167
258
type : 'API_FETCH' ,
168
259
request : {
169
- url : endpoint ,
260
+ url : pluginEndpoint ,
170
261
method : 'DELETE' ,
171
262
} ,
172
263
} ) ;
173
264
174
265
expect ( generator . next ( ) . value ) . toEqual ( {
175
266
type : 'REMOVE_INSTALLED_BLOCK_TYPE' ,
176
- item : itemWithEndpoint ,
267
+ item : block ,
177
268
} ) ;
178
269
179
270
expect ( generator . next ( ) ) . toEqual ( {
@@ -183,20 +274,20 @@ describe( 'actions', () => {
183
274
} ) ;
184
275
185
276
it ( "should set a global notice if the plugin can't be deleted" , ( ) => {
186
- const generator = uninstallBlockType ( itemWithEndpoint ) ;
277
+ const generator = uninstallBlockType ( block ) ;
187
278
188
279
expect ( generator . next ( ) . value ) . toMatchObject ( {
189
280
type : 'API_FETCH' ,
190
281
request : {
191
- url : endpoint ,
282
+ url : pluginEndpoint ,
192
283
method : 'PUT' ,
193
284
} ,
194
285
} ) ;
195
286
196
287
expect ( generator . next ( ) . value ) . toMatchObject ( {
197
288
type : 'API_FETCH' ,
198
289
request : {
199
- url : endpoint ,
290
+ url : pluginEndpoint ,
200
291
method : 'DELETE' ,
201
292
} ,
202
293
} ) ;
0 commit comments