Guest User

Untitled

a guest
Jul 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. import { HttpClient } from '@angular/common/http';
  2. import { Injectable } from '@angular/core';
  3. import { timer, Observable, BehaviorSubject } from 'rxjs';
  4. import { includes, isEqual } from 'lodash';
  5.  
  6. @Injectable({
  7. providedIn: 'root'
  8. })
  9.  
  10. export class CardService {
  11. private pollInstance: Observable<number>;
  12. private card: IContactlessCard = undefined;
  13. private pollingEnabled = true;
  14.  
  15. public listen: BehaviorSubject<any> = new BehaviorSubject(undefined);
  16.  
  17. constructor(private http: HttpClient) {
  18. this.pollInstance = timer(1000);
  19. }
  20.  
  21. private handleCardRemovedEvent = () => {
  22. this.card = undefined;
  23. console.log('Card removed from reader');
  24. }
  25.  
  26. private handleCardDetectedEvent = (response) => {
  27. console.log('Card detected');
  28. this.card = response;
  29. this.listen.next({
  30. card: this.card
  31. });
  32. }
  33.  
  34. private handleCardStilPresentEvent = () => {
  35. console.log('Card still present');
  36. }
  37.  
  38. private restartPoll = () => {
  39. if (this.pollingEnabled) {
  40. this.pollInstance.subscribe(() => {
  41. this.pollCardStatus();
  42. });
  43. }
  44. }
  45.  
  46. public pollCardStatus = () => {
  47. console.log('pollCardStatus...');
  48. this.pollingEnabled = true;
  49. this.http.get('http://someendpoint:1234/card').subscribe((response: any) => {
  50. if (!response) {
  51. if (this.card) {
  52. this.handleCardRemovedEvent();
  53. }
  54.  
  55. this.restartPoll();
  56. return;
  57. }
  58.  
  59. if (!isEqual(this.card, response)) {
  60. this.handleCardDetectedEvent(response);
  61. } else {
  62. this.handleCardStilPresentEvent();
  63. }
  64.  
  65. this.restartPoll();
  66. }, (error) => {
  67. this.restartPoll();
  68. });
  69. }
  70.  
  71. public stopPolling = () => {
  72. console.log('Card polling cancelled');
  73. this.pollingEnabled = false;
  74. }
  75.  
  76. }
  77.  
  78. import { CardService } from './card.service';
  79. import { TestBed, getTestBed, fakeAsync, tick } from '@angular/core/testing';
  80. import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
  81.  
  82. describe('CardService Tests:', () => {
  83. let cardService: CardService;
  84. let injector: TestBed;
  85. let httpMock: HttpTestingController;
  86. let req;
  87.  
  88. const mockCardResponse = {
  89. 'type': 'SOME_CARD',
  90. 'uid': 'xxx-111-222-ZZZ'
  91. };
  92.  
  93. beforeEach(() => {
  94. TestBed.configureTestingModule({
  95. imports: [HttpClientTestingModule],
  96. providers: [CardService]
  97. });
  98. injector = getTestBed();
  99. cardService = injector.get(CardService);
  100. httpMock = injector.get(HttpTestingController);
  101. });
  102.  
  103. afterEach(() => {
  104. httpMock.verify();
  105. });
  106.  
  107. describe('when polling for card status changes', () => {
  108. beforeEach(() => {
  109. cardService.pollCardStatus();
  110. req = httpMock.expectOne('http://someendpoint:1234/card');
  111. expect(req.request.method).toBe('GET');
  112. });
  113. describe('and a new card is detected', () => {
  114. beforeEach(() => {
  115. req.flush(mockCardResponse);
  116. });
  117. it('should be able inspect the card details from the card service listen subject', () => {
  118. cardService.listen.subscribe((cardEvent) => {
  119. expect(cardEvent.card).toBe(mockCardResponse);
  120. });
  121. });
  122. });
  123. });
  124. });
  125.  
  126. import { CardService } from './card.service';
  127. import { TestBed, getTestBed, fakeAsync, tick } from '@angular/core/testing';
  128. import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
  129.  
  130. describe('CardService Tests:', () => {
  131. let cardService: CardService;
  132. let injector: TestBed;
  133. let httpMock: HttpTestingController;
  134. let req;
  135.  
  136. const mockCardResponse = {
  137. 'type': 'SOME_CARD',
  138. 'uid': 'xxx-111-222-ZZZ'
  139. };
  140.  
  141. beforeEach(() => {
  142. TestBed.configureTestingModule({
  143. imports: [HttpClientTestingModule],
  144. providers: [CardService]
  145. });
  146. injector = getTestBed();
  147. cardService = injector.get(CardService);
  148. httpMock = injector.get(HttpTestingController);
  149. });
  150.  
  151. afterEach(() => {
  152. httpMock.verify();
  153. });
  154.  
  155. describe('when polling for card status changes', () => {
  156. beforeEach(() => {
  157. cardService.pollCardStatus();
  158. req = httpMock.expectOne('http://someendpoint:1234/card');
  159. expect(req.request.method).toBe('GET');
  160. });
  161. describe('and a new card is detected', () => {
  162. beforeEach(() => {
  163. req.flush(mockCardResponse);
  164. });
  165. it('should be able inspect the card details from the card service listen subject', () => {
  166. cardService.listen.subscribe((cardEvent) => {
  167. expect(cardEvent.card).toBe(mockCardResponse);
  168. });
  169. });
  170. describe('and the same card is detected on the next poll response', () => {
  171. beforeEach(() => {
  172. req = httpMock.expectOne('http://localhost:8080/services/card');
  173. expect(req.request.method).toBe('GET');
  174. req.flush(itsoCardResponse);
  175. });
  176. it('should log a message to say the same card is still present', ) => {
  177. expect(console.log).toHaveBeenCalledWith('Card still present');
  178. });
  179. });
  180. });
  181. });
  182. });
Add Comment
Please, Sign In to add comment