diff --git a/.prettierrc.json b/.prettierrc.json index 14a7fef481..250c75e465 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -10,7 +10,7 @@ } }, { - "files": ["spec/operators/**/*.ts"], + "files": ["spec/operators/**/*.ts", "spec/subjects/**/*.ts"], "options": { "requirePragma": false } diff --git a/spec/subjects/AsyncSubject-spec.ts b/spec/subjects/AsyncSubject-spec.ts index f5735071e0..4792477de3 100644 --- a/spec/subjects/AsyncSubject-spec.ts +++ b/spec/subjects/AsyncSubject-spec.ts @@ -196,13 +196,13 @@ describe('AsyncSubject', () => { const subject = new AsyncSubject(); let calls = 0; subject.subscribe({ - next: value => { + next: (value) => { calls++; if (calls < 2) { // if this is more than 1, we're reentrant, and that's bad. subject.complete(); } - } + }, }); subject.next(1); @@ -215,13 +215,13 @@ describe('AsyncSubject', () => { const subject = new AsyncSubject(); let calls = 0; subject.subscribe({ - next: value => { + next: (value) => { calls++; if (calls < 2) { // if this is more than 1, we're reentrant, and that's bad. subject.next(value + 1); } - } + }, }); subject.next(1); @@ -231,18 +231,18 @@ describe('AsyncSubject', () => { }); it('should allow reentrant subscriptions', () => { - const subject = new AsyncSubject() + const subject = new AsyncSubject(); let results: any[] = []; subject.subscribe({ next: (value) => { subject.subscribe({ - next: value => results.push('inner: ' + (value + value)), - complete: () => results.push('inner: done') + next: (value) => results.push('inner: ' + (value + value)), + complete: () => results.push('inner: done'), }); results.push('outer: ' + value); }, - complete: () => results.push('outer: done') + complete: () => results.push('outer: done'), }); subject.next(1); diff --git a/spec/subjects/BehaviorSubject-spec.ts b/spec/subjects/BehaviorSubject-spec.ts index ff5c2d2882..594f0f75a5 100644 --- a/spec/subjects/BehaviorSubject-spec.ts +++ b/spec/subjects/BehaviorSubject-spec.ts @@ -1,11 +1,18 @@ import { expect } from 'chai'; -import { hot, expectObservable } from '../helpers/marble-testing'; import { BehaviorSubject, Subject, ObjectUnsubscribedError, of } from 'rxjs'; import { tap, mergeMapTo } from 'rxjs/operators'; import { asInteropSubject } from '../helpers/interop-helper'; +import { TestScheduler } from 'rxjs/testing'; +import { observableMatcher } from '../helpers/observableMatcher'; /** @test {BehaviorSubject} */ describe('BehaviorSubject', () => { + let testScheduler: TestScheduler; + + beforeEach(() => { + testScheduler = new TestScheduler(observableMatcher); + }); + it('should extend Subject', () => { const subject = new BehaviorSubject(null); expect(subject).to.be.instanceof(Subject); @@ -19,8 +26,7 @@ describe('BehaviorSubject', () => { }).to.throw(Error, 'derp'); }); - it('should throw an ObjectUnsubscribedError if getValue() is called ' + - 'and the BehaviorSubject has been unsubscribed', () => { + it('should throw an ObjectUnsubscribedError if getValue() is called and the BehaviorSubject has been unsubscribed', () => { const subject = new BehaviorSubject('hi there'); subject.unsubscribe(); expect(() => { @@ -63,9 +69,12 @@ describe('BehaviorSubject', () => { const expected = ['foo', 'bar']; let i = 0; - subject.subscribe({ next: (x: string) => { - expect(x).to.equal(expected[i++]); - }, complete: done }); + subject.subscribe({ + next: (x: string) => { + expect(x).to.equal(expected[i++]); + }, + complete: done, + }); subject.next('bar'); subject.complete(); @@ -81,9 +90,12 @@ describe('BehaviorSubject', () => { expect(x).to.equal(expected[i++]); }); - subject.subscribe({ next: (x: string) => { - expect(x).to.equal(expected[j++]); - }, complete: done }); + subject.subscribe({ + next: (x: string) => { + expect(x).to.equal(expected[j++]); + }, + complete: done, + }); expect(subject.observers.length).to.equal(2); subject.next('foo'); @@ -130,49 +142,59 @@ describe('BehaviorSubject', () => { }); it('should replay the previous value when subscribed', () => { - const behaviorSubject = new BehaviorSubject('0'); - function feedNextIntoSubject(x: string) { behaviorSubject.next(x); } - function feedErrorIntoSubject(err: any) { behaviorSubject.error(err); } - function feedCompleteIntoSubject() { behaviorSubject.complete(); } - - const sourceTemplate = '-1-2-3----4------5-6---7--8----9--|'; - const subscriber1 = hot(' (a|) ').pipe(mergeMapTo(behaviorSubject)); - const unsub1 = ' ! '; - const expected1 = ' 3---4------5-6-- '; - const subscriber2 = hot(' (b|) ').pipe(mergeMapTo(behaviorSubject)); - const unsub2 = ' ! '; - const expected2 = ' 4----5-6---7-- '; - const subscriber3 = hot(' (c|) ').pipe(mergeMapTo(behaviorSubject)); - const expected3 = ' 8---9--|'; - - expectObservable(hot(sourceTemplate).pipe( - tap( - { next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject } - ) - )).toBe(sourceTemplate); - expectObservable(subscriber1, unsub1).toBe(expected1); - expectObservable(subscriber2, unsub2).toBe(expected2); - expectObservable(subscriber3).toBe(expected3); + testScheduler.run(({ hot, expectObservable }) => { + const behaviorSubject = new BehaviorSubject('0'); + function feedNextIntoSubject(x: string) { + behaviorSubject.next(x); + } + function feedErrorIntoSubject(err: any) { + behaviorSubject.error(err); + } + function feedCompleteIntoSubject() { + behaviorSubject.complete(); + } + + const sourceTemplate = ' -1-2-3----4------5-6---7--8----9--|'; + const subscriber1 = hot('------(a|) ').pipe(mergeMapTo(behaviorSubject)); + const unsub1 = ' ---------------------! '; + const expected1 = ' ------3---4------5-6-- '; + const subscriber2 = hot('------------(b|) ').pipe(mergeMapTo(behaviorSubject)); + const unsub2 = ' -------------------------! '; + const expected2 = ' ------------4----5-6---7-- '; + const subscriber3 = hot('---------------------------(c|) ').pipe(mergeMapTo(behaviorSubject)); + const expected3 = ' ---------------------------8---9--|'; + + expectObservable( + hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject })) + ).toBe(sourceTemplate); + expectObservable(subscriber1, unsub1).toBe(expected1); + expectObservable(subscriber2, unsub2).toBe(expected2); + expectObservable(subscriber3).toBe(expected3); + }); }); it('should emit complete when subscribed after completed', () => { - const behaviorSubject = new BehaviorSubject('0'); - function feedNextIntoSubject(x: string) { behaviorSubject.next(x); } - function feedErrorIntoSubject(err: any) { behaviorSubject.error(err); } - function feedCompleteIntoSubject() { behaviorSubject.complete(); } - - const sourceTemplate = '-1-2-3--4--|'; - const subscriber1 = hot(' (a|)').pipe( - mergeMapTo(behaviorSubject) - ); - const expected1 = ' | '; - - expectObservable(hot(sourceTemplate).pipe( - tap( - { next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject } - ) - )).toBe(sourceTemplate); - expectObservable(subscriber1).toBe(expected1); + testScheduler.run(({ hot, expectObservable }) => { + const behaviorSubject = new BehaviorSubject('0'); + function feedNextIntoSubject(x: string) { + behaviorSubject.next(x); + } + function feedErrorIntoSubject(err: any) { + behaviorSubject.error(err); + } + function feedCompleteIntoSubject() { + behaviorSubject.complete(); + } + + const sourceTemplate = ' -1-2-3--4--| '; + const subscriber1 = hot('---------------(a|)').pipe(mergeMapTo(behaviorSubject)); + const expected1 = ' ---------------| '; + + expectObservable( + hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject })) + ).toBe(sourceTemplate); + expectObservable(subscriber1).toBe(expected1); + }); }); it('should be an Observer which can be given to Observable.subscribe', (done) => { @@ -180,15 +202,18 @@ describe('BehaviorSubject', () => { const subject = new BehaviorSubject(0); const expected = [0, 1, 2, 3, 4, 5]; - subject.subscribe( - { next: (x: number) => { + subject.subscribe({ + next: (x: number) => { expect(x).to.equal(expected.shift()); - }, error: (x) => { + }, + error: (x) => { done(new Error('should not be called')); - }, complete: () => { + }, + complete: () => { expect(subject.value).to.equal(5); done(); - } }); + }, + }); source.subscribe(subject); }); @@ -202,16 +227,19 @@ describe('BehaviorSubject', () => { const subject = new BehaviorSubject(0); const expected = [0, 1, 2, 3, 4, 5]; - subject.subscribe( - { next: (x: number) => { + subject.subscribe({ + next: (x: number) => { expect(x).to.equal(expected.shift()); - }, error: (x) => { + }, + error: (x) => { done(new Error('should not be called')); - }, complete: () => { + }, + complete: () => { expect(subject.value).to.equal(5); done(); - } }); + }, + }); - source.subscribe(asInteropSubject(subject)); + source.subscribe(asInteropSubject(subject)); }); }); diff --git a/spec/subjects/ReplaySubject-spec.ts b/spec/subjects/ReplaySubject-spec.ts index e86f05e47c..836f7260a0 100644 --- a/spec/subjects/ReplaySubject-spec.ts +++ b/spec/subjects/ReplaySubject-spec.ts @@ -39,18 +39,20 @@ describe('ReplaySubject', () => { subject.next(1); subject.next(2); subject.next(3); - subject.subscribe( - { next: (x: number) => { + subject.subscribe({ + next: (x: number) => { expect(x).to.equal(expects[i++]); if (i === 3) { subject.complete(); } - }, error: (err: any) => { + }, + error: (err: any) => { done(new Error('should not be called')); - }, complete: () => { + }, + complete: () => { done(); - } } - ); + }, + }); }); it('should replay values and complete', (done) => { @@ -61,11 +63,12 @@ describe('ReplaySubject', () => { subject.next(2); subject.next(3); subject.complete(); - subject.subscribe( - { next: (x: number) => { + subject.subscribe({ + next: (x: number) => { expect(x).to.equal(expects[i++]); - }, complete: done } - ); + }, + complete: done, + }); }); it('should replay values and error', (done) => { @@ -76,14 +79,15 @@ describe('ReplaySubject', () => { subject.next(2); subject.next(3); subject.error('fooey'); - subject.subscribe( - { next: (x: number) => { + subject.subscribe({ + next: (x: number) => { expect(x).to.equal(expects[i++]); - }, error: (err: any) => { + }, + error: (err: any) => { expect(err).to.equal('fooey'); done(); - } } - ); + }, + }); }); it('should only replay values within its buffer size', (done) => { @@ -93,18 +97,20 @@ describe('ReplaySubject', () => { subject.next(1); subject.next(2); subject.next(3); - subject.subscribe( - { next: (x: number) => { + subject.subscribe({ + next: (x: number) => { expect(x).to.equal(expects[i++]); if (i === 2) { subject.complete(); } - }, error: (err: any) => { + }, + error: (err: any) => { done(new Error('should not be called')); - }, complete: () => { + }, + complete: () => { done(); - } } - ); + }, + }); }); describe('with bufferSize=2', () => { @@ -131,9 +137,9 @@ describe('ReplaySubject', () => { const subscriber3 = hot('---------------------------(c|) ').pipe(mergeMapTo(replaySubject)); const expected3 = ' ---------------------------(78)9--|'; - expectObservable(hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject }))).toBe( - sourceTemplate - ); + expectObservable( + hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject })) + ).toBe(sourceTemplate); expectObservable(subscriber1, unsub1).toBe(expected1); expectObservable(subscriber2, unsub2).toBe(expected2); expectObservable(subscriber3).toBe(expected3); @@ -157,9 +163,9 @@ describe('ReplaySubject', () => { const subscriber1 = hot('---------------(a|) ').pipe(mergeMapTo(replaySubject)); const expected1 = ' ---------------(34|)'; - expectObservable(hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject }))).toBe( - sourceTemplate - ); + expectObservable( + hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject })) + ).toBe(sourceTemplate); expectObservable(subscriber1).toBe(expected1); }); }); @@ -175,27 +181,31 @@ describe('ReplaySubject', () => { subject.next(3); subject.next(4); - const subscription1 = subject.subscribe( - { next: (x: number) => { + const subscription1 = subject.subscribe({ + next: (x: number) => { results1.push(x); - }, error: (err: any) => { + }, + error: (err: any) => { results1.push('E'); - }, complete: () => { + }, + complete: () => { results1.push('C'); - } } - ); + }, + }); subject.next(5); - const subscription2 = subject.subscribe( - { next: (x: number) => { + const subscription2 = subject.subscribe({ + next: (x: number) => { results2.push(x); - }, error: (err: any) => { + }, + error: (err: any) => { results2.push('E'); - }, complete: () => { + }, + complete: () => { results2.push('C'); - } } - ); + }, + }); subject.next(6); subject.next(7); @@ -209,15 +219,17 @@ describe('ReplaySubject', () => { subject.next(9); subject.next(10); - const subscription3 = subject.subscribe( - { next: (x: number) => { + const subscription3 = subject.subscribe({ + next: (x: number) => { results3.push(x); - }, error: (err: any) => { + }, + error: (err: any) => { results3.push('E'); - }, complete: () => { + }, + complete: () => { results3.push('C'); - } } - ); + }, + }); subject.next(11); @@ -255,9 +267,9 @@ describe('ReplaySubject', () => { const subscriber3 = hot('---------------------------(c|) ').pipe(mergeMapTo(replaySubject)); const expected3 = ' ---------------------------(78)9--|'; - expectObservable(hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject }))).toBe( - sourceTemplate - ); + expectObservable( + hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject })) + ).toBe(sourceTemplate); expectObservable(subscriber1, unsub1).toBe(expected1); expectObservable(subscriber2, unsub2).toBe(expected2); expectObservable(subscriber3).toBe(expected3); @@ -281,9 +293,9 @@ describe('ReplaySubject', () => { const subscriber1 = hot('-------------(a|)').pipe(mergeMapTo(replaySubject)); const expected1 = ' -------------(4|)'; - expectObservable(hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject }))).toBe( - sourceTemplate - ); + expectObservable( + hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject })) + ).toBe(sourceTemplate); expectObservable(subscriber1).toBe(expected1); }); }); @@ -305,9 +317,9 @@ describe('ReplaySubject', () => { const subscriber1 = hot('----(a|)').pipe(mergeMapTo(replaySubject)); const expected1 = ' ----(34)---|'; - expectObservable(hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject }))).toBe( - sourceTemplate - ); + expectObservable( + hot(sourceTemplate).pipe(tap({ next: feedNextIntoSubject, error: feedErrorIntoSubject, complete: feedCompleteIntoSubject })) + ).toBe(sourceTemplate); expectObservable(subscriber1).toBe(expected1); }); }); @@ -318,9 +330,7 @@ describe('ReplaySubject', () => { const subject = new ReplaySubject(3); let results: (number | string)[] = []; - subject.subscribe( - { next: (x) => results.push(x), complete: () => results.push('done') } - ); + subject.subscribe({ next: (x) => results.push(x), complete: () => results.push('done') }); source.subscribe(subject); @@ -328,9 +338,7 @@ describe('ReplaySubject', () => { results = []; - subject.subscribe( - { next: (x) => results.push(x), complete: () => results.push('done') } - ); + subject.subscribe({ next: (x) => results.push(x), complete: () => results.push('done') }); expect(results).to.deep.equal([3, 4, 5, 'done']); }); @@ -343,7 +351,7 @@ describe('ReplaySubject', () => { subject.complete(); subject.next(3); subject.subscribe({ - next: value => results.push(value), + next: (value) => results.push(value), complete: () => results.push('C'), }); expect(results).to.deep.equal([1, 2, 'C']); @@ -357,7 +365,7 @@ describe('ReplaySubject', () => { subject.error(new Error('Boom!')); subject.next(3); subject.subscribe({ - next: value => results.push(value), + next: (value) => results.push(value), error: () => results.push('E'), }); expect(results).to.deep.equal([1, 2, 'E']);