@@ -2,13 +2,16 @@ import { TestBed, inject } from '@angular/core/testing';
2
2
import EventSourceMock , { sources } from 'eventsourcemock' ;
3
3
import { NotificationService } from './notification.service' ;
4
4
import { EventSourceService } from './event-source.service' ;
5
- import { StoreModule } from '@ngrx/store' ;
5
+ import { Store , StoreModule } from '@ngrx/store' ;
6
6
import { reducers } from '../../action-reducer-map' ;
7
+ import { UpdatePlannedRoute } from './actions/notification.action' ;
7
8
8
9
describe ( 'NotificationService' , ( ) => {
10
+ const DEFAULT_TIMEOUT = jasmine . DEFAULT_TIMEOUT_INTERVAL ;
9
11
let uri : string ;
10
12
11
13
beforeEach ( ( ) => {
14
+ jasmine . DEFAULT_TIMEOUT_INTERVAL = 10000 ;
12
15
uri = 'http://localhost:1234/events' ;
13
16
const mockEventSource = new EventSourceMock ( uri ) ;
14
17
@@ -19,6 +22,8 @@ describe('NotificationService', () => {
19
22
providers : [ NotificationService , EventSourceService ]
20
23
} ) ;
21
24
const eventSourceService = TestBed . get ( EventSourceService ) ;
25
+ const store = TestBed . get ( Store ) ;
26
+ spyOn ( store , 'dispatch' ) . and . callThrough ( ) ;
22
27
spyOn ( eventSourceService , 'forUrl' ) . and . returnValue ( mockEventSource ) ;
23
28
} ) ;
24
29
@@ -35,52 +40,50 @@ describe('NotificationService', () => {
35
40
} ) ;
36
41
}
37
42
38
- it ( 'should be able to update ' , ( done ) => {
39
- // Arrange
43
+ it ( 'should use the EventSourceService ' , ( ) => {
44
+ const eventSourceService = TestBed . get ( EventSourceService ) ;
40
45
const service = TestBed . get ( NotificationService ) ;
41
- const source = sources [ uri ] ;
42
- const expectedCost = 1234.56 ;
43
- const expectedCostString = expectedCost . toLocaleString ( 'en-US' , { currency : 'USD' , style : 'currency' } ) ;
44
- const message = mockNotificationEvent ( NotificationService . ESTIMATED_PRICE , {
45
- cost : expectedCost
46
- } ) ;
47
46
48
- // Act
49
- source . emitOpen ( ) ;
50
- const observable = service . getCurrentPriceEstimate ( ) ;
51
- observable . subscribe ( val => {
52
- // Assert
53
- expect ( val ) . toBe ( expectedCostString ) ;
54
- done ( ) ;
55
- } ) ;
47
+ service . onInit ( ) ;
56
48
57
- source . emitMessage ( message ) ;
49
+ expect ( eventSourceService . forUrl ) . toHaveBeenCalled ( ) ;
58
50
} ) ;
59
51
60
- it ( 'should use last available value' , ( done ) => {
52
+ it ( 'should respond correctly to EventSource failing' , ( ) => {
53
+
54
+ const service = TestBed . get ( NotificationService ) ;
55
+ const eventSourceService = TestBed . get ( EventSourceService ) ;
56
+ spyOn ( service , 'bindToNotificationEventSource' ) . and . callThrough ( ) ;
57
+ //spyOn(eventSourceService, 'forUrl');
58
+
59
+ service . onInit ( ) ;
60
+ sources [ uri ] . emitError ( new Event ( null , null ) ) ;
61
+
62
+ expect ( eventSourceService . forUrl ) . toHaveBeenCalledTimes ( 2 ) ;
63
+ expect ( service . bindToNotificationEventSource ) . toHaveBeenCalledTimes ( 1 ) ;
64
+ } ) ;
65
+
66
+ it ( 'should be able to update the app state' , ( ) => {
61
67
// Arrange
68
+ const store = TestBed . get ( Store ) ;
62
69
const service = TestBed . get ( NotificationService ) ;
63
70
const source = sources [ uri ] ;
64
- const expectedCost = 1248.16 ;
65
- const expectedCostString = expectedCost . toLocaleString ( 'en-US' , { currency : 'USD' , style : 'currency' } ) ;
66
- const messageA = mockNotificationEvent ( NotificationService . ESTIMATED_PRICE , {
67
- cost : 0.01
68
- } ) ;
69
- const messageB = mockNotificationEvent ( NotificationService . ESTIMATED_PRICE , {
70
- cost : expectedCost
71
- } ) ;
71
+ const expectedCost = 1234.56 ;
72
+ let body = {
73
+ cost : expectedCost ,
74
+ origin : null ,
75
+ destination : null ,
76
+ distance : 0 ,
77
+ duration : 0
78
+ } ;
79
+ const message = mockNotificationEvent ( NotificationService . ESTIMATED_PRICE , body ) ;
72
80
73
81
// Act
74
82
source . emitOpen ( ) ;
75
- source . emitMessage ( messageA ) ;
76
- source . emitMessage ( messageB ) ;
77
- setTimeout ( ( ) => {
78
- const observable = service . getCurrentPriceEstimate ( ) ;
79
- observable . subscribe ( val => {
80
- // Assert
81
- expect ( val ) . toBe ( expectedCostString ) ;
82
- done ( ) ;
83
- } ) ;
84
- } , 1000 ) ;
83
+ source . emitMessage ( message ) ;
84
+
85
+ // Assert
86
+ expect ( service . _isActive ) . toBeTruthy ( ) ;
87
+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new UpdatePlannedRoute ( body ) ) ;
85
88
} ) ;
86
89
} ) ;
0 commit comments