【发布时间】:2020-07-27 09:41:17
【问题描述】:
我尝试使用大理石测试来测试一个简单的角度组件。为此,我使用了与 rxjs 一起提供的 TestScheduler。
这是一个包含代码的 stackblitz 链接:https://stackblitz.com/edit/angular-ivy-xwzn1z
这是我的组件的简化版本:
@Component({
selector: 'detail-component',
template: ` <ng-container *ngIf="(detailsVisible$ | async)"> <p> detail visible </p> </ng-container>`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DetailComponent implements OnInit, OnDestroy {
@Input() set isAdditionalContentVisible(isAdditionalContentVisible: boolean) {
this.resetSubject.next(isAdditionalContentVisible);
}
private readonly resetSubject = new Subject<boolean>();
private readonly toggleVisibilitySubject = new Subject<void>();
private readonly destroySubject = new Subject();
public detailsVisible$: Observable<boolean> = this.toggleVisibilitySubject.pipe(
scan((state, _) => !state, false),
startWith(false)
);
private readonly resetDetailsVisibilitySideEffect$: Observable<void> = this.resetSubject.asObservable().pipe(
withLatestFrom(this.detailsVisible$),
map(([resetTrigger, state]) => {
if (state !== resetTrigger) {
this.toggleVisibilitySubject.next();
}
})
);
constructor() {}
ngOnInit(): void {
this.resetDetailsVisibilitySideEffect$.pipe(takeUntil(this.destroySubject)).subscribe();
}
ngOnDestroy(): void {
this.destroySubject.next();
this.destroySubject.complete();
}
toggleAdditionalContentVisibility(): void {
this.toggleVisibilitySubject.next();
}
}
我想测试detailsVisible$-observable。
为此,我创建了以下测试:
import { TestScheduler } from 'rxjs/testing';
describe('DetailComponent', () => {
const debug = true;
let scheduler: TestScheduler;
let component: DetailComponent;
beforeEach(() => {
component = new DetailComponent();
scheduler = new TestScheduler((actual, expected) => {
// asserting the two objects are equal
if (debug) {
console.log('-------------------------------');
console.log('Expected:\n' + JSON.stringify(expected, null, 2));
console.log('Actual:\n' + JSON.stringify(actual, null, 2));
}
expect(actual).toEqual(expected);
});
});
it('should finally work out', () => {
scheduler.run((helpers) => {
const { cold, hot, expectObservable, expectSubscriptions } = helpers;
const values = {
f: false,
t: true
};
const toggleVisibilityValues = {
v: void 0
};
const resetValues = {
f: false,
t: true
};
component.ngOnInit();
// marbles
// prettier-ignore
const detailsVisibleMarble = 'f-t-f-t-f-t-f';
// prettier-ignore
const toggleVisibilityMarble = '--v-v-----v--';
// prettier-ignore
const resetMarble = '------t-f---f';
// Mock observables
(component as any).toggleVisibilitySubject = cold(toggleVisibilityMarble,toggleVisibilityValues);
(component as any).resetSubject = cold(resetMarble, resetValues);
// output
expectObservable(component.detailsVisible$).toBe(detailsVisibleMarble, values);
});
});
});
我尝试了几件事,但都导致以下输出:
Expected $.length = 1 to equal 7.
Expected $[1] = undefined to equal Object({ frame: 2, notification: Notification({ kind: 'N', value: true, error: undefined, hasValue: true }) }).
Expected $[2] = undefined to equal Object({ frame: 4, notification: Notification({ kind: 'N', value: false, error: undefined, hasValue: true }) }).
Expected $[3] = undefined to equal Object({ frame: 6, notification: Notification({ kind: 'N', value: true, error: undefined, hasValue: true }) }).
Expected $[4] = undefined to equal Object({ frame: 8, notification: Notification({ kind: 'N', value: false, error: undefined, hasValue: true }) }).
Expected $[5] = undefined to equal Object({ frame: 10, notification: Notification({ kind: 'N', value: true, error: undefined, hasValue: true }) }).
Expected $[6] = undefined to equal Object({ frame: 12, notification: Notification({ kind: 'N', value: false, error: undefined, hasValue: true }) }).
<Jasmine>
所以不知何故,detailsVisible$ (toggleVisibilitySubject) 的来源永远不会发出任何值(我只在结果中得到startWith-值)。
我看不到我缺少什么。代码本身运行良好。
感谢您的任何建议。
编辑: 我也试过了
toggle$ = this.toggleVisibilitySubject.asObservable();
public detailsVisible$ = this.toggle.pipe(...)
并在测试中:component.toggle$ =cold(toggleVisibilityMarble,toggleVisibilityValues)。
【问题讨论】:
标签: angular rxjs karma-jasmine rxjs-test-scheduler