From b6f8cce27547b4d69d9dcd6d3c34b1ab8b9c1ed6 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 16 Oct 2020 15:56:18 +0200 Subject: [PATCH] Fix for router sync. --- .../angular/routers/router-2-state.spec.ts | 33 +++--- .../angular/routers/router-2-state.ts | 11 +- frontend/app/shared/state/query.spec.ts | 107 +++++++++++++++++- frontend/app/shared/state/query.ts | 4 + 4 files changed, 136 insertions(+), 19 deletions(-) diff --git a/frontend/app/framework/angular/routers/router-2-state.spec.ts b/frontend/app/framework/angular/routers/router-2-state.spec.ts index 0b0b9fde6..500fcf0ea 100644 --- a/frontend/app/framework/angular/routers/router-2-state.spec.ts +++ b/frontend/app/framework/angular/routers/router-2-state.spec.ts @@ -23,17 +23,17 @@ describe('Router2State', () => { synchronizer.writeValue(value, params); - expect(params['key']).toEqual('my-string'); + expect(params).toEqual({ key: 'my-string' }); }); - it('should not write value to route when not a string', () => { + it('Should write undefined when not a string', () => { const params: Params = {}; const value = 123; synchronizer.writeValue(value, params); - expect(params).toEqual({}); + expect(params).toEqual({ key: undefined }); }); it('should get string from route', () => { @@ -60,27 +60,27 @@ describe('Router2State', () => { synchronizer.writeValue(value, params); - expect(params['key']).toEqual('flag1,flag2'); + expect(params).toEqual({ key: 'flag1,flag2' }); }); - it('should write empty object to route', () => { + it('Should write undefined when empty', () => { const params: Params = {}; const value = {}; synchronizer.writeValue(value, params); - expect(params['key']).toEqual(''); + expect(params).toEqual({ key: undefined }); }); - it('should not write value to route when not an object', () => { + it('Should write undefined when not an object', () => { const params: Params = {}; const value = 123; synchronizer.writeValue(value, params); - expect(params).toEqual({}); + expect(params).toEqual({ key: undefined }); }); it('should get object from route', () => { @@ -117,45 +117,44 @@ describe('Router2State', () => { synchronizer.writeValue(value, params); - expect(params['page']).toEqual('10'); - expect(params['take']).toEqual('20'); + expect(params).toEqual({ take: '20', page: '10' }); localStore.verify(x => x.setInt('contents.pageSize', 20), Times.once()); }); - it('should not write page if zero', () => { + it('Should write undefined when page number is zero', () => { const params: Params = {}; const value = new Pager(0, 0, 20, true); synchronizer.writeValue(value, params); - expect(params['page']).toBeUndefined(); - expect(params['take']).toEqual('20'); + expect(params).toEqual({ take: '20', page: undefined }); localStore.verify(x => x.setInt('contents.pageSize', 20), Times.once()); }); - it('should not write value to route when not pager', () => { + it('should write undefined when value not a pager', () => { const params: Params = {}; const value = 123; synchronizer.writeValue(value, params); - expect(params).toEqual({}); + expect(params).toEqual({ take: undefined, page: undefined }); localStore.verify(x => x.setInt('contents.pageSize', 20), Times.never()); }); - it('should not write value to route when null', () => { + it('should write undefined when value is null', () => { const params: Params = {}; const value = null; synchronizer.writeValue(value, params); - expect(params).toEqual({}); + expect(params).toEqual({ take: undefined, page: undefined }); + localStore.verify(x => x.setInt('contents.pageSize', 20), Times.never()); }); diff --git a/frontend/app/framework/angular/routers/router-2-state.ts b/frontend/app/framework/angular/routers/router-2-state.ts index 4b5abf78e..d47616305 100644 --- a/frontend/app/framework/angular/routers/router-2-state.ts +++ b/frontend/app/framework/angular/routers/router-2-state.ts @@ -54,6 +54,9 @@ export class PagerSynchronizer implements RouteSynchronizer { } public writeValue(state: any, params: Params) { + params['page'] = undefined; + params['take'] = undefined; + if (Types.is(state, Pager)) { if (state.page > 0) { params['page'] = state.page.toString(); @@ -81,6 +84,8 @@ export class StringSynchronizer implements RouteSynchronizer { } public writeValue(state: any, params: Params) { + params[this.name] = undefined; + if (Types.isString(state)) { params[this.name] = state; } @@ -110,10 +115,14 @@ export class StringKeysSynchronizer implements RouteSynchronizer { } public writeValue(state: any, params: Params) { + params[this.name] = undefined; + if (Types.isObject(state)) { const value = Object.keys(state).join(','); - params[this.name] = value; + if (value.length > 0) { + params[this.name] = value; + } } } } diff --git a/frontend/app/shared/state/query.spec.ts b/frontend/app/shared/state/query.spec.ts index f2ead7f8b..0165115ca 100644 --- a/frontend/app/shared/state/query.spec.ts +++ b/frontend/app/shared/state/query.spec.ts @@ -1,3 +1,4 @@ +import { Params } from '@angular/router'; /* * Squidex Headless CMS * @@ -6,7 +7,7 @@ */ import { Query } from '@app/shared/internal'; -import { equalsQuery } from './query'; +import { equalsQuery, QueryFullTextSynchronizer, QuerySynchronizer } from './query'; describe('equalsQuery', () => { it('should return true when comparing with empty query', () => { @@ -53,4 +54,108 @@ describe('equalsQuery', () => { expect(equalsQuery(lhs, rhs)).toBeTruthy(); }); +}); + +describe('QueryFullTextSynchronizer', () => { + const synchronizer = new QueryFullTextSynchronizer(); + + it('should write full text to route', () => { + const params: Params = {}; + + const value = { fullText: 'my-query' }; + + synchronizer.writeValue(value, params); + + expect(params).toEqual({ query: 'my-query' }); + }); + + it('Should write undefined when not a query', () => { + const params: Params = {}; + + const value = 123; + + synchronizer.writeValue(value, params); + + expect(params).toEqual({ query: undefined }); + }); + + it('Should write undefined query has no full text', () => { + const params: Params = {}; + + const value = { fullText: '' }; + + synchronizer.writeValue(value, params); + + expect(params).toEqual({ query: undefined }); + }); + + it('should get query from route', () => { + const params: Params = { + query: 'my-query' + }; + + const value = synchronizer.getValue(params); + + expect(value).toEqual({ fullText: 'my-query' }); + }); + + it('should get query as undefined from route', () => { + const params: Params = {}; + + const value = synchronizer.getValue(params); + + expect(value).toBeUndefined(); + }); +}); + +describe('QuerySynchronizer', () => { + const synchronizer = new QuerySynchronizer(); + + it('should write query to route', () => { + const params: Params = {}; + + const value = { filter: 'my-filter' }; + + synchronizer.writeValue(value, params); + + expect(params).toEqual({ query: '{"filter":"my-filter","sort":[]}' }); + }); + + it('Should write undefined when not a query', () => { + const params: Params = {}; + + const value = 123; + + synchronizer.writeValue(value, params); + + expect(params).toEqual({ query: undefined }); + }); + + it('should get query from route', () => { + const params: Params = { + query: '{"filter":"my-filter"}' + }; + + const value = synchronizer.getValue(params) as any; + + expect(value).toEqual({ filter: 'my-filter' }); + }); + + it('should get query full text from route', () => { + const params: Params = { + query: 'my-query' + }; + + const value = synchronizer.getValue(params); + + expect(value).toEqual({ fullText: 'my-query' }); + }); + + it('should get query as undefined from route', () => { + const params: Params = {}; + + const value = synchronizer.getValue(params); + + expect(value).toBeUndefined(); + }); }); \ No newline at end of file diff --git a/frontend/app/shared/state/query.ts b/frontend/app/shared/state/query.ts index 79c0cfb4a..f3d6e4385 100644 --- a/frontend/app/shared/state/query.ts +++ b/frontend/app/shared/state/query.ts @@ -128,6 +128,8 @@ export class QueryFullTextSynchronizer implements RouteSynchronizer { } public writeValue(state: any, params: Params) { + params['query'] = undefined; + if (Types.isObject(state) && Types.isString(state.fullText) && state.fullText.length > 0) { params['query'] = state.fullText; } @@ -144,6 +146,8 @@ export class QuerySynchronizer implements RouteSynchronizer { } public writeValue(state: any, params: Params) { + params['query'] = undefined; + if (Types.isObject(state)) { params['query'] = serializeQuery(state); }