How can I unit test an RxJS effect that uses a withLatestFrom operator to call another service

I have an RxJs effect that uses a withLatestFrom operator, within my action pipe, to retrieve some state from the store. When it comes to unit testing, I’m getting the following error concerning undefined where stream was expected so clearly my observables are not set up correctly in the test. The code works as expected in System Testing.

Here’s the error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Expected $[0].notification.kind = 'E' to equal 'N'.
Expected $[0].notification.value = undefined to equal Object({ type: '[Workplan Validation] Workplan Validation Completed' }).
Expected $[0].notification.error = TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable. to equal undefined.
</code>
<code>Expected $[0].notification.kind = 'E' to equal 'N'. Expected $[0].notification.value = undefined to equal Object({ type: '[Workplan Validation] Workplan Validation Completed' }). Expected $[0].notification.error = TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable. to equal undefined. </code>
Expected $[0].notification.kind = 'E' to equal 'N'.
Expected $[0].notification.value = undefined to equal Object({ type: '[Workplan Validation] Workplan Validation Completed' }).
Expected $[0].notification.error = TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable. to equal undefined.

Here is the effect code, edited:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>create$ = createEffect(() => this._actions$.pipe(
ofType(WorkplanValidationActions.ValidateAnomalyMediaCompletedAction),
withLatestFrom(this._storeSelector.get()),
mergeMap(([a, s]) => {
// cut out stuff just to show the unit test fails on this simple example
return of(WorkplanValidationActions.WorkplanValidationCompletedAction());
})
));
</code>
<code>create$ = createEffect(() => this._actions$.pipe( ofType(WorkplanValidationActions.ValidateAnomalyMediaCompletedAction), withLatestFrom(this._storeSelector.get()), mergeMap(([a, s]) => { // cut out stuff just to show the unit test fails on this simple example return of(WorkplanValidationActions.WorkplanValidationCompletedAction()); }) )); </code>
create$ = createEffect(() => this._actions$.pipe(
    ofType(WorkplanValidationActions.ValidateAnomalyMediaCompletedAction),
    withLatestFrom(this._storeSelector.get()),
    mergeMap(([a, s]) => {
        // cut out stuff just to show the unit test fails on this simple example
        return of(WorkplanValidationActions.WorkplanValidationCompletedAction());
    })
));

Here is the unit test:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>it('GIVEN anomaly media to validate, WHEN all anomaly media has been validated, THEN next anomaly should be validated and event fired ', () => {
//arrange
const anomalyNumber1 = 'MTL-401-XXX';
const anomalyNumber2 = 'MTL-401-YYY';
const validationResult = {anomalyNumber: anomalyNumber2} as undefined as AnomalyMediaValidationResult;
const validationState = {
validationResults: {
workplanTaskFindingsValidationResults: [
{anomalyNumber: anomalyNumber1} as undefined as AnomalyValidationResult,
{anomalyNumber: anomalyNumber2} as undefined as AnomalyValidationResult
],
anomalyMediaValidationResults: [
{anomalyNumber: anomalyNumber1} as undefined as AnomalyMediaValidationResult,
{anomalyNumber: anomalyNumber2} as undefined as AnomalyMediaValidationResult
]
}
} as undefined as WorkplanValidationState;
const action = ValidateAnomalyMediaCompletedAction({ validationResult });
const completion = WorkplanValidationCompletedAction();
actions$ = hot('a', {a: action});
const stateResponse = cold('a|', { a: validationState });
getSelectorService.get.and.returnValue(stateResponse);
const expected = cold('b', {b: completion});
//act & assert
expect(effect.create$).toBeObservable(expected);
expect(service.validateAnomalyMedia$).not.toHaveBeenCalled();
});
</code>
<code>it('GIVEN anomaly media to validate, WHEN all anomaly media has been validated, THEN next anomaly should be validated and event fired ', () => { //arrange const anomalyNumber1 = 'MTL-401-XXX'; const anomalyNumber2 = 'MTL-401-YYY'; const validationResult = {anomalyNumber: anomalyNumber2} as undefined as AnomalyMediaValidationResult; const validationState = { validationResults: { workplanTaskFindingsValidationResults: [ {anomalyNumber: anomalyNumber1} as undefined as AnomalyValidationResult, {anomalyNumber: anomalyNumber2} as undefined as AnomalyValidationResult ], anomalyMediaValidationResults: [ {anomalyNumber: anomalyNumber1} as undefined as AnomalyMediaValidationResult, {anomalyNumber: anomalyNumber2} as undefined as AnomalyMediaValidationResult ] } } as undefined as WorkplanValidationState; const action = ValidateAnomalyMediaCompletedAction({ validationResult }); const completion = WorkplanValidationCompletedAction(); actions$ = hot('a', {a: action}); const stateResponse = cold('a|', { a: validationState }); getSelectorService.get.and.returnValue(stateResponse); const expected = cold('b', {b: completion}); //act & assert expect(effect.create$).toBeObservable(expected); expect(service.validateAnomalyMedia$).not.toHaveBeenCalled(); }); </code>
it('GIVEN anomaly media to validate, WHEN all anomaly media has been validated, THEN next anomaly should be validated and event fired ', () => {

        //arrange
        const anomalyNumber1 = 'MTL-401-XXX';
        const anomalyNumber2 = 'MTL-401-YYY';
        const validationResult = {anomalyNumber: anomalyNumber2} as undefined as AnomalyMediaValidationResult;
        const validationState = {
            validationResults: {
                workplanTaskFindingsValidationResults: [
                    {anomalyNumber: anomalyNumber1} as undefined as AnomalyValidationResult,
                    {anomalyNumber: anomalyNumber2} as undefined as AnomalyValidationResult
                ],
                anomalyMediaValidationResults: [
                    {anomalyNumber: anomalyNumber1} as undefined as AnomalyMediaValidationResult,
                    {anomalyNumber: anomalyNumber2} as undefined as AnomalyMediaValidationResult
                ]
            }
        } as undefined as WorkplanValidationState;

        const action = ValidateAnomalyMediaCompletedAction({ validationResult });
        const completion = WorkplanValidationCompletedAction();

        actions$ = hot('a', {a: action});
        const stateResponse = cold('a|', { a: validationState });
        getSelectorService.get.and.returnValue(stateResponse);

        const expected = cold('b', {b: completion});

        //act & assert
        expect(effect.create$).toBeObservable(expected);
        expect(service.validateAnomalyMedia$).not.toHaveBeenCalled();
    });

Things to note:

  1. The injected _storeSelector is a wrapper service around the store and allows me to mock its calls without using the mockStore. Therefore, I believe, I am not dependent on setting any store state as this could be any other service fetching data?
  2. If I remove the withLatestFrom operator and shrink the mergemap to just one param, I can remove the marble for the selector service and the unit test passes.
  3. If I add frames into the calls (i.e. -a) , it results in an extra error about frame 0 not equal to 20.

Any help in getting a better understanding of observables would be very much appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật