Skip to content

Commit

Permalink
Merge pull request #31 from angelnikolov/bug/30_default_parameters
Browse files Browse the repository at this point in the history
#30 Fixed an issue where undefined parameters were changed to null
  • Loading branch information
angelnikolov authored Mar 29, 2019
2 parents 03277c5 + 30c54d2 commit fabdd2a
Show file tree
Hide file tree
Showing 24 changed files with 420 additions and 246 deletions.
39 changes: 38 additions & 1 deletion cacheable.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class Service {
return timer(1000).pipe(mapTo('SAVED'));
}

mockServiceCallWithMultipleParameters(parameter1, parameter2) {
return timer(1000).pipe(mapTo({ payload: [parameter1, parameter2] }));
}

@Cacheable()
getData(parameter: string) {
return this.mockServiceCall(parameter);
Expand Down Expand Up @@ -107,7 +111,18 @@ class Service {
getDataWithCacheBusting(parameter: string) {
return this.mockServiceCall(parameter);
}

@Cacheable()
getDataWithUndefinedParameter(parameter: string = '') {
return this.mockServiceCall(parameter);
}

@Cacheable()
getDataWithMultipleUndefinedParameters(parameter: string = 'Parameter1', parameter1: string = 'Parameter2') {
return this.mockServiceCallWithMultipleParameters(parameter, parameter1);
}
}

describe('CacheableDecorator', () => {
let service: Service = null;
let mockServiceCallSpy: jasmine.Spy = null;
Expand Down Expand Up @@ -169,7 +184,6 @@ describe('CacheableDecorator', () => {
expect(mockServiceCallSpy).toHaveBeenCalledTimes(4);
});


it('returns observables in cache with a referential type params', () => {
let params = {
number: [1]
Expand Down Expand Up @@ -780,6 +794,29 @@ describe('CacheableDecorator', () => {
*/
expect(mockServiceCallSpy).toHaveBeenCalledTimes(6);
});

it('should not change undefined parameters to null', () => {
service.getDataWithUndefinedParameter(undefined);
expect(mockServiceCallSpy).toHaveBeenCalledWith('');
service.getDataWithUndefinedParameter();
expect(mockServiceCallSpy).toHaveBeenCalledWith('');

let mockServiceCallWithMultipleParametersSpy = spyOn(service, 'mockServiceCallWithMultipleParameters').and.callThrough();

const asyncData = _timedStreamAsyncAwait(
service.getDataWithMultipleUndefinedParameters(undefined, undefined),
1000
);

expect(asyncData).toEqual({ payload: ['Parameter1', 'Parameter2'] });
expect(mockServiceCallWithMultipleParametersSpy).toHaveBeenCalledWith('Parameter1', 'Parameter2');

service.getDataWithMultipleUndefinedParameters(undefined, undefined);
expect(mockServiceCallWithMultipleParametersSpy).toHaveBeenCalledTimes(1);

service.getDataWithMultipleUndefinedParameters('Parameter1', undefined);
expect(mockServiceCallWithMultipleParametersSpy).toHaveBeenCalledTimes(2);
});
});

function _timedStreamAsyncAwait(stream$: Observable<any>, skipTime?: number) {
Expand Down
2 changes: 1 addition & 1 deletion cacheable.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {

/* use function instead of an arrow function to keep context of invocation */
(propertyDescriptor.value as any) = function (..._parameters) {
let parameters = JSON.parse(JSON.stringify(_parameters));
let parameters = _parameters.map(param => param !== undefined ? JSON.parse(JSON.stringify(param)) : param);
let _foundCachePair = cachePairs.find(cp =>
cacheConfig.cacheResolver(cp.parameters, parameters)
);
Expand Down
5 changes: 3 additions & 2 deletions dist/cache-buster.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Observable } from 'rxjs';
import { ICacheBusterConfig } from './common/ICacheBusterConfig';
export declare const CacheBuster: (cacheBusterConfig?: ICacheBusterConfig) => (_target: Object, _propertyKey: string, propertyDescriptor: TypedPropertyDescriptor<(...args: any[]) => Observable<any>>) => TypedPropertyDescriptor<(...args: any[]) => Observable<any>>;
import { ICacheable } from './common';
import { Observable } from 'rxjs';
export declare function CacheBuster(cacheBusterConfig?: ICacheBusterConfig): (_target: Object, _propertyKey: string, propertyDescriptor: TypedPropertyDescriptor<ICacheable<Observable<any>>>) => TypedPropertyDescriptor<ICacheable<Observable<any>>>;
34 changes: 21 additions & 13 deletions dist/cache-buster.decorator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/cache-buster.decorator.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion dist/cacheable.decorator.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Observable, Subject } from 'rxjs';
import { ICacheable } from './common';
import { IObservableCacheConfig } from './common/IObservableCacheConfig';
export declare const globalCacheBusterNotifier: Subject<void>;
export declare const Cacheable: (cacheConfig?: IObservableCacheConfig) => (_target: Object, _propertyKey: string, propertyDescriptor: TypedPropertyDescriptor<(...args: any[]) => Observable<any>>) => TypedPropertyDescriptor<(...args: any[]) => Observable<any>>;
export declare function Cacheable(cacheConfig?: IObservableCacheConfig): (_target: Object, _propertyKey: string, propertyDescriptor: TypedPropertyDescriptor<ICacheable<Observable<any>>>) => TypedPropertyDescriptor<ICacheable<Observable<any>>>;
191 changes: 101 additions & 90 deletions dist/cacheable.decorator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fabdd2a

Please sign in to comment.