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

chore(fromEventPattern): convert fromEventPattern specs to run mode #6958

Merged
merged 1 commit into from Sep 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 48 additions & 30 deletions spec/observables/fromEventPattern-spec.ts
@@ -1,26 +1,33 @@
/** @prettier */
import { expect } from 'chai';
import * as sinon from 'sinon';
import { expectObservable } from '../helpers/marble-testing';

import { fromEventPattern, noop, NEVER, timer } from 'rxjs';
import { mapTo, take, concat } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';

declare const rxTestScheduler: TestScheduler;
import { observableMatcher } from '../helpers/observableMatcher';

/** @test {fromEventPattern} */
describe('fromEventPattern', () => {
let rxTestScheduler: TestScheduler;

beforeEach(() => {
rxTestScheduler = new TestScheduler(observableMatcher);
});

it('should create an observable from the handler API', () => {
function addHandler(h: any) {
timer(50, 20, rxTestScheduler).pipe(
mapTo('ev'),
take(2),
concat(NEVER)
).subscribe(h);
}
const e1 = fromEventPattern(addHandler);
const expected = '-----x-x---';
expectObservable(e1).toBe(expected, {x: 'ev'});
rxTestScheduler.run(({ time, expectObservable }) => {
const time1 = time('-----| ');
const time2 = time(' --| ');
const expected = ' -----x-x---';

function addHandler(h: any) {
timer(time1, time2, rxTestScheduler).pipe(mapTo('ev'), take(2), concat(NEVER)).subscribe(h);
}
const e1 = fromEventPattern(addHandler);

expectObservable(e1).toBe(expected, { x: 'ev' });
});
});

it('should call addHandler on subscription', () => {
Expand Down Expand Up @@ -50,7 +57,7 @@ describe('fromEventPattern', () => {
});

it('should deliver return value of addHandler to removeHandler as signal', () => {
const expected = { signal: true};
const expected = { signal: true };
const addHandler = () => expected;
const removeHandler = sinon.spy();
fromEventPattern(addHandler, removeHandler).subscribe(noop).unsubscribe();
Expand All @@ -62,11 +69,14 @@ describe('fromEventPattern', () => {
it('should send errors in addHandler down the error path', (done) => {
fromEventPattern((h: any) => {
throw 'bad';
}, noop).subscribe(
{ next: () => done(new Error('should not be called')), error: (err: any) => {
}, noop).subscribe({
next: () => done(new Error('should not be called')),
error: (err: any) => {
expect(err).to.equal('bad');
done();
}, complete: () => done(new Error('should not be called')) });
},
complete: () => done(new Error('should not be called')),
});
});

it('should accept a selector that maps outgoing values', (done) => {
Expand All @@ -87,14 +97,19 @@ describe('fromEventPattern', () => {
return a + b + '!';
};

fromEventPattern(addHandler, removeHandler, selector).pipe(take(1))
.subscribe({ next: (x: any) => {
expect(x).to.equal('testme!');
}, error: (err: any) => {
done(new Error('should not be called'));
}, complete: () => {
done();
} });
fromEventPattern(addHandler, removeHandler, selector)
.pipe(take(1))
.subscribe({
next: (x: any) => {
expect(x).to.equal('testme!');
},
error: (err: any) => {
done(new Error('should not be called'));
},
complete: () => {
done();
},
});

trigger('test', 'me');
});
Expand All @@ -117,15 +132,18 @@ describe('fromEventPattern', () => {
throw 'bad';
};

fromEventPattern(addHandler, removeHandler, selector)
.subscribe({ next: (x: any) => {
fromEventPattern(addHandler, removeHandler, selector).subscribe({
next: (x: any) => {
done(new Error('should not be called'));
}, error: (err: any) => {
},
error: (err: any) => {
expect(err).to.equal('bad');
done();
}, complete: () => {
},
complete: () => {
done(new Error('should not be called'));
} });
},
});

trigger('test');
});
Expand Down