Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { HttpClient } from '@angular/common/http';
- import { Injectable } from '@angular/core';
- import { timer, Observable, BehaviorSubject } from 'rxjs';
- import { includes, isEqual } from 'lodash';
- @Injectable({
- providedIn: 'root'
- })
- export class CardService {
- private pollInstance: Observable<number>;
- private card: IContactlessCard = undefined;
- private pollingEnabled = true;
- public listen: BehaviorSubject<any> = new BehaviorSubject(undefined);
- constructor(private http: HttpClient) {
- this.pollInstance = timer(1000);
- }
- private handleCardRemovedEvent = () => {
- this.card = undefined;
- console.log('Card removed from reader');
- }
- private handleCardDetectedEvent = (response) => {
- console.log('Card detected');
- this.card = response;
- this.listen.next({
- card: this.card
- });
- }
- private handleCardStilPresentEvent = () => {
- console.log('Card still present');
- }
- private restartPoll = () => {
- if (this.pollingEnabled) {
- this.pollInstance.subscribe(() => {
- this.pollCardStatus();
- });
- }
- }
- public pollCardStatus = () => {
- console.log('pollCardStatus...');
- this.pollingEnabled = true;
- this.http.get('http://someendpoint:1234/card').subscribe((response: any) => {
- if (!response) {
- if (this.card) {
- this.handleCardRemovedEvent();
- }
- this.restartPoll();
- return;
- }
- if (!isEqual(this.card, response)) {
- this.handleCardDetectedEvent(response);
- } else {
- this.handleCardStilPresentEvent();
- }
- this.restartPoll();
- }, (error) => {
- this.restartPoll();
- });
- }
- public stopPolling = () => {
- console.log('Card polling cancelled');
- this.pollingEnabled = false;
- }
- }
- import { CardService } from './card.service';
- import { TestBed, getTestBed, fakeAsync, tick } from '@angular/core/testing';
- import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
- describe('CardService Tests:', () => {
- let cardService: CardService;
- let injector: TestBed;
- let httpMock: HttpTestingController;
- let req;
- const mockCardResponse = {
- 'type': 'SOME_CARD',
- 'uid': 'xxx-111-222-ZZZ'
- };
- beforeEach(() => {
- TestBed.configureTestingModule({
- imports: [HttpClientTestingModule],
- providers: [CardService]
- });
- injector = getTestBed();
- cardService = injector.get(CardService);
- httpMock = injector.get(HttpTestingController);
- });
- afterEach(() => {
- httpMock.verify();
- });
- describe('when polling for card status changes', () => {
- beforeEach(() => {
- cardService.pollCardStatus();
- req = httpMock.expectOne('http://someendpoint:1234/card');
- expect(req.request.method).toBe('GET');
- });
- describe('and a new card is detected', () => {
- beforeEach(() => {
- req.flush(mockCardResponse);
- });
- it('should be able inspect the card details from the card service listen subject', () => {
- cardService.listen.subscribe((cardEvent) => {
- expect(cardEvent.card).toBe(mockCardResponse);
- });
- });
- });
- });
- });
- import { CardService } from './card.service';
- import { TestBed, getTestBed, fakeAsync, tick } from '@angular/core/testing';
- import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
- describe('CardService Tests:', () => {
- let cardService: CardService;
- let injector: TestBed;
- let httpMock: HttpTestingController;
- let req;
- const mockCardResponse = {
- 'type': 'SOME_CARD',
- 'uid': 'xxx-111-222-ZZZ'
- };
- beforeEach(() => {
- TestBed.configureTestingModule({
- imports: [HttpClientTestingModule],
- providers: [CardService]
- });
- injector = getTestBed();
- cardService = injector.get(CardService);
- httpMock = injector.get(HttpTestingController);
- });
- afterEach(() => {
- httpMock.verify();
- });
- describe('when polling for card status changes', () => {
- beforeEach(() => {
- cardService.pollCardStatus();
- req = httpMock.expectOne('http://someendpoint:1234/card');
- expect(req.request.method).toBe('GET');
- });
- describe('and a new card is detected', () => {
- beforeEach(() => {
- req.flush(mockCardResponse);
- });
- it('should be able inspect the card details from the card service listen subject', () => {
- cardService.listen.subscribe((cardEvent) => {
- expect(cardEvent.card).toBe(mockCardResponse);
- });
- });
- describe('and the same card is detected on the next poll response', () => {
- beforeEach(() => {
- req = httpMock.expectOne('http://localhost:8080/services/card');
- expect(req.request.method).toBe('GET');
- req.flush(itsoCardResponse);
- });
- it('should log a message to say the same card is still present', ) => {
- expect(console.log).toHaveBeenCalledWith('Card still present');
- });
- });
- });
- });
- });
Add Comment
Please, Sign In to add comment