Skip to content
Snippets Groups Projects
Commit e2e13715 authored by Tifenn Guillas's avatar Tifenn Guillas
Browse files

Tests on samp => DONE

parent 51816c4b
No related branches found
No related tags found
2 merge requests!29Develop,!8Resolve "Add tests for instance store module"
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { EffectsMetadata, getEffectsMetadata } from '@ngrx/effects';
import { Observable } from 'rxjs';
import { cold, hot } from 'jasmine-marbles';
import { ToastrService } from 'ngx-toastr';
import { SampEffects } from './samp.effects';
import { SampService } from '../services/samp.service';
import * as sampActions from '../actions/samp.actions';
describe('SampEffects', () => {
let actions = new Observable();
let effects: SampEffects;
let metadata: EffectsMetadata<SampEffects>;
let sampService: SampService;
let toastr: ToastrService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
SampEffects,
{ provide: SampService, useValue: {
register: jest.fn(),
unregister: jest.fn(),
broadcast: jest.fn()
}},
{ provide: ToastrService, useValue: {
success: jest.fn(),
error: jest.fn()
}},
provideMockActions(() => actions)
]
}).compileComponents();
effects = TestBed.inject(SampEffects);
metadata = getEffectsMetadata(effects);
sampService = TestBed.inject(SampService);
toastr = TestBed.inject(ToastrService);
});
it('should be created', () => {
expect(effects).toBeTruthy();
});
describe('register$ effect', () => {
it('should dispatch the registerSuccess action on success', () => {
const action = sampActions.register();
const outcome = sampActions.registerSuccess();
actions = hot('-a', { a: action });
const response = cold('-a|', { a: action });
const expected = cold('--b', { b: outcome });
sampService.register = jest.fn(() => response);
expect(effects.register$).toBeObservable(expected);
});
it('should dispatch the registerFail action on failure', () => {
const action = sampActions.register();
const error = new Error();
const outcome = sampActions.registerFail();
actions = hot('-a', { a: action });
const response = cold('-#|', {}, error);
const expected = cold('--b', { b: outcome });
sampService.register = jest.fn(() => response);
expect(effects.register$).toBeObservable(expected);
});
});
describe('registerSuccess$ effect', () => {
it('should not dispatch', () => {
expect(metadata.registerSuccess$).toEqual(
expect.objectContaining({ dispatch: false })
);
});
it('should display a success notification', () => {
const spy = jest.spyOn(toastr, 'success');
const action = sampActions.registerSuccess();
actions = hot('a', { a: action });
const expected = cold('a', { a: action });
expect(effects.registerSuccess$).toBeObservable(expected);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('You are now connected to a SAMP-hub', 'SAMP-hub register success');
});
});
describe('registerFail$ effect', () => {
it('should not dispatch', () => {
expect(metadata.registerFail$).toEqual(
expect.objectContaining({ dispatch: false })
);
});
it('should display a error notification', () => {
const spy = jest.spyOn(toastr, 'error');
const action = sampActions.registerFail();
actions = hot('a', { a: action });
const expected = cold('a', { a: action });
expect(effects.registerFail$).toBeObservable(expected);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('Connection to a SAMP-hub has failed', 'SAMP-hub register fail');
});
});
describe('unregister$ effect', () => {
it('should not dispatch', () => {
expect(metadata.registerFail$).toEqual(
expect.objectContaining({ dispatch: false })
);
});
it('should call unregister from sampService', () => {
const spy = jest.spyOn(sampService, 'unregister');
const action = sampActions.unregister();
actions = hot('a', { a: action });
const expected = cold('a', { a: action });
expect(effects.unregister$).toBeObservable(expected);
expect(spy).toHaveBeenCalledTimes(1);
});
});
describe('broadcastVotable$ effect', () => {
it('should not dispatch', () => {
expect(metadata.registerFail$).toEqual(
expect.objectContaining({ dispatch: false })
);
});
it('should call broadcast from sampService', () => {
const spy = jest.spyOn(sampService, 'broadcast');
const action = sampActions.broadcastVotable({ url: 'url' });
actions = hot('a', { a: action });
const expected = cold('a', { a: action });
expect(effects.broadcastVotable$).toBeObservable(expected);
expect(spy).toHaveBeenCalledTimes(1);
});
});
});
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
*/ */
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects'; import { Actions, createEffect, ofType } from '@ngrx/effects';
import { of } from 'rxjs'; import { of } from 'rxjs';
import { map, tap, mergeMap, catchError } from 'rxjs/operators'; import { map, tap, mergeMap, catchError } from 'rxjs/operators';
...@@ -15,10 +16,18 @@ import { ToastrService } from 'ngx-toastr'; ...@@ -15,10 +16,18 @@ import { ToastrService } from 'ngx-toastr';
import { SampService } from '../services/samp.service'; import { SampService } from '../services/samp.service';
import * as sampActions from '../actions/samp.actions'; import * as sampActions from '../actions/samp.actions';
/**
* @class
* @classdesc Samp effects.
*/
@Injectable() @Injectable()
export class SampEffects { export class SampEffects {
register$ = createEffect(() =>
/**
* Calls actions to register.
*/
register$ = createEffect((): any =>
this.actions$.pipe( this.actions$.pipe(
ofType(sampActions.register), ofType(sampActions.register),
mergeMap(() => this.sampService.register() mergeMap(() => this.sampService.register()
...@@ -30,6 +39,9 @@ export class SampEffects { ...@@ -30,6 +39,9 @@ export class SampEffects {
) )
); );
/**
* Displays register success notification.
*/
registerSuccess$ = createEffect(() => registerSuccess$ = createEffect(() =>
this.actions$.pipe( this.actions$.pipe(
ofType(sampActions.registerSuccess), ofType(sampActions.registerSuccess),
...@@ -38,6 +50,9 @@ export class SampEffects { ...@@ -38,6 +50,9 @@ export class SampEffects {
{ dispatch: false } { dispatch: false }
); );
/**
* Displays register error notification.
*/
registerFail$ = createEffect(() => registerFail$ = createEffect(() =>
this.actions$.pipe( this.actions$.pipe(
ofType(sampActions.registerFail), ofType(sampActions.registerFail),
...@@ -46,6 +61,9 @@ export class SampEffects { ...@@ -46,6 +61,9 @@ export class SampEffects {
{ dispatch: false } { dispatch: false }
); );
/**
* Calls actions to disconnect.
*/
unregister$ = createEffect(() => unregister$ = createEffect(() =>
this.actions$.pipe( this.actions$.pipe(
ofType(sampActions.unregister), ofType(sampActions.unregister),
...@@ -56,6 +74,9 @@ export class SampEffects { ...@@ -56,6 +74,9 @@ export class SampEffects {
{ dispatch: false } { dispatch: false }
); );
/**
* Calls actions to broadcast.
*/
broadcastVotable$ = createEffect(() => broadcastVotable$ = createEffect(() =>
this.actions$.pipe( this.actions$.pipe(
ofType(sampActions.broadcastVotable), ofType(sampActions.broadcastVotable),
......
...@@ -15,11 +15,11 @@ import { AppConfigService } from 'src/app/app-config.service'; ...@@ -15,11 +15,11 @@ import { AppConfigService } from 'src/app/app-config.service';
declare var samp: any; declare var samp: any;
@Injectable()
/** /**
* @class * @class
* @classdesc Samp service. * @classdesc Samp service.
*/ */
@Injectable()
export class SampService { export class SampService {
private connector = null; private connector = null;
...@@ -33,10 +33,15 @@ export class SampService { ...@@ -33,10 +33,15 @@ export class SampService {
"home.page": "https://anis.lam.fr", "home.page": "https://anis.lam.fr",
"samp.icon.url": baseUrl + "/assets/cesam_anis40.png" "samp.icon.url": baseUrl + "/assets/cesam_anis40.png"
}; };
this.connector = new samp.Connector("anis-client", meta) this.connector = new samp.Connector("anis-client", meta);
} }
register() { /**
* Register to Samp.
*
* @return Observable<any>
*/
register(): Observable<any> {
return new Observable(observer => { return new Observable(observer => {
samp.register(this.connector.name, (conn) => { samp.register(this.connector.name, (conn) => {
this.connector.setConnection(conn); this.connector.setConnection(conn);
...@@ -49,10 +54,21 @@ export class SampService { ...@@ -49,10 +54,21 @@ export class SampService {
}); });
} }
/**
* Disconnect to Samp.
*
* @return Observable<any>
*/
unregister(): void { unregister(): void {
this.connector.unregister(); this.connector.unregister();
} }
/**
* Disconnect to Samp.
*
* @param {string} mtype - Message type.
* @param {string} url - URL.
*/
broadcast(mtype: string, url: string): void { broadcast(mtype: string, url: string): void {
const message = new samp.Message(mtype, {"url": encodeURI(url)}); const message = new samp.Message(mtype, {"url": encodeURI(url)});
this.connector.connection.notifyAll([message]); this.connector.connection.notifyAll([message]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment