@@ -3,12 +3,23 @@ import app from 'app';
3
3
import { createTemplate , getUserTemplates } from 'handler/templates/TemplatesHandler' ;
4
4
import { omit , pick } from 'lodash' ;
5
5
import { createPage , getUserPages } from 'handler/templates/PagesHandler' ;
6
- import type { Page , Template } from 'handler/templates/types' ;
6
+ import type { Page , PageGroup , Template } from 'handler/templates/types' ;
7
+ import { createPageGroup , getUserPageGroups } from 'handler/templates/PageGroupsHandler' ;
8
+ import validateUniqueness from 'handler/widgets/validateUniqueness' ;
9
+ import installFiles from 'handler/widgets/installFiles' ;
10
+ import decompress from 'decompress' ;
11
+ import path from 'path' ;
12
+ import { importWidgetBackend } from 'handler/BackendHandler' ;
7
13
import mockDb from '../mockDb' ;
14
+ import type { PageGroupsSnapshot , PagesSnapshot , TemplatesSnapshot } from '../../routes/Snapshots' ;
8
15
9
16
jest . mock ( 'db/Connection' ) ;
10
17
jest . mock ( 'handler/templates/TemplatesHandler' ) ;
11
18
jest . mock ( 'handler/templates/PagesHandler' ) ;
19
+ jest . mock ( 'handler/templates/PageGroupsHandler' ) ;
20
+ jest . mock ( 'handler/widgets/validateUniqueness' ) ;
21
+ jest . mock ( 'handler/widgets/installFiles' ) ;
22
+ jest . mock ( 'handler/BackendHandler' ) ;
12
23
13
24
describe ( '/snapshots/ua endpoint' , ( ) => {
14
25
const userAppRow = {
@@ -90,21 +101,23 @@ describe('/snapshots/templates endpoint', () => {
90
101
updatedAt : '2022-06-01T18:30:57.450Z'
91
102
} ;
92
103
104
+ const templatesSnapshot : TemplatesSnapshot = [ omit ( template , 'name' , 'custom' ) ] ;
105
+
93
106
it ( 'allows to get snapshot data' , ( ) => {
94
107
( < jest . Mock > getUserTemplates ) . mockReturnValue ( [ template ] ) ;
95
108
96
109
return request ( app )
97
110
. get ( '/console/snapshots/templates' )
98
111
. then ( response => {
99
112
expect ( response . statusCode ) . toBe ( 200 ) ;
100
- expect ( response . body ) . toStrictEqual ( [ omit ( template , 'name' , 'custom' ) ] ) ;
113
+ expect ( response . body ) . toStrictEqual ( templatesSnapshot ) ;
101
114
} ) ;
102
115
} ) ;
103
116
104
117
it ( 'allows to restore snapshot data' , ( ) => {
105
118
return request ( app )
106
119
. post ( '/console/snapshots/templates' )
107
- . send ( [ omit ( template , 'name' , 'custom' ) ] )
120
+ . send ( templatesSnapshot )
108
121
. then ( response => {
109
122
expect ( response . statusCode ) . toBe ( 201 ) ;
110
123
expect ( createTemplate ) . toHaveBeenCalledWith (
@@ -132,21 +145,23 @@ describe('/snapshots/pages endpoint', () => {
132
145
updatedAt : '2022-06-01T18:30:57.450Z'
133
146
} ;
134
147
148
+ const pagesSnapshot : PagesSnapshot = [ omit ( page , 'custom' ) ] ;
149
+
135
150
it ( 'allows to get snapshot data' , ( ) => {
136
151
( < jest . Mock > getUserPages ) . mockReturnValue ( [ page ] ) ;
137
152
138
153
return request ( app )
139
154
. get ( '/console/snapshots/pages' )
140
155
. then ( response => {
141
156
expect ( response . statusCode ) . toBe ( 200 ) ;
142
- expect ( response . body ) . toStrictEqual ( [ omit ( page , 'custom' ) ] ) ;
157
+ expect ( response . body ) . toStrictEqual ( pagesSnapshot ) ;
143
158
} ) ;
144
159
} ) ;
145
160
146
161
it ( 'allows to restore snapshot data' , ( ) => {
147
162
return request ( app )
148
163
. post ( '/console/snapshots/pages' )
149
- . send ( [ omit ( page , 'custom' ) ] )
164
+ . send ( pagesSnapshot )
150
165
. then ( response => {
151
166
expect ( response . statusCode ) . toBe ( 201 ) ;
152
167
expect ( createPage ) . toHaveBeenCalledWith (
@@ -160,3 +175,78 @@ describe('/snapshots/pages endpoint', () => {
160
175
} ) ;
161
176
} ) ;
162
177
} ) ;
178
+
179
+ describe ( '/snapshots/page-groups endpoint' , ( ) => {
180
+ const pageGroup : PageGroup = {
181
+ id : 'test' ,
182
+ name : 'test' ,
183
+ custom : true ,
184
+ pages : [ 'adminDash' ] ,
185
+ icon : 'rocket' ,
186
+ updatedBy : 'user' ,
187
+ updatedAt : '2022-06-01T18:30:57.450Z'
188
+ } ;
189
+
190
+ const pageGroupsSnapshot : PageGroupsSnapshot = [ omit ( pageGroup , 'custom' ) ] ;
191
+
192
+ it ( 'allows to get snapshot data' , ( ) => {
193
+ ( < jest . Mock > getUserPageGroups ) . mockReturnValue ( [ pageGroup ] ) ;
194
+
195
+ return request ( app )
196
+ . get ( '/console/snapshots/page-groups' )
197
+ . then ( response => {
198
+ expect ( response . statusCode ) . toBe ( 200 ) ;
199
+ expect ( response . body ) . toStrictEqual ( pageGroupsSnapshot ) ;
200
+ } ) ;
201
+ } ) ;
202
+
203
+ it ( 'allows to restore snapshot data' , ( ) => {
204
+ return request ( app )
205
+ . post ( '/console/snapshots/page-groups' )
206
+ . send ( pageGroupsSnapshot )
207
+ . then ( response => {
208
+ expect ( response . statusCode ) . toBe ( 201 ) ;
209
+ expect ( createPageGroup ) . toHaveBeenCalledWith (
210
+ expect . objectContaining ( pick ( pageGroup , 'name' , 'pages' , 'icon' ) ) ,
211
+ pageGroup . updatedBy ,
212
+ pageGroup . updatedAt
213
+ ) ;
214
+ } ) ;
215
+ } ) ;
216
+ } ) ;
217
+
218
+ describe ( '/snapshots/widgets endpoint' , ( ) => {
219
+ it ( 'allows to get snapshot data' , ( ) => {
220
+ return request ( app )
221
+ . get ( '/console/snapshots/widgets' )
222
+ . responseType ( 'blob' )
223
+ . then ( response => {
224
+ expect ( response . statusCode ) . toBe ( 200 ) ;
225
+ return decompress ( response . body ) ;
226
+ } ) ;
227
+ } ) ;
228
+
229
+ it ( 'allows to restore snapshot data' , ( ) => {
230
+ ( < jest . Mock > validateUniqueness ) . mockResolvedValue ( null ) ;
231
+ ( < jest . Mock > importWidgetBackend ) . mockResolvedValue ( null ) ;
232
+ ( < jest . Mock > installFiles ) . mockResolvedValue ( null ) ;
233
+ return request ( app )
234
+ . post ( '/console/snapshots/widgets' )
235
+ . attach ( 'snapshot' , path . join ( __dirname , 'fixtures/snapshots/widgets.zip' ) )
236
+ . then ( response => {
237
+ expect ( response . statusCode ) . toBe ( 201 ) ;
238
+ expect ( validateUniqueness ) . toHaveBeenCalledTimes ( 2 ) ;
239
+ expect ( validateUniqueness ) . toHaveBeenCalledWith ( 'testWidget' ) ;
240
+ expect ( validateUniqueness ) . toHaveBeenCalledWith ( 'testWidgetBackend' ) ;
241
+ expect ( installFiles ) . toHaveBeenCalledTimes ( 2 ) ;
242
+ expect ( installFiles ) . toHaveBeenCalledWith ( 'testWidget' , expect . stringMatching ( '/testWidget$' ) ) ;
243
+ expect ( installFiles ) . toHaveBeenCalledWith (
244
+ 'testWidgetBackend' ,
245
+ expect . stringMatching ( '/testWidgetBackend' )
246
+ ) ;
247
+ expect ( importWidgetBackend ) . toHaveBeenCalledTimes ( 2 ) ;
248
+ expect ( importWidgetBackend ) . toHaveBeenCalledWith ( 'testWidget' ) ;
249
+ expect ( importWidgetBackend ) . toHaveBeenCalledWith ( 'testWidgetBackend' ) ;
250
+ } ) ;
251
+ } ) ;
252
+ } ) ;
0 commit comments