From b54e42cce20fc7181ec492a3d1adb46641f79a51 Mon Sep 17 00:00:00 2001 From: AleksanderSklorz <115619721+AleksanderSklorz@users.noreply.github.com> Date: Fri, 28 Nov 2025 10:06:18 +0100 Subject: [PATCH] [MNT-25276] sorting using sorting key configuration is not correctly saved in localstorage (#4905) * [MNT-25276] Fixed sorting when column key is different than sortingKey * [MNT-25276] Unit tests --- .../document-list.directive.spec.ts | 64 +++++++++++++++++-- .../lib/directives/document-list.directive.ts | 3 +- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/projects/aca-content/src/lib/directives/document-list.directive.spec.ts b/projects/aca-content/src/lib/directives/document-list.directive.spec.ts index da456d726..eeb12ac66 100644 --- a/projects/aca-content/src/lib/directives/document-list.directive.spec.ts +++ b/projects/aca-content/src/lib/directives/document-list.directive.spec.ts @@ -30,6 +30,8 @@ import { TestBed } from '@angular/core/testing'; describe('DocumentListDirective', () => { let documentListDirective: DocumentListDirective; + const preferenceKey = 'files'; + const sortingKey = 'sortingKey'; const documentListMock: any = { currentFolderId: '', stickyHeader: false, @@ -159,7 +161,7 @@ describe('DocumentListDirective', () => { it('should set user preferences for columns visibility`', () => { const event = new CustomEvent('columnsVisibilityChanged', { detail: { 'app.tags': true, 'app.name': false } }); - mockRoute.snapshot.data.sortingPreferenceKey = 'files'; + mockRoute.snapshot.data.sortingPreferenceKey = preferenceKey; documentListDirective.ngOnInit(); documentListDirective.onColumnsVisibilityChange(event); @@ -168,7 +170,7 @@ describe('DocumentListDirective', () => { it('should set user preferences for columns order`', () => { const event = new CustomEvent('columnsOrderChanged', { detail: ['app.tags', 'app.name'] }); - mockRoute.snapshot.data.sortingPreferenceKey = 'files'; + mockRoute.snapshot.data.sortingPreferenceKey = preferenceKey; documentListDirective.ngOnInit(); documentListDirective.onColumnOrderChanged(event); @@ -177,7 +179,7 @@ describe('DocumentListDirective', () => { it('should set user preferences for columns width`', () => { const event = new CustomEvent('columnsWidthChanged', { detail: { 'app.tags': 65, 'app.name': 75 } }); - mockRoute.snapshot.data.sortingPreferenceKey = 'files'; + mockRoute.snapshot.data.sortingPreferenceKey = preferenceKey; documentListDirective.ngOnInit(); documentListDirective.onColumnsWidthChanged(event); @@ -185,7 +187,7 @@ describe('DocumentListDirective', () => { }); it('should set document list properties from user preferences`', () => { - mockRoute.snapshot.data.sortingPreferenceKey = 'files'; + mockRoute.snapshot.data.sortingPreferenceKey = preferenceKey; userPreferencesServiceMock.hasItem.and.returnValue(true); userPreferencesServiceMock.get.and.returnValue(false); userPreferencesServiceMock.get.withArgs('files.columns.width').and.returnValue(JSON.stringify({ 'app.tag': 87 })); @@ -197,4 +199,58 @@ describe('DocumentListDirective', () => { expect(documentListMock.setColumnsOrder).toEqual(['app.tag', 'app.name']); expect(documentListMock.setColumnsVisibility).toEqual({ 'app.tag': true }); }); + + it('should set sorting using sortingKey from preferences if sortingKey exists in preferences', () => { + mockRoute.snapshot.data.sortingPreferenceKey = preferenceKey; + documentListMock.sortingMode = 'server'; + userPreferencesServiceMock.get.and.callThrough().withArgs(`${preferenceKey}.sorting.sortingKey`, null).and.returnValue(sortingKey); + + documentListDirective.ngOnInit(); + expect(documentListMock.sorting).toEqual([sortingKey, undefined]); + expect(documentListMock.data.setSorting).toHaveBeenCalledWith({ + key: sortingKey, + direction: undefined + }); + }); + + it('should set sorting using key from preferences when sortingKey is missing in preferences', () => { + mockRoute.snapshot.data.sortingPreferenceKey = preferenceKey; + documentListMock.sortingMode = 'server'; + userPreferencesServiceMock.get.and.callThrough().withArgs(`${preferenceKey}.sorting.key`, null).and.returnValue(sortingKey); + + documentListDirective.ngOnInit(); + expect(documentListMock.sorting).toEqual([sortingKey, undefined]); + expect(documentListMock.data.setSorting).toHaveBeenCalledWith({ + key: sortingKey, + direction: undefined + }); + }); + + describe('onReady', () => { + beforeEach(() => { + mockRoute.snapshot.data.sortingPreferenceKey = preferenceKey; + }); + + it('should set sorting using sortingKey from preferences if sortingKey exists in preferences', () => { + userPreferencesServiceMock.get.and.callThrough().withArgs(`${preferenceKey}.sorting.sortingKey`, null).and.returnValue(sortingKey); + + documentListDirective.onReady(); + expect(documentListMock.sorting).toEqual([sortingKey, undefined]); + expect(documentListMock.data.setSorting).toHaveBeenCalledWith({ + key: sortingKey, + direction: undefined + }); + }); + + it('should set sorting using key from preferences when sortingKey is missing in preferences', () => { + userPreferencesServiceMock.get.and.callThrough().withArgs(`${preferenceKey}.sorting.key`, null).and.returnValue(sortingKey); + + documentListDirective.onReady(); + expect(documentListMock.sorting).toEqual([sortingKey, undefined]); + expect(documentListMock.data.setSorting).toHaveBeenCalledWith({ + key: sortingKey, + direction: undefined + }); + }); + }); }); diff --git a/projects/aca-content/src/lib/directives/document-list.directive.ts b/projects/aca-content/src/lib/directives/document-list.directive.ts index 3b4083e37..bb4aab843 100644 --- a/projects/aca-content/src/lib/directives/document-list.directive.ts +++ b/projects/aca-content/src/lib/directives/document-list.directive.ts @@ -193,7 +193,8 @@ export class DocumentListDirective implements OnInit { ]; const [currentKey, currentDir] = [ - this.preferences.get(`${this.sortingPreferenceKey}.sorting.key`, null), + this.preferences.get(`${this.sortingPreferenceKey}.sorting.sortingKey`, null) || + this.preferences.get(`${this.sortingPreferenceKey}.sorting.key`, null), this.preferences.get(`${this.sortingPreferenceKey}.sorting.direction`, null) ];