Browse Source

Fix for router sync.

pull/592/head
Sebastian 5 years ago
parent
commit
b6f8cce275
  1. 33
      frontend/app/framework/angular/routers/router-2-state.spec.ts
  2. 9
      frontend/app/framework/angular/routers/router-2-state.ts
  3. 107
      frontend/app/shared/state/query.spec.ts
  4. 4
      frontend/app/shared/state/query.ts

33
frontend/app/framework/angular/routers/router-2-state.spec.ts

@ -23,17 +23,17 @@ describe('Router2State', () => {
synchronizer.writeValue(value, params); 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 params: Params = {};
const value = 123; const value = 123;
synchronizer.writeValue(value, params); synchronizer.writeValue(value, params);
expect(params).toEqual({}); expect(params).toEqual({ key: undefined });
}); });
it('should get string from route', () => { it('should get string from route', () => {
@ -60,27 +60,27 @@ describe('Router2State', () => {
synchronizer.writeValue(value, params); 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 params: Params = {};
const value = {}; const value = {};
synchronizer.writeValue(value, params); 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 params: Params = {};
const value = 123; const value = 123;
synchronizer.writeValue(value, params); synchronizer.writeValue(value, params);
expect(params).toEqual({}); expect(params).toEqual({ key: undefined });
}); });
it('should get object from route', () => { it('should get object from route', () => {
@ -117,45 +117,44 @@ describe('Router2State', () => {
synchronizer.writeValue(value, params); synchronizer.writeValue(value, params);
expect(params['page']).toEqual('10'); expect(params).toEqual({ take: '20', page: '10' });
expect(params['take']).toEqual('20');
localStore.verify(x => x.setInt('contents.pageSize', 20), Times.once()); 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 params: Params = {};
const value = new Pager(0, 0, 20, true); const value = new Pager(0, 0, 20, true);
synchronizer.writeValue(value, params); synchronizer.writeValue(value, params);
expect(params['page']).toBeUndefined(); expect(params).toEqual({ take: '20', page: undefined });
expect(params['take']).toEqual('20');
localStore.verify(x => x.setInt('contents.pageSize', 20), Times.once()); 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 params: Params = {};
const value = 123; const value = 123;
synchronizer.writeValue(value, params); synchronizer.writeValue(value, params);
expect(params).toEqual({}); expect(params).toEqual({ take: undefined, page: undefined });
localStore.verify(x => x.setInt('contents.pageSize', 20), Times.never()); 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 params: Params = {};
const value = null; const value = null;
synchronizer.writeValue(value, params); synchronizer.writeValue(value, params);
expect(params).toEqual({}); expect(params).toEqual({ take: undefined, page: undefined });
localStore.verify(x => x.setInt('contents.pageSize', 20), Times.never()); localStore.verify(x => x.setInt('contents.pageSize', 20), Times.never());
}); });

9
frontend/app/framework/angular/routers/router-2-state.ts

@ -54,6 +54,9 @@ export class PagerSynchronizer implements RouteSynchronizer {
} }
public writeValue(state: any, params: Params) { public writeValue(state: any, params: Params) {
params['page'] = undefined;
params['take'] = undefined;
if (Types.is(state, Pager)) { if (Types.is(state, Pager)) {
if (state.page > 0) { if (state.page > 0) {
params['page'] = state.page.toString(); params['page'] = state.page.toString();
@ -81,6 +84,8 @@ export class StringSynchronizer implements RouteSynchronizer {
} }
public writeValue(state: any, params: Params) { public writeValue(state: any, params: Params) {
params[this.name] = undefined;
if (Types.isString(state)) { if (Types.isString(state)) {
params[this.name] = state; params[this.name] = state;
} }
@ -110,13 +115,17 @@ export class StringKeysSynchronizer implements RouteSynchronizer {
} }
public writeValue(state: any, params: Params) { public writeValue(state: any, params: Params) {
params[this.name] = undefined;
if (Types.isObject(state)) { if (Types.isObject(state)) {
const value = Object.keys(state).join(','); const value = Object.keys(state).join(',');
if (value.length > 0) {
params[this.name] = value; params[this.name] = value;
} }
} }
} }
}
export interface StateSynchronizer { export interface StateSynchronizer {
mapTo<T extends object>(state: State<T>): StateSynchronizerMap<T>; mapTo<T extends object>(state: State<T>): StateSynchronizerMap<T>;

107
frontend/app/shared/state/query.spec.ts

@ -1,3 +1,4 @@
import { Params } from '@angular/router';
/* /*
* Squidex Headless CMS * Squidex Headless CMS
* *
@ -6,7 +7,7 @@
*/ */
import { Query } from '@app/shared/internal'; import { Query } from '@app/shared/internal';
import { equalsQuery } from './query'; import { equalsQuery, QueryFullTextSynchronizer, QuerySynchronizer } from './query';
describe('equalsQuery', () => { describe('equalsQuery', () => {
it('should return true when comparing with empty query', () => { it('should return true when comparing with empty query', () => {
@ -54,3 +55,107 @@ describe('equalsQuery', () => {
expect(equalsQuery(lhs, rhs)).toBeTruthy(); 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();
});
});

4
frontend/app/shared/state/query.ts

@ -128,6 +128,8 @@ export class QueryFullTextSynchronizer implements RouteSynchronizer {
} }
public writeValue(state: any, params: Params) { public writeValue(state: any, params: Params) {
params['query'] = undefined;
if (Types.isObject(state) && Types.isString(state.fullText) && state.fullText.length > 0) { if (Types.isObject(state) && Types.isString(state.fullText) && state.fullText.length > 0) {
params['query'] = state.fullText; params['query'] = state.fullText;
} }
@ -144,6 +146,8 @@ export class QuerySynchronizer implements RouteSynchronizer {
} }
public writeValue(state: any, params: Params) { public writeValue(state: any, params: Params) {
params['query'] = undefined;
if (Types.isObject(state)) { if (Types.isObject(state)) {
params['query'] = serializeQuery(state); params['query'] = serializeQuery(state);
} }

Loading…
Cancel
Save