/
dot-palette.store.spec.ts
306 lines (266 loc) · 9.88 KB
/
dot-palette.store.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { Observable, of } from 'rxjs';
import { Injectable } from '@angular/core';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { DotContentTypeService, DotESContentService, PaginatorService } from '@dotcms/data-access';
import { DotCMSContentlet, DotCMSContentType, ESContent } from '@dotcms/dotcms-models';
import { DotPaletteStore } from './dot-palette.store';
import { contentTypeDataMock } from '../dot-palette-content-type/dot-palette-content-type.component.spec';
import {
contentletFormDataMock,
contentletProductDataMock
} from '../dot-palette-contentlets/dot-palette-contentlets.component.spec';
const responseData: DotCMSContentType[] = [
{
icon: 'cloud',
id: 'a1661fbc-9e84-4c00-bd62-76d633170da3',
name: 'Widget X',
variable: 'WidgetX',
baseType: 'WIDGET'
},
{
icon: 'alt_route',
id: '799f176a-d32e-4844-a07c-1b5fcd107578',
name: 'Banner',
variable: 'Banner'
},
{
icon: 'cloud',
id: '897cf4a9-171a-4204-accb-c1b498c813fe',
name: 'Contact',
variable: 'Contact'
},
{
icon: 'cloud',
id: 'now-show',
name: 'now-show',
variable: 'persona'
},
{
icon: 'cloud',
id: 'now-show',
name: 'now-show',
variable: 'host'
},
{
icon: 'cloud',
id: 'now-show',
name: 'now-show',
variable: 'vanityurl'
},
{
icon: 'cloud',
id: 'now-show',
name: 'now-show',
variable: 'languagevariable'
}
] as DotCMSContentType[];
@Injectable()
class MockPaginatorService {
url: string;
paginationPerPage = 10;
maxLinksPage = 5;
sortField: string;
sortOrder: string;
totalRecords = 40;
setExtraParams(): void {
/** */
}
public getWithOffset(): Observable<DotCMSContentlet[] | DotCMSContentType[]> {
return null;
}
}
@Injectable()
class MockESPaginatorService {
paginationPerPage = 15;
totalRecords = 20;
public get(): Observable<ESContent> {
return null;
}
}
@Injectable()
class MockContentTypeService {
public getContentTypes(): Observable<ESContent> {
return null;
}
public filterContentTypes(): Observable<ESContent> {
return null;
}
}
describe('DotPaletteStore', () => {
let dotPaletteStore: DotPaletteStore;
let paginatorService: PaginatorService;
let dotContentTypeService: DotContentTypeService;
let dotESContentService: DotESContentService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
DotPaletteStore,
{ provide: PaginatorService, useClass: MockPaginatorService },
{ provide: DotContentTypeService, useClass: MockContentTypeService },
{ provide: DotESContentService, useClass: MockESPaginatorService }
]
});
dotPaletteStore = TestBed.inject(DotPaletteStore);
paginatorService = TestBed.inject(PaginatorService);
dotContentTypeService = TestBed.inject(DotContentTypeService);
dotESContentService = TestBed.inject(DotESContentService);
});
// Updaters
it('should update filter', () => {
dotPaletteStore.setFilter('test');
dotPaletteStore.state$.subscribe((data) => {
expect(data.filter).toEqual('test');
});
});
it('should update languageId', () => {
dotPaletteStore.setLanguageId('1');
dotPaletteStore.state$.subscribe((data) => {
expect(data.languageId).toEqual('1');
});
});
it('should update viewContentlet', () => {
dotPaletteStore.setViewContentlet('in');
dotPaletteStore.state$.subscribe((data) => {
expect(data.viewContentlet).toEqual('in');
});
});
it('should update setLoading', () => {
dotPaletteStore.setLoading();
dotPaletteStore.state$.subscribe((data) => {
expect(data.loading).toEqual(true);
});
});
it('should update setLoaded', () => {
dotPaletteStore.setLoaded();
dotPaletteStore.state$.subscribe((data) => {
expect(data.loading).toEqual(false);
});
});
it('should update allowdContent', () => {
const allowedContent = ['banner', 'contact', 'block editor'];
dotPaletteStore.setAllowedContent(allowedContent);
dotPaletteStore.state$.subscribe((data) => {
expect(data.allowedContent).toEqual(allowedContent);
});
});
// Effects
it('should load contentTypes to store', (done) => {
const sortedDataMock = contentTypeDataMock.sort((a, b) => a.name.localeCompare(b.name));
spyOn(dotContentTypeService, 'filterContentTypes').and.returnValues(
of(sortedDataMock as DotCMSContentType[])
);
spyOn(dotContentTypeService, 'getContentTypes').and.returnValues(of([]));
dotPaletteStore.loadContentTypes(['blog', 'banner']);
dotPaletteStore.vm$.subscribe((data) => {
expect(data.contentTypes).toEqual(sortedDataMock as DotCMSContentType[]);
done();
});
});
it('should load inly widgets to store if allowedContent is empty', (done) => {
const sortedDataMock = contentTypeDataMock.sort((a, b) => a.name.localeCompare(b.name));
spyOn(dotContentTypeService, 'filterContentTypes').and.returnValues(of([]));
spyOn(dotContentTypeService, 'getContentTypes').and.returnValues(
of(sortedDataMock as DotCMSContentType[])
);
dotPaletteStore.loadContentTypes([]);
dotPaletteStore.vm$.subscribe((data) => {
expect(data.contentTypes).toEqual(sortedDataMock as DotCMSContentType[]);
done();
});
expect(dotContentTypeService.filterContentTypes).not.toHaveBeenCalled();
expect(dotContentTypeService.getContentTypes).toHaveBeenCalled();
});
it('should load Forms contentlets to store', (done) => {
spyOn(paginatorService, 'getWithOffset').and.returnValue(of([contentletFormDataMock]));
dotPaletteStore.loadContentlets('forms');
expect(paginatorService.url).toBe('v1/contenttype');
expect(paginatorService.paginationPerPage).toBe(25);
expect(paginatorService.sortField).toBe('modDate');
expect(paginatorService.sortOrder).toBe(1);
dotPaletteStore.vm$.subscribe((data) => {
expect(data.contentlets).toEqual([
contentletFormDataMock
] as unknown as DotCMSContentType[]);
expect(data.filter).toEqual('');
expect(data.loading).toEqual(false);
expect(data.totalRecords).toEqual(paginatorService.totalRecords);
done();
});
});
it('should load Product contentlets to store', (done) => {
spyOn(dotESContentService, 'get').and.returnValue(
of({
contentTook: 0,
jsonObjectView: {
contentlets: [contentletProductDataMock] as unknown as DotCMSContentlet[]
},
queryTook: 1,
resultsSize: 20
})
);
dotPaletteStore.loadContentlets('product');
dotPaletteStore.vm$.subscribe((data) => {
expect(dotESContentService.get).toHaveBeenCalledWith({
itemsPerPage: 25,
lang: '1',
filter: '',
offset: '0',
query: '+contentType: product +deleted: false'
});
expect(data.contentlets).toEqual([
contentletProductDataMock
] as unknown as DotCMSContentlet[]);
expect(data.filter).toEqual('');
expect(data.loading).toEqual(false);
expect(data.totalRecords).toEqual(20);
done();
});
});
it('should set filter value in store', (done) => {
spyOn(dotESContentService, 'get').and.returnValue(
of({
contentTook: 0,
jsonObjectView: {
contentlets: [contentletProductDataMock] as unknown as DotCMSContentlet[]
},
queryTook: 1,
resultsSize: 20
})
);
dotPaletteStore.filterContentlets('Prod');
dotPaletteStore.vm$.subscribe((data) => {
expect(data.filter).toEqual('Prod');
done();
});
});
it('should filter contenttypes in stores', fakeAsync(() => {
spyOn(dotContentTypeService, 'filterContentTypes').and.returnValue(of(responseData));
spyOn(dotContentTypeService, 'getContentTypes').and.returnValue(of(responseData));
const allowedContent = ['banner', 'blog'];
const filter = 'blog';
dotPaletteStore.setAllowedContent(allowedContent);
dotPaletteStore.filterContentTypes(filter);
tick(400);
dotPaletteStore.vm$.subscribe((data) => expect(data.filter).toEqual(filter));
expect(dotContentTypeService.filterContentTypes).toHaveBeenCalledWith(
filter,
allowedContent.join(',')
);
expect(dotContentTypeService.getContentTypes).toHaveBeenCalledWith({
filter,
page: 40,
type: 'WIDGET'
});
}));
it('should not call filterContentTypes is filter values es shoter than 3 caracteres', fakeAsync(() => {
spyOn(dotContentTypeService, 'filterContentTypes').and.returnValue(of(responseData));
spyOn(dotContentTypeService, 'getContentTypes').and.returnValue(of(responseData));
const allowedContent = ['banner', 'blog'];
const filter = 'bo';
dotPaletteStore.setAllowedContent(allowedContent);
dotPaletteStore.filterContentTypes(filter);
tick(400);
expect(dotContentTypeService.filterContentTypes).not.toHaveBeenCalled();
expect(dotContentTypeService.getContentTypes).not.toHaveBeenCalled();
}));
});