Skip to content

Commit

Permalink
fix(state): fix tests in selectSlice and distinctUntilSomeChanges
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Jul 26, 2020
1 parent a3df99e commit a573a5a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,21 @@ describe('distinctUntilSomeChanged operator', () => {
val: 2,
objVal: {
foo: 'foo',
bar: 'bar'
bar: 0
}
},
d: {
val: 2,
objVal: {
foo: 'foo2',
bar: 'bar'
bar: 0
}
},
e: {
val: 2,
objVal: {
foo: 'foo2',
bar: 'bar3'
bar: 3
}
}
};
Expand Down
9 changes: 5 additions & 4 deletions libs/state/spec/rxjs/operators/selectSlice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TestScheduler } from 'rxjs/testing';
import { selectSlice } from '../../../src/lib/rxjs/operators/selectSlice';
import { KeyCompareMap } from '../../../src/lib/rxjs/interfaces';
import { map, mergeMap } from 'rxjs/operators';
import { ColdObservable } from 'rxjs/internal/testing/ColdObservable';

let testScheduler: TestScheduler;

Expand Down Expand Up @@ -109,25 +110,25 @@ describe('selectSlice operator', () => {
val: 2,
objVal: {
foo: 'foo',
bar: 'bar'
bar: 0
}
},
d: {
val: 2,
objVal: {
foo: 'foo2',
bar: 'bar'
bar: 0
}
},
e: {
val: 2,
objVal: {
foo: 'foo2',
bar: 'bar3'
bar: 3
}
}
};
const e1 = cold('--a--a--b--c--d--e--|', values);
const e1: ColdObservable<ISelectSliceTest> = cold('--a--a--b--c--d--e--|', values);
const e1subs = '^-------------------!';
const expected = '--a-----b--c--d-----|';
const keyCompare: KeyCompareMap<ISelectSliceTest> = {
Expand Down
12 changes: 6 additions & 6 deletions libs/state/src/lib/rxjs/operators/selectSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import { distinctUntilSomeChanged } from './distinctUntilSomeChanged';
* { title: 'nextTitle', items: ['foo', 'baR'], panelOpen: true },
* { title: 'nextTitle', items: ['fooRz', 'boo'], panelOpen: false },
* );
* const slice$ = state$.pipe(selectSlice(['title', 'items']), tap(console.log)).subscribe();
* const slice$ = state$.pipe(selectSlice(['title', 'items'], customComparison), tap(console.log)).subscribe();
*
* // displays:
* // { title: 'myTitle', items: ['foo', 'bar'] }
Expand All @@ -78,16 +78,16 @@ export function selectSlice<T extends object, K extends keyof T>(
): OperatorFunction<T, PickSlice<T, K>> {
return (o$: Observable<T>): Observable<PickSlice<T, K>> =>
o$.pipe(
filter(state => state !== undefined),
map(state => {
filter((state) => state !== undefined),
map((state) => {
// forward null
if (state === null) {
return null;
}

const definedKeys = keys
// filter out undefined properties e. g. {}, { str: undefined }
.filter(k => state.hasOwnProperty(k) && state[k] !== undefined);
.filter((k) => state.hasOwnProperty(k) && state[k] !== undefined);

// this will get filtered out in the next operator
// {str: 'test'} => selectSlice([]) => no emission
Expand All @@ -100,13 +100,13 @@ export function selectSlice<T extends object, K extends keyof T>(

// create view-model
return definedKeys
.filter(k => state.hasOwnProperty(k) && state[k] !== undefined)
.filter((k) => state.hasOwnProperty(k) && state[k] !== undefined)
.reduce((vm, key) => {
vm[key] = state[key];
return vm;
}, {} as PickSlice<T, K>);
}),
filter(v => v !== undefined),
filter((v) => v !== undefined),
distinctUntilSomeChanged(keys, keyCompareMap)
);
}
Expand Down

0 comments on commit a573a5a

Please sign in to comment.