Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(sampleTime): convert sampleTime tests to run mode (#6323)
- Loading branch information
Showing
1 changed file
with
116 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,150 @@ | ||
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; | ||
/** @prettier */ | ||
import { sampleTime, mergeMap } from 'rxjs/operators'; | ||
import { TestScheduler } from 'rxjs/testing'; | ||
import { of } from 'rxjs'; | ||
|
||
declare const rxTestScheduler: TestScheduler; | ||
import { observableMatcher } from '../helpers/observableMatcher'; | ||
|
||
/** @test {sampleTime} */ | ||
describe('sampleTime operator', () => { | ||
it('should get samples on a delay', () => { | ||
const e1 = hot('a---b-c---------d--e---f-g-h--|'); | ||
const e1subs = '^ !'; | ||
const expected = '-------c-------------e------h-|'; | ||
// timer -------!------!------!------!-- | ||
describe('sampleTime', () => { | ||
let rxTest: TestScheduler; | ||
|
||
expectObservable(e1.pipe(sampleTime(70, rxTestScheduler))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
beforeEach(() => { | ||
rxTest = new TestScheduler(observableMatcher); | ||
}); | ||
|
||
it('should sample nothing if new value has not arrived', () => { | ||
const e1 = hot('----a-^--b----c--------------f----|'); | ||
const e1subs = '^ !'; | ||
const expected = '-----------c----------------|'; | ||
// timer -----------!----------!--------- | ||
it('should get samples on a delay', () => { | ||
rxTest.run(({ hot, expectObservable, expectSubscriptions, time }) => { | ||
const e1 = hot(' a---b-c---------d--e---f-g-h--|'); | ||
const e1subs = ' ^-----------------------------!'; | ||
const expected = ' -------c-------------e------h-|'; | ||
// period -------!------!------!------!-- | ||
const period = time('-------| '); | ||
|
||
expectObservable(e1.pipe(sampleTime(period, rxTest))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); | ||
|
||
expectObservable(e1.pipe(sampleTime(110, rxTestScheduler))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
it('should sample nothing if new value has not arrived', () => { | ||
rxTest.run(({ hot, expectObservable, expectSubscriptions, time }) => { | ||
const e1 = hot(' ----a-^--b----c--------------f----|'); | ||
const e1subs = ' ^---------------------------!'; | ||
const expected = ' -----------c----------------|'; | ||
// period -----------!----------!--------- | ||
const period = time(' -----------| '); | ||
|
||
expectObservable(e1.pipe(sampleTime(period, rxTest))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); | ||
|
||
it('should sample if new value has arrived, even if it is the same value', () => { | ||
const e1 = hot('----a-^--b----c----------c---f----|'); | ||
const e1subs = '^ !'; | ||
const expected = '-----------c----------c-----|'; | ||
// timer -----------!----------!--------- | ||
|
||
expectObservable(e1.pipe(sampleTime(110, rxTestScheduler))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
rxTest.run(({ hot, expectObservable, expectSubscriptions, time }) => { | ||
const e1 = hot('----a-^--b----c----------c---f----|'); | ||
const e1subs = ' ^---------------------------!'; | ||
const expected = ' -----------c----------c-----|'; | ||
// period -----------!----------!--------- | ||
const period = time(' -----------| '); | ||
|
||
expectObservable(e1.pipe(sampleTime(period, rxTest))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); | ||
|
||
it('should sample nothing if source has not nexted by time of sample', () => { | ||
const e1 = hot('----a-^-------------b-------------|'); | ||
const e1subs = '^ !'; | ||
const expected = '----------------------b-----|'; | ||
// timer -----------!----------!--------- | ||
|
||
expectObservable(e1.pipe(sampleTime(110, rxTestScheduler))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
rxTest.run(({ hot, expectObservable, expectSubscriptions, time }) => { | ||
const e1 = hot('----a-^-------------b-------------|'); | ||
const e1subs = ' ^---------------------------!'; | ||
const expected = ' ----------------------b-----|'; | ||
// period -----------!----------!--------- | ||
const period = time(' -----------| '); | ||
|
||
expectObservable(e1.pipe(sampleTime(period, rxTest))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); | ||
|
||
it('should raise error if source raises error', () => { | ||
const e1 = hot('----a-^--b----c----d----#'); | ||
const e1subs = '^ !'; | ||
const expected = '-----------c------#'; | ||
// timer -----------!----------!--------- | ||
|
||
expectObservable(e1.pipe(sampleTime(110, rxTestScheduler))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
rxTest.run(({ hot, expectObservable, expectSubscriptions, time }) => { | ||
const e1 = hot('----a-^--b----c----d----#'); | ||
const e1subs = ' ^-----------------!'; | ||
const expected = ' -----------c------#'; | ||
// period -----------!----------!--------- | ||
const period = time(' -----------| '); | ||
|
||
expectObservable(e1.pipe(sampleTime(period, rxTest))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); | ||
|
||
it('should allow unsubscribing explicitly and early', () => { | ||
const e1 = hot('----a-^--b----c----d----e----f----|'); | ||
const unsub = ' ! '; | ||
const e1subs = '^ ! '; | ||
const expected = '-----------c----- '; | ||
// timer -----------!----------!--------- | ||
|
||
expectObservable(e1.pipe(sampleTime(110, rxTestScheduler)), unsub).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
rxTest.run(({ hot, expectObservable, expectSubscriptions, time }) => { | ||
const e1 = hot('----a-^--b----c----d----e----f----|'); | ||
const unsub = ' ----------------! '; | ||
const e1subs = ' ^---------------! '; | ||
const expected = ' -----------c----- '; | ||
// period -----------!----------!--------- | ||
const period = time(' -----------| '); | ||
|
||
expectObservable(e1.pipe(sampleTime(period, rxTest)), unsub).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); | ||
|
||
it('should not break unsubscription chains when result is unsubscribed explicitly', () => { | ||
const e1 = hot('----a-^--b----c----d----e----f----|'); | ||
const e1subs = '^ ! '; | ||
// timer -----------!----------!--------- | ||
const expected = '-----------c----- '; | ||
const unsub = ' ! '; | ||
|
||
const result = e1.pipe( | ||
mergeMap((x: string) => of(x)), | ||
sampleTime(110, rxTestScheduler), | ||
mergeMap((x: string) => of(x)) | ||
); | ||
|
||
expectObservable(result, unsub).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
rxTest.run(({ hot, expectObservable, expectSubscriptions, time }) => { | ||
const e1 = hot('----a-^--b----c----d----e----f----|'); | ||
const e1subs = ' ^---------------! '; | ||
// period -----------!----------!--------- | ||
const period = time(' -----------| '); | ||
const expected = ' -----------c----- '; | ||
const unsub = ' ----------------! '; | ||
|
||
const result = e1.pipe( | ||
mergeMap((x: string) => of(x)), | ||
sampleTime(period, rxTest), | ||
mergeMap((x: string) => of(x)) | ||
); | ||
|
||
expectObservable(result, unsub).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); | ||
|
||
it('should completes if source does not emits', () => { | ||
const e1 = cold('|'); | ||
const e1subs = '(^!)'; | ||
const expected = '|'; | ||
|
||
expectObservable(e1.pipe(sampleTime(60, rxTestScheduler))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
rxTest.run(({ cold, expectObservable, expectSubscriptions, time }) => { | ||
const e1 = cold(' | '); | ||
const e1subs = ' (^!) '; | ||
const expected = ' | '; | ||
const period = time('-----|'); | ||
|
||
expectObservable(e1.pipe(sampleTime(period, rxTest))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); | ||
|
||
it('should raise error if source throws immediately', () => { | ||
const e1 = cold('#'); | ||
const e1subs = '(^!)'; | ||
const expected = '#'; | ||
|
||
expectObservable(e1.pipe(sampleTime(60, rxTestScheduler))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
rxTest.run(({ cold, expectObservable, expectSubscriptions, time }) => { | ||
const e1 = cold(' # '); | ||
const e1subs = ' (^!) '; | ||
const expected = ' # '; | ||
const period = time('-----|'); | ||
|
||
expectObservable(e1.pipe(sampleTime(period, rxTest))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); | ||
|
||
it('should not completes if source does not complete', () => { | ||
const e1 = cold('-'); | ||
const e1subs = '^'; | ||
const expected = '-'; | ||
|
||
expectObservable(e1.pipe(sampleTime(60, rxTestScheduler))).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
it('should not complete if source does not complete', () => { | ||
rxTest.run(({ cold, expectObservable, expectSubscriptions, time }) => { | ||
const e1 = cold(' --------'); | ||
const e1subs = ' ^------!'; | ||
const expected = ' --------'; | ||
const period = time('-----| '); | ||
const e1unsbs = ' -------!'; | ||
|
||
expectObservable(e1.pipe(sampleTime(period, rxTest)), e1unsbs).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); | ||
}); |