is-ready-effects.service.spec.ts
🧩 Syntax:
import { discardPeriodicTasks, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { NgxsModule, Store } from '@ngxs/store';
import { NgxsEffectsModule } from 'ngxs-effects';
import { of } from 'rxjs';
import { NgxsDispatchPluginModule } from '@ngxs-labs/dispatch-decorator';
import { DevicePosControllerService } from '@app/api/services/device-pos-controller.service';
import { IsReadyEffectsService } from './is-ready-effects.service';
import { DeviceEnrolmentState } from '../../device-enrolment/device-enrolment.state';
import { CheckIsReadyRequested, IsReadyCheckedSuccessfully } from '../device-qr-code.actions';
import {
REQUEST_POLLING_INTERVAL,
REQUEST_POLLING_TIMEOUT,
} from '@app/device-enrolment/store/device-qr-code/services/constants';
describe('IsReadyEffectsService', () => {
let service: IsReadyEffectsService;
let devicePOSControllerServiceMock;
let store: Store;
beforeEach(() => {
devicePOSControllerServiceMock = jasmine.createSpyObj('DevicePosControllerService', ['isReady$Response']);
TestBed.configureTestingModule({
imports: [
NgxsModule.forRoot([DeviceEnrolmentState]),
NgxsEffectsModule.forRoot(IsReadyEffectsService),
NgxsDispatchPluginModule.forRoot(),
],
providers: [{ provide: DevicePosControllerService, useValue: devicePOSControllerServiceMock }],
});
service = TestBed.inject(IsReadyEffectsService);
store = TestBed.inject(Store);
});
describe('creating an instance', () => {
it('should create service PreCheckStatusEffectsService', () => {
expect(service).toBeTruthy();
expect(service).toEqual(jasmine.any(IsReadyEffectsService));
});
});
describe('checkIsReadyRequestedEffect()', () => {
describe('subscription workflow', () => {
let effectSpy;
beforeEach(() => {
effectSpy = spyOn(service, 'checkIsReadyRequestedEffect');
});
it('should fire the effect for the event CheckIsReadyRequested', async () => {
const action = new CheckIsReadyRequested();
await store.dispatch(action).toPromise();
expect(effectSpy).toHaveBeenCalled();
});
});
describe('processing workflow', () => {
const imeiMock = '11111111';
let dispatchSpy;
beforeEach(() => {
dispatchSpy = spyOn(store, 'dispatch');
store.reset({ deviceEnrolment: { imei: imeiMock } });
});
it('should check device status by IMEI', fakeAsync(() => {
devicePOSControllerServiceMock.isReady$Response.and.returnValue(of({ body: { ready: true } }));
service.checkIsReadyRequestedEffect();
tick(REQUEST_POLLING_INTERVAL);
expect(devicePOSControllerServiceMock.isReady$Response).toHaveBeenCalledWith({ imei: imeiMock });
discardPeriodicTasks();
}));
it('should retry to check device status if the device is not ready', fakeAsync(() => {
devicePOSControllerServiceMock.isReady$Response.and.returnValue(of({ body: { ready: false } }));
service.checkIsReadyRequestedEffect();
tick(0);
expect(devicePOSControllerServiceMock.isReady$Response).toHaveBeenCalledTimes(1);
discardPeriodicTasks();
}));
it('should stop cheching device status if POS error is received', fakeAsync(() => {
devicePOSControllerServiceMock.isReady$Response.and.returnValue(of({ body: { ready: false } }));
service.setPosErrorEffect();
service.checkIsReadyRequestedEffect();
tick(0);
expect(devicePOSControllerServiceMock.isReady$Response).toHaveBeenCalledTimes(1);
discardPeriodicTasks();
}));
it('should stop checking device status if the device is ready', fakeAsync(() => {
devicePOSControllerServiceMock.isReady$Response.and.returnValue(of({ body: { ready: false } }));
service.checkIsReadyRequestedEffect();
tick(0);
expect(devicePOSControllerServiceMock.isReady$Response).toHaveBeenCalledTimes(1);
devicePOSControllerServiceMock.isReady$Response.and.returnValue(of({ body: { ready: true } }));
discardPeriodicTasks();
}));
it('should stop checking device status due to timeout', fakeAsync(() => {
devicePOSControllerServiceMock.isReady$Response.and.returnValue(of({ body: { ready: false } }));
service.checkIsReadyRequestedEffect();
tick(0);
expect(devicePOSControllerServiceMock.isReady$Response).toHaveBeenCalledTimes(1);
discardPeriodicTasks();
}));
it('should fire event IsReadyCheckedSuccessfully with true if the device is ready', fakeAsync(() => {
devicePOSControllerServiceMock.isReady$Response.and.returnValue(of({ body: { ready: true } }));
service.checkIsReadyRequestedEffect();
tick(0);
expect(dispatchSpy.calls.argsFor(0)).toEqual([new IsReadyCheckedSuccessfully(true)]);
discardPeriodicTasks();
}));
xit('should fire event IsReadyCheckedSuccessfully with false if the device is not ready', fakeAsync(() => {
// Since we have implemented 60 seconds timer to wait for device ready status after which we are redirecting to device page where we show device is registered but not ready, this test case won't be valid anymore
devicePOSControllerServiceMock.isReady$Response.and.returnValue(of({ body: { ready: false } }));
service.checkIsReadyRequestedEffect();
tick(REQUEST_POLLING_TIMEOUT);
expect(dispatchSpy.calls.argsFor(0)).toEqual([new IsReadyCheckedSuccessfully(false)]);
}));
xit('should fire event IsReadyCheckedSuccessfully with false if an empty result returned', fakeAsync(() => {
// Since we have implemented 60 seconds timer to wait for device ready status after which we are redirecting to device page where we show device is registered but not ready, this test case won't be valid anymore
devicePOSControllerServiceMock.isReady$Response.and.returnValue(of({ body: {} }));
service.checkIsReadyRequestedEffect();
tick(REQUEST_POLLING_TIMEOUT);
expect(dispatchSpy.calls.argsFor(0)).toEqual([new IsReadyCheckedSuccessfully(false)]);
}));
it('should fire event IsReadyCheckedSuccessfully once if the device is not ready', fakeAsync(() => {
devicePOSControllerServiceMock.isReady$Response.and.returnValue(of({ body: { ready: false } }));
service.checkIsReadyRequestedEffect();
tick(REQUEST_POLLING_TIMEOUT);
expect(dispatchSpy).toHaveBeenCalledTimes(1);
}));
it('should fire event IsReadyCheckedSuccessfully once if the device is ready', fakeAsync(() => {
devicePOSControllerServiceMock.isReady$Response.and.returnValue(of({ body: { ready: true } }));
service.checkIsReadyRequestedEffect();
tick(REQUEST_POLLING_TIMEOUT);
expect(dispatchSpy).toHaveBeenCalledTimes(1);
}));
});
});
});svinay1986
Member