stomp-js / rx-stomp

STOMP adaptor for RxJS
Apache License 2.0
112 stars 21 forks source link

Testing ng2-stompjs with Jest #404

Closed itrash0505 closed 2 years ago

itrash0505 commented 2 years ago
import { async, ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { RxStompRPCService, RxStompService, rxStompServiceFactory } from '@stomp/ng2-stompjs';
import { myRxStompConfig } from '../src/app/config/stomp.config';
import { DocksService } from '../src/app/update-tool/docks.service';
import { UpdateToolComponent } from '../src/app/update-tool/update-tool.component';

// jest.mock('../src/app/update-tool/docks.service');
describe('UpdateToolComponent', () => {
    let component: UpdateToolComponent;
    let fixture: ComponentFixture<UpdateToolComponent>;
    let service: DocksService;
    let dialogMock: any;
    let dockServiceMock: DocksService;
    let spinnerMock: any;

    beforeEach(() => {
        dockServiceMock = new DocksService(new RxStompService());
        component = new UpdateToolComponent(dialogMock, dockServiceMock, spinnerMock);
    });

    describe('Setup Component',()=>{
        describe('ngOnInit',()=>{
            it('Should be created and fetch the list of safe docks ',()=>
            {
                const getConfigSpy = jest.spyOn (component,'getSafedocks');
                component.ngOnInit();
                expect(getConfigSpy).toBeCalled();
            });
        });
    });
    it('should create', () => {
        expect(component).toBeTruthy();
    });
});

Getting this error when writing Jest Test

Class constructor RxStomp cannot be invoked without 'new'
kum-deepak commented 2 years ago

Which version of the library are you using?

itrash0505 commented 2 years ago

ng2-stompjs@8.0.0

 "@angular/cli": "12.1.0",
 "@angular/compiler-cli": "12.1.0",
 "@stomp/ng2-stompjs": "8.0.0",
 "jest": "^27.0.1",
kum-deepak commented 2 years ago

This error, usually, happens if the underlying library was compiled to an ES version that did not support classes. The ng2-stompjs package is bundled by a very old version of https://github.com/ng-packagr/ng-packagr to support Angular 6+. Unfortunately that causes issues in a few combinations in newer setups. The way to solve this issue fundamentally is to use newer tool chain to build ng2-stompjs, however, that causes this library to fail with older Angular versions.

To minimize the impact, few years back, I extracted majority of the functionality to another package @stomp/rx-stomp. At this stage the @stomp/ng2-stompjs became a very thin Angular specific wrapper, for example https://github.com/stomp-js/ng2-stompjs/blob/develop/src/app/rx-stomp.service.ts

In your case you are not injecting the RxStompService class, so, without any loss of functionality, you should be able to use @stomp/rx-stomp. See if you can do that and whether that resolves the issue.

itrash0505 commented 2 years ago

i am using RPC , replacing the library will work ?

const rxStompRPC = new RxStompRPC(this.rxStomp),
            correlationId = utils.getCorellationId();

        const resp = await rxStompRPC
                .rpc({
                    destination: config.mq,
                    body: JSON.stringify(command),
                    headers: {
                        'correlation-id': correlationId,
                        exclusive: 'false',
                        'auto-delete': 'true',
                    },
                })
                .toPromise()
                .then((x) => x),
            respCorrelationId = resp.headers['correlation-id'];
        if (respCorrelationId === correlationId) {
            return JSON.parse(resp.body).data;
        }
kum-deepak commented 2 years ago

The above should work.

The library generates and matches the correlationId, so, your code can be much simpler. Please check the guide at https://stomp-js.github.io/guide/rx-stomp/ng2-stompjs/remote-procedure-call.html

itrash0505 commented 2 years ago

I am now using the@stomp/rx-stomp library, and able to create the instances in the unit test. I have one more question, is there any library which i can use to mockup the data response against the rpc request