Я создал новый оператор RxJs (6) switchIfEmpty (other), он переключается на other
, когда источник пуст.
Я новичок в мраморных тестах, я создал несколько тестовдля проверки моего оператора все тесты проходят, кроме одного.я не знаю, были ли ошибки от оператора или теста!
import { Observable } from 'rxjs';
export const switchIfEmpty = <T, R>(emptySource: Observable<R>) =>
(source: Observable<T>) => {
return new Observable<T | R>(subscriber => {
let empty = true;
const subscription = source.subscribe(
e => {
empty = false;
subscriber.next(e);
},
error => subscriber.error(error),
() => {
if (empty) {
const innerSub = emptySource.subscribe(
innerE => subscriber.next(innerE),
error => subscriber.error(error),
() => subscriber.complete()
);
subscription.add(innerSub);
} else {
subscriber.complete();
}
}
);
return subscription;
});
};
спецификация теста:
import { TestScheduler } from 'rxjs/testing';
import { of, throwError } from 'rxjs';
import { switchIfEmpty } from './switch-empty';
import { delay } from 'rxjs/operators';
describe('SWITCH IF EMPTY OPERATOR', () => {
let scheduler: TestScheduler;
beforeEach(() => {
scheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
});
it('switchIfEmpty : source not empty', () => {
const source = '-x-y-z|';
const expected = '-x-y-z|';
scheduler.run(helper => {
const source$ = scheduler.createColdObservable(source);
helper.expectObservable(source$.pipe(switchIfEmpty(of(1)))).toBe(expected);
});
});
it('switchIfEmpty : source empty', () => {
const source = '|';
const expected = '(1 a|)';
scheduler.run(helper => {
const source$ = scheduler.createColdObservable(source);
helper.expectObservable(source$.pipe(switchIfEmpty(of('1', 'a')))).toBe(expected);
});
});
it('switchIfEmpty : source error', () => {
const source = '-x#';
const expected = '-x#';
scheduler.run(helper => {
const source$ = scheduler.createColdObservable(source);
helper.expectObservable(source$.pipe(switchIfEmpty(of(1)))).toBe(expected);
});
});
it('switchIfEmpty : empty source switched to error', () => {
const source = '|';
const expected = '#';
scheduler.run(helper => {
const source$ = scheduler.createColdObservable(source);
helper.expectObservable(source$.pipe(switchIfEmpty(throwError('error')))).toBe(expected);
});
});
it('switchIfEmpty : empty source switched to delay', () => {
const source = '|';
const expected = '--x|'; // tried '--(x|)' not work
scheduler.run(helper => {
const source$ = scheduler.createColdObservable(source);
const switchedSource = of(1).pipe(delay(20));
helper.expectObservable(source$.pipe(switchIfEmpty(switchedSource))).toBe(expected, {x: 1});
});
});
});
только последняя не работает:
it('switchIfEmpty : empty source switched to delay', () => {
const source = '|';
const expected = '--x|'; // tried '--(x|)' not work
scheduler.run(helper => {
const source$ = scheduler.createColdObservable(source);
const switchedSource = of(1).pipe(delay(20));
helper.expectObservable(source$.pipe(switchIfEmpty(switchedSource))).toBe(expected, {x: 1});
});
});
сообщение об ошибке
SWITCH IF EMPTY OPERATOR switchIfEmpty : empty source switched to delay FAILED
Expected $.length = 0 to equal 2.
Expected $[0] = undefined to equal Object({ frame: 2, notification: Notification({ kind: 'N', value: 1, error: undefined, hasValue: true }) }).
Expected $[1] = undefined to equal Object({ frame: 3, notification: Notification({ kind: 'C', value: undefined, error: undefined, hasValue: false }) }).
at TestScheduler.assertDeepEqual (http://localhost:9876/src/switch-empty.spec.ts?:12:22)
at http://localhost:9876/node_modules/rxjs/_esm5/internal/testing/TestScheduler.js?:122:1
at Array.filter (<anonymous>)
at TestScheduler.push../node_modules/rxjs/_esm5/internal/testing/TestScheduler.js.TestScheduler.flush (http://localhost:9876/node_modules/rxjs/_esm5/internal/testing/TestScheduler.js?:120:1)