Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor distinch until changed #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/distinct-until-changed.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { distinctUntilChanged as d } from 'rxjs/operators'
import { equals } from 'ramda'

export default function distinctUntilChanged(source) {
return source.pipe(d(diligentEquals))
}
const distinctUntilChanged = d(equals)

function diligentEquals(a, b) {
if (typeof a.equals === 'function') return a.equals(b)
return equals(a, b)
}
export default distinctUntilChanged
126 changes: 89 additions & 37 deletions test/distinct-until-changed.test.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,99 @@
import * as matchers from 'jest-immutable-matchers'
import { finalize } from 'rxjs/operators'
import * as R from 'ramda'
import { Map } from 'immutable';
import { TestScheduler } from 'rxjs/testing'
import { distinctUntilChanged } from '../src'
import { of } from 'rxjs'
import { Map, fromJS } from 'immutable'

describe('Distinct until changed', () => {
beforeEach(() => jest.addMatchers(matchers))

it('Should understand native JS types', done => {
const mockCallBack = jest.fn()

of(1, 2, 2, 3, 2)
.pipe(
distinctUntilChanged,
finalize(() => {
const calls = mockCallBack.mock.calls
expect(calls).toEqual([[1], [2], [3], [2]])
done()
})
)
.subscribe(mockCallBack, fail)
it('Should understand native JS types', () => {
const testScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected)
})

testScheduler.run((helpers) => {
const { cold, expectObservable } = helpers
const e1 = cold(' -1--2--2-3-2-|');
const e1subs = ' ^------------!'
const expected = '-1--2----3-2-'
expectObservable(e1.pipe(distinctUntilChanged), e1subs).toBe(expected)
})
})

it('Should work with plain javascript objects', () => {
const testScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected)
})
testScheduler.run((helpers) => {
const { cold, expectObservable } = helpers
const e1 = cold(' -a--b--c---|', {
a: { someKey: 'someValue' },
b: { someKey: 'someValue' },
c: { someKey: 'someValue' }
})
const e1subs = ' ^----------!'
const expected = '-a---------'
expectObservable(e1.pipe(distinctUntilChanged), e1subs).toBe(expected, {
a: { someKey: 'someValue' }
})
})
})


it('Should understand immutable structures', () => {
const testScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
})

testScheduler.run(({cold, expectObservable}) => {
const values = [
Map({ a: 1 }),
Map({ a: 2 }),
Map({ a: 2 }),
Map({ a: 3 }),
Map({ a: 2 })
];

const e1 = cold('-0-1-2-3-4-|', values);

expectObservable(e1.pipe(distinctUntilChanged), '^- 20ms -!').toBe('-0-2---3-4-|', values);
});
})

it('Should understand immutable structures', done => {
const mockCallBack = jest.fn()

of(
Map({ a: 1 }),
Map({ a: 2 }),
Map({ a: 2 }),
Map({ a: 3 }),
Map({ a: 2 })
)
.pipe(
distinctUntilChanged,
finalize(() => {
const calls = fromJS(mockCallBack.mock.calls)
expect(calls).toEqualImmutable(
fromJS([[{ a: 1 }], [{ a: 2 }], [{ a: 3 }], [{ a: 2 }]])
)
done()
})
)
.subscribe(mockCallBack, fail)
it('Should account for implemented equals method', () => {
const testScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected)
})
testScheduler.run((helpers) => {
const { cold, expectObservable } = helpers
const getSecondProp = R.prop('secondProp')
const equalityFn = function (a) {
return getSecondProp(a) === getSecondProp(this)
}
const values = {
a: {
someKey: 'someValue1',
secondProp: 'thatIsSame',
equals: equalityFn
},
b: {
someKey: 'someValue2',
secondProp: 'thatIsSame',
equals: equalityFn
},
c: {
someKey: 'someValue3',
secondProp: 'thatIsSame',
equals: equalityFn
}
}
const e1 = cold(' -a--b--c---|', values)
const e1subs = ' ^----------!'
const expected = '-a---------'
expectObservable(e1.pipe(distinctUntilChanged), e1subs).toBe(expected, {
a: values.a
})
})
})
})