1 import { Modules } from "@medusajs/utils"
2 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
3 import { SettingsTypes } from "@medusajs/types"
4
5 jest.setTimeout(30000)
6
7 moduleIntegrationTestRunner<SettingsTypes.ISettingsModuleService>({
8 moduleName: Modules.SETTINGS,
9 testSuite: ({ service }) => {
10 describe("SettingsModuleService", function () {
11 describe("ViewConfiguration", function () {
12 it("should create a view configuration", async () => {
13 const viewConfig = await service.createViewConfigurations({
14 entity: "orders",
15 name: "My Orders View",
16 user_id: "user_123",
17 configuration: {
18 visible_columns: ["id", "status", "created_at"],
19 column_order: ["id", "status", "created_at"],
20 column_widths: { id: 100, status: 150 },
21 filters: { status: ["pending", "completed"] },
22 sorting: { id: "created_at", desc: true },
23 search: "",
24 },
25 })
26
27 expect(viewConfig).toEqual(
28 expect.objectContaining({
29 id: expect.any(String),
30 entity: "orders",
31 name: "My Orders View",
32 user_id: "user_123",
33 })
34 )
35 })
36
37 it("should update a view configuration and remove filters", async () => {
38 // Create a view with filters
39 const viewConfig = await service.createViewConfigurations({
40 entity: "products",
41 name: "Filtered Products View",
42 user_id: "user_456",
43 configuration: {
44 visible_columns: ["id", "title", "status"],
45 column_order: ["id", "title", "status"],
46 filters: {
47 status: ["draft", "published"],
48 collection_id: ["col_123", "col_456"],
49 },
50 sorting: { id: "created_at", desc: true },
51 },
52 })
53
54 expect(viewConfig.configuration.filters).toEqual({
55 status: ["draft", "published"],
56 collection_id: ["col_123", "col_456"],
57 })
58
59 // Update the view to remove filters
60 const updatedConfig = await service.updateViewConfigurations(
61 viewConfig.id,
62 {
63 configuration: {
64 visible_columns: ["id", "title", "status"],
65 column_order: ["id", "title", "status"],
66 filters: {}, // Empty filters object
67 sorting: { id: "created_at", desc: true },
68 },
69 }
70 )
71
72 expect(updatedConfig.configuration.filters).toEqual({})
73
74 // Retrieve the view again to ensure filters were persisted as empty
75 const retrievedConfig = await service.retrieveViewConfiguration(
76 viewConfig.id
77 )
78
79 expect(retrievedConfig.configuration.filters).toEqual({})
80 })
81
82 it("should update view configuration with partial configuration updates", async () => {
83 // Create a view with full configuration
84 const viewConfig = await service.createViewConfigurations({
85 entity: "customers",
86 name: "Customer View",
87 user_id: "user_789",
88 configuration: {
89 visible_columns: ["id", "name", "email"],
90 column_order: ["id", "name", "email"],
91 filters: {
92 has_account: true,
93 groups: ["vip", "regular"],
94 },
95 sorting: { id: "created_at", desc: false },
96 search: "test search",
97 },
98 })
99
100 // Update only filters (should preserve other configuration)
101 const updatedConfig = await service.updateViewConfigurations(
102 viewConfig.id,
103 {
104 configuration: {
105 visible_columns: ["id", "name", "email"],
106 column_order: ["id", "name", "email"],
107 filters: { has_account: false }, // Changed filters
108 sorting: { id: "created_at", desc: false },
109 search: "test search",
110 },
111 }
112 )
113
114 expect(updatedConfig.configuration).toEqual({
115 visible_columns: ["id", "name", "email"],
116 column_order: ["id", "name", "email"],
117 filters: { has_account: false },
118 sorting: { id: "created_at", desc: false },
119 search: "test search",
120 column_widths: {}, // Default value when not provided
121 })
122 })
123
124 it("should update only the name field without affecting configuration", async () => {
125 // Create a view with full configuration
126 const viewConfig = await service.createViewConfigurations({
127 entity: "orders",
128 name: "Original Name",
129 user_id: "user_123",
130 configuration: {
131 visible_columns: ["id", "status", "total"],
132 column_order: ["id", "status", "total"],
133 column_widths: { id: 100, status: 150, total: 200 },
134 filters: { status: ["pending", "completed"] },
135 sorting: { id: "created_at", desc: true },
136 search: "test search",
137 },
138 })
139
140 // Update only the name field
141 const updatedConfig = await service.updateViewConfigurations(
142 viewConfig.id,
143 { name: "Updated Name" }
144 )
145
146 expect(updatedConfig.name).toBe("Updated Name")
147 expect(updatedConfig.configuration).toEqual(viewConfig.configuration)
148 })
149
150 it("should completely replace filters when updating configuration", async () => {
151 // Create a view with complex filters
152 const viewConfig = await service.createViewConfigurations({
153 entity: "products",
154 name: "Product View",
155 user_id: "user_123",
156 configuration: {
157 visible_columns: ["id", "title"],
158 column_order: ["id", "title"],
159 filters: {
160 status: ["draft", "published"],
161 collection_id: ["col_123", "col_456"],
162 price_range: { min: 10, max: 100 },
163 },
164 sorting: { id: "created_at", desc: true },
165 },
166 })
167
168 // Update with new filters
169 const updatedConfig = await service.updateViewConfigurations(
170 viewConfig.id,
171 {
172 configuration: {
173 visible_columns: ["id", "title"],
174 column_order: ["id", "title"],
175 filters: { category: ["electronics"] }, // Completely different filters
176 sorting: { id: "created_at", desc: true },
177 },
178 }
179 )
180
181 expect(updatedConfig.configuration.filters).toEqual({
182 category: ["electronics"],
183 })
184 // Verify old filters are gone
185 expect(updatedConfig.configuration.filters?.status).toBeUndefined()
186 expect(updatedConfig.configuration.filters?.collection_id).toBeUndefined()
187 expect(updatedConfig.configuration.filters?.price_range).toBeUndefined()
188 })
189
190 it("should remove filters when explicitly set to empty object", async () => {
191 // Create a view with filters
192 const viewConfig = await service.createViewConfigurations({
193 entity: "customers",
194 name: "Customer View",
195 user_id: "user_123",
196 configuration: {
197 visible_columns: ["id", "name", "email"],
198 column_order: ["id", "name", "email"],
199 filters: {
200 has_account: true,
201 groups: ["vip", "regular"],
202 },
203 sorting: { id: "created_at", desc: false },
204 },
205 })
206
207 // Update configuration with empty filters
208 const updatedConfig = await service.updateViewConfigurations(
209 viewConfig.id,
210 {
211 configuration: {
212 visible_columns: ["id", "name", "email"],
213 column_order: ["id", "name", "email"],
214 filters: {}, // Empty filters
215 sorting: { id: "created_at", desc: false },
216 },
217 }
218 )
219
220 expect(updatedConfig.configuration.filters).toEqual({})
221 })
222
223 it("should preserve other configuration properties when updating specific ones", async () => {
224 // Create a view with full configuration
225 const viewConfig = await service.createViewConfigurations({
226 entity: "inventory",
227 name: "Inventory View",
228 user_id: "user_123",
229 configuration: {
230 visible_columns: ["id", "sku", "quantity"],
231 column_order: ["id", "sku", "quantity"],
232 column_widths: { id: 80, sku: 120, quantity: 100 },
233 filters: { location: ["warehouse_1"] },
234 sorting: { id: "sku", desc: false },
235 search: "original search",
236 },
237 })
238
239 // Update only filters and sorting
240 const updatedConfig = await service.updateViewConfigurations(
241 viewConfig.id,
242 {
243 configuration: {
244 visible_columns: viewConfig.configuration.visible_columns,
245 column_order: viewConfig.configuration.column_order,
246 filters: { location: ["warehouse_2"] },
247 sorting: { id: "quantity", desc: true },
248 // Note: not providing column_widths and search
249 },
250 }
251 )
252
253 expect(updatedConfig.configuration.filters).toEqual({
254 location: ["warehouse_2"],
255 })
256 expect(updatedConfig.configuration.sorting).toEqual({
257 id: "quantity",
258 desc: true,
259 })
260 // These should be preserved
261 expect(updatedConfig.configuration.visible_columns).toEqual(
262 viewConfig.configuration.visible_columns
263 )
264 expect(updatedConfig.configuration.column_order).toEqual(
265 viewConfig.configuration.column_order
266 )
267 // These should have default values since not provided
268 expect(updatedConfig.configuration.column_widths).toEqual({})
269 expect(updatedConfig.configuration.search).toBe("")
270 })
271
272 it("should handle missing configuration properties with defaults", async () => {
273 // Create a view with full configuration
274 const viewConfig = await service.createViewConfigurations({
275 entity: "payments",
276 name: "Payment View",
277 user_id: "user_123",
278 configuration: {
279 visible_columns: ["id", "amount", "status"],
280 column_order: ["id", "amount", "status"],
281 column_widths: { id: 100, amount: 150, status: 120 },
282 filters: { status: ["completed"] },
283 sorting: { id: "created_at", desc: true },
284 search: "payment search",
285 },
286 })
287
288 // Update with partial configuration (only visible_columns)
289 const updatedConfig = await service.updateViewConfigurations(
290 viewConfig.id,
291 {
292 configuration: {
293 visible_columns: ["id", "amount"],
294 column_order: ["id", "amount"],
295 // Not providing filters, sorting, search, column_widths
296 },
297 }
298 )
299
300 expect(updatedConfig.configuration).toEqual({
301 visible_columns: ["id", "amount"],
302 column_order: ["id", "amount"],
303 column_widths: {}, // Default
304 filters: {}, // Default
305 sorting: null, // Default
306 search: "", // Default
307 })
308 })
309
310 it("should update multiple view configurations when using selector", async () => {
311 // Create multiple views for the same entity
312 const view1 = await service.createViewConfigurations({
313 entity: "orders",
314 name: "Orders View 1",
315 user_id: "user_123",
316 configuration: {
317 visible_columns: ["id", "status"],
318 column_order: ["id", "status"],
319 filters: { status: ["pending"] },
320 },
321 })
322
323 const view2 = await service.createViewConfigurations({
324 entity: "orders",
325 name: "Orders View 2",
326 user_id: "user_456",
327 configuration: {
328 visible_columns: ["id", "total"],
329 column_order: ["id", "total"],
330 filters: { status: ["completed"] },
331 },
332 })
333
334 const view3 = await service.createViewConfigurations({
335 entity: "products", // Different entity
336 name: "Products View",
337 user_id: "user_123",
338 configuration: {
339 visible_columns: ["id", "title"],
340 column_order: ["id", "title"],
341 },
342 })
343
344 // Update using selector for entity "orders"
345 const updatedConfigs = await service.updateViewConfigurations(
346 { entity: "orders" },
347 {
348 configuration: {
349 visible_columns: ["id", "status", "total"],
350 column_order: ["id", "status", "total"],
351 filters: {},
352 },
353 }
354 )
355
356 // Should return an array
357 expect(Array.isArray(updatedConfigs)).toBe(true)
358 expect(updatedConfigs).toHaveLength(2)
359
360 // Both orders views should be updated
361 const updatedIds = updatedConfigs.map((v) => v.id).sort()
362 expect(updatedIds).toEqual([view1.id, view2.id].sort())
363
364 // All should have the new configuration
365 updatedConfigs.forEach((config) => {
366 expect(config.configuration.visible_columns).toEqual([
367 "id",
368 "status",
369 "total",
370 ])
371 expect(config.configuration.filters).toEqual({})
372 })
373
374 // Products view should not be affected
375 const productView = await service.retrieveViewConfiguration(view3.id)
376 expect(productView.configuration.visible_columns).toEqual(["id", "title"])
377 })
378
379 it("should return empty array when no views match selector", async () => {
380 // Try to update with a selector that matches no views
381 const result = await service.updateViewConfigurations(
382 { entity: "non_existent_entity" },
383 { name: "New Name" }
384 )
385
386 expect(Array.isArray(result)).toBe(true)
387 expect(result).toHaveLength(0)
388 })
389
390 it("should handle null values in configuration", async () => {
391 // Create a view with sorting
392 const viewConfig = await service.createViewConfigurations({
393 entity: "shipping",
394 name: "Shipping View",
395 user_id: "user_123",
396 configuration: {
397 visible_columns: ["id", "carrier", "tracking"],
398 column_order: ["id", "carrier", "tracking"],
399 sorting: { id: "created_at", desc: true },
400 search: "fedex",
401 },
402 })
403
404 // Update with sorting: null
405 const updatedConfig = await service.updateViewConfigurations(
406 viewConfig.id,
407 {
408 configuration: {
409 visible_columns: viewConfig.configuration.visible_columns,
410 column_order: viewConfig.configuration.column_order,
411 sorting: null,
412 search: "", // Also test empty string
413 },
414 }
415 )
416
417 expect(updatedConfig.configuration.sorting).toBeNull()
418 expect(updatedConfig.configuration.search).toBe("")
419 })
420 })
421
422 describe("LayoutConfiguration", function () {
423 it("should create a user layout configuration via setLayoutConfiguration", async () => {
424 const result = await service.setLayoutConfiguration(
425 "product.details",
426 "user_123",
427 {
428 widgets: {
429 "widget-general": { hidden: false, section: "main", order: 0 },
430 "widget-pricing": { hidden: true },
431 },
432 }
433 )
434
435 expect(result).toEqual(
436 expect.objectContaining({
437 id: expect.any(String),
438 zone: "product.details",
439 user_id: "user_123",
440 is_system_default: false,
441 configuration: {
442 widgets: {
443 "widget-general": {
444 hidden: false,
445 section: "main",
446 order: 0,
447 },
448 "widget-pricing": { hidden: true },
449 },
450 },
451 })
452 )
453 })
454
455 it("should upsert user layout configuration replacing widgets wholesale", async () => {
456 await service.setLayoutConfiguration("product.details", "user_123", {
457 widgets: {
458 "widget-general": { hidden: false },
459 "widget-pricing": { hidden: true },
460 "widget-media": { order: 2 },
461 },
462 })
463
464 // Second call should replace, not merge
465 const updated = await service.setLayoutConfiguration(
466 "product.details",
467 "user_123",
468 {
469 widgets: {
470 "widget-general": { hidden: true },
471 // widget-pricing and widget-media intentionally omitted
472 },
473 }
474 )
475
476 expect(updated.configuration.widgets).toEqual({
477 "widget-general": { hidden: true },
478 })
479 // Removed widgets must not survive the replacement
480 expect(
481 updated.configuration.widgets["widget-pricing"]
482 ).toBeUndefined()
483 expect(updated.configuration.widgets["widget-media"]).toBeUndefined()
484 })
485
486 it("should keep one record per user per zone after multiple upserts", async () => {
487 await service.setLayoutConfiguration("order.details", "user_abc", {
488 widgets: { "widget-a": { hidden: false } },
489 })
490 await service.setLayoutConfiguration("order.details", "user_abc", {
491 widgets: { "widget-b": { hidden: true } },
492 })
493
494 const configs = await service.listLayoutConfigurations({
495 zone: "order.details",
496 user_id: "user_abc",
497 })
498
499 expect(configs).toHaveLength(1)
500 expect(configs[0].configuration.widgets).toEqual({
501 "widget-b": { hidden: true },
502 })
503 })
504
505 it("should create a system default layout configuration", async () => {
506 const result = await service.setSystemDefaultLayoutConfiguration(
507 "customer.details",
508 {
509 widgets: {
510 "widget-info": { hidden: false, order: 0 },
511 },
512 }
513 )
514
515 expect(result).toEqual(
516 expect.objectContaining({
517 id: expect.any(String),
518 zone: "customer.details",
519 user_id: null,
520 is_system_default: true,
521 configuration: {
522 widgets: {
523 "widget-info": { hidden: false, order: 0 },
524 },
525 },
526 })
527 )
528 })
529
530 it("should upsert system default layout configuration replacing widgets wholesale", async () => {
531 await service.setSystemDefaultLayoutConfiguration("order.list", {
532 widgets: {
533 "widget-filters": { hidden: false },
534 "widget-table": { order: 1 },
535 },
536 })
537
538 const updated = await service.setSystemDefaultLayoutConfiguration(
539 "order.list",
540 {
541 widgets: {
542 "widget-filters": { hidden: true },
543 // widget-table intentionally omitted
544 },
545 }
546 )
547
548 expect(updated.configuration.widgets).toEqual({
549 "widget-filters": { hidden: true },
550 })
551 expect(updated.configuration.widgets["widget-table"]).toBeUndefined()
552
553 // Still only one system default record
554 const configs = await service.listLayoutConfigurations({
555 zone: "order.list",
556 is_system_default: true,
557 })
558 expect(configs).toHaveLength(1)
559 })
560
561 it("should return null from getSystemDefaultLayoutConfiguration when none exists", async () => {
562 const result = await service.getSystemDefaultLayoutConfiguration(
563 "zone.without.default"
564 )
565
566 expect(result).toBeNull()
567 })
568
569 it("should return the system default from getSystemDefaultLayoutConfiguration", async () => {
570 await service.setSystemDefaultLayoutConfiguration("product.list", {
571 widgets: { "widget-header": { order: 0 } },
572 })
573
574 const result =
575 await service.getSystemDefaultLayoutConfiguration("product.list")
576
577 expect(result).not.toBeNull()
578 expect(result!.zone).toBe("product.list")
579 expect(result!.is_system_default).toBe(true)
580 expect(result!.configuration.widgets).toEqual({
581 "widget-header": { order: 0 },
582 })
583 })
584
585 it("should clear user layout configuration", async () => {
586 await service.setLayoutConfiguration("product.details", "user_del", {
587 widgets: { "widget-x": { hidden: false } },
588 })
589
590 await service.clearLayoutConfiguration("product.details", "user_del")
591
592 const configs = await service.listLayoutConfigurations({
593 zone: "product.details",
594 user_id: "user_del",
595 })
596 expect(configs).toHaveLength(0)
597 })
598
599 it("should no-op clearLayoutConfiguration when no config exists", async () => {
600 await expect(
601 service.clearLayoutConfiguration(
602 "zone.nothing.here",
603 "user_nonexistent"
604 )
605 ).resolves.not.toThrow()
606 })
607
608 it("should not affect system default when clearing user config", async () => {
609 await service.setSystemDefaultLayoutConfiguration("product.details", {
610 widgets: { "widget-default": { order: 0 } },
611 })
612 await service.setLayoutConfiguration("product.details", "user_999", {
613 widgets: { "widget-personal": { hidden: false } },
614 })
615
616 await service.clearLayoutConfiguration("product.details", "user_999")
617
618 const systemDefault =
619 await service.getSystemDefaultLayoutConfiguration("product.details")
620 expect(systemDefault).not.toBeNull()
621 expect(systemDefault!.configuration.widgets).toEqual({
622 "widget-default": { order: 0 },
623 })
624 })
625
626 it("should isolate layout configurations across zones", async () => {
627 await service.setLayoutConfiguration("zone.a", "user_123", {
628 widgets: { "widget-a1": { hidden: false } },
629 })
630 await service.setLayoutConfiguration("zone.b", "user_123", {
631 widgets: { "widget-b1": { hidden: true } },
632 })
633
634 const zoneA = await service.listLayoutConfigurations({
635 zone: "zone.a",
636 user_id: "user_123",
637 })
638 const zoneB = await service.listLayoutConfigurations({
639 zone: "zone.b",
640 user_id: "user_123",
641 })
642
643 expect(zoneA).toHaveLength(1)
644 expect(zoneA[0].configuration.widgets["widget-a1"]).toBeDefined()
645 expect(zoneB).toHaveLength(1)
646 expect(zoneB[0].configuration.widgets["widget-b1"]).toBeDefined()
647 })
648
649 it("should isolate layout configurations across users", async () => {
650 await service.setLayoutConfiguration("shared.zone", "user_A", {
651 widgets: { "widget-x": { hidden: false } },
652 })
653 await service.setLayoutConfiguration("shared.zone", "user_B", {
654 widgets: { "widget-x": { hidden: true } },
655 })
656
657 const userAConfigs = await service.listLayoutConfigurations({
658 zone: "shared.zone",
659 user_id: "user_A",
660 })
661 const userBConfigs = await service.listLayoutConfigurations({
662 zone: "shared.zone",
663 user_id: "user_B",
664 })
665
666 expect(userAConfigs[0].configuration.widgets["widget-x"]).toEqual({
667 hidden: false,
668 })
669 expect(userBConfigs[0].configuration.widgets["widget-x"]).toEqual({
670 hidden: true,
671 })
672 })
673
674 it("should handle empty widgets object", async () => {
675 const result = await service.setLayoutConfiguration(
676 "product.details",
677 "user_empty",
678 { widgets: {} }
679 )
680
681 expect(result.configuration.widgets).toEqual({})
682 })
683
684 describe("getActiveLayoutScope / setActiveLayoutScope", function () {
685 it("should return null when no scope preference exists", async () => {
686 const scope = await service.getActiveLayoutScope(
687 "product.details",
688 "user_new"
689 )
690
691 expect(scope).toBeNull()
692 })
693
694 it("should set and retrieve personal scope", async () => {
695 await service.setActiveLayoutScope(
696 "product.details",
697 "user_123",
698 "personal"
699 )
700
701 const scope = await service.getActiveLayoutScope(
702 "product.details",
703 "user_123"
704 )
705
706 expect(scope).toBe("personal")
707 })
708
709 it("should set and retrieve default scope", async () => {
710 await service.setActiveLayoutScope(
711 "order.details",
712 "user_456",
713 "default"
714 )
715
716 const scope = await service.getActiveLayoutScope(
717 "order.details",
718 "user_456"
719 )
720
721 expect(scope).toBe("default")
722 })
723
724 it("should update scope from personal to default", async () => {
725 await service.setActiveLayoutScope(
726 "product.details",
727 "user_789",
728 "personal"
729 )
730 await service.setActiveLayoutScope(
731 "product.details",
732 "user_789",
733 "default"
734 )
735
736 const scope = await service.getActiveLayoutScope(
737 "product.details",
738 "user_789"
739 )
740
741 expect(scope).toBe("default")
742 })
743
744 it("should clear scope by setting it to null", async () => {
745 await service.setActiveLayoutScope(
746 "product.details",
747 "user_321",
748 "personal"
749 )
750 await service.setActiveLayoutScope(
751 "product.details",
752 "user_321",
753 null
754 )
755
756 const scope = await service.getActiveLayoutScope(
757 "product.details",
758 "user_321"
759 )
760
761 expect(scope).toBeNull()
762 })
763
764 it("should isolate scope preferences across zones", async () => {
765 await service.setActiveLayoutScope(
766 "zone.a",
767 "user_iso",
768 "personal"
769 )
770 await service.setActiveLayoutScope("zone.b", "user_iso", "default")
771
772 const scopeA = await service.getActiveLayoutScope(
773 "zone.a",
774 "user_iso"
775 )
776 const scopeB = await service.getActiveLayoutScope(
777 "zone.b",
778 "user_iso"
779 )
780
781 expect(scopeA).toBe("personal")
782 expect(scopeB).toBe("default")
783 })
784
785 it("should isolate scope preferences across users", async () => {
786 await service.setActiveLayoutScope(
787 "shared.zone",
788 "user_X",
789 "personal"
790 )
791 await service.setActiveLayoutScope(
792 "shared.zone",
793 "user_Y",
794 "default"
795 )
796
797 const scopeX = await service.getActiveLayoutScope(
798 "shared.zone",
799 "user_X"
800 )
801 const scopeY = await service.getActiveLayoutScope(
802 "shared.zone",
803 "user_Y"
804 )
805
806 expect(scopeX).toBe("personal")
807 expect(scopeY).toBe("default")
808 })
809 })
810 })
811 })
812 },
813 })