@@ -2,79 +2,147 @@ import { Configuration } from '../configuration/Configuration';
2
2
import { IEvent } from '../models/IEvent' ;
3
3
import { IUserDescription } from '../models/IUserDescription' ;
4
4
import { ISubmissionClient } from './ISubmissionClient' ;
5
+ import { ISubmissionAdapter } from './ISubmissionAdapter' ;
5
6
import { DefaultSubmissionClient } from './DefaultSubmissionClient' ;
6
7
import { SettingsResponse } from './SettingsResponse' ;
8
+ import { SubmissionCallback } from './SubmissionCallback' ;
9
+ import { SubmissionRequest } from './SubmissionRequest' ;
7
10
import { SubmissionResponse } from './SubmissionResponse' ;
8
11
9
- describe ( 'DefaultSubmissionClient' , ( ) => {
10
- it ( 'should submit events' , ( done ) => {
11
- function processResponse ( response :SubmissionResponse ) {
12
- if ( response . success ) {
13
- expect ( response . message ) . toBe ( null ) ;
14
- } else {
15
- expect ( response . message ) . toBe ( 'Unable to connect to server.' ) ;
16
- }
17
12
18
- done ( ) ;
19
- }
13
+ class TestAdapter implements ISubmissionAdapter {
14
+ private request ;
15
+ private checks : { ( request : SubmissionRequest ) :void } [ ] = [ ] ;
16
+ private callback :SubmissionCallback ;
17
+ private status = 202 ;
18
+ private message = null ;
19
+ private data ;
20
+ private headers ;
20
21
21
- var config = new Configuration ( { apiKey :'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw' , serverUrl :'http://localhost:50000' } ) ;
22
- var submissionClient = new DefaultSubmissionClient ( ) ;
23
- submissionClient . postEvents ( [ { type : 'log' , message : 'From js client' , reference_id : '123454321' } ] , config , processResponse ) ;
24
- } , 5000 ) ;
22
+ constructor ( check : ( request : SubmissionRequest ) => void ) {
23
+ this . checks . push ( check ) ;
24
+ }
25
25
26
- it ( 'should submit invalid object data' , ( done ) => {
27
- function processResponse ( response : SubmissionResponse ) {
28
- if ( response . success ) {
29
- expect ( response . message ) . toBe ( null ) ;
30
- } else {
31
- expect ( response . message ) . toBe ( 'Unable to connect to server.' ) ;
32
- }
26
+ public withResponse ( status : number , message : string , data ?: string , headers ?: any ) {
27
+ this . status = status ;
28
+ this . message = message ;
29
+ this . data = data ;
30
+ this . headers = headers ;
31
+ return this ;
32
+ }
33
33
34
- done ( ) ;
34
+ public withCheck ( check : ( request : SubmissionRequest ) => void ) {
35
+ this . checks . push ( check ) ;
36
+ return this ;
37
+ }
38
+
39
+ public sendRequest ( request :SubmissionRequest , callback :SubmissionCallback , isAppExiting ?:boolean ) {
40
+ this . request = request ;
41
+ this . callback = callback ;
42
+
43
+ if ( isAppExiting ) {
44
+ this . done ( ) ;
35
45
}
46
+ }
47
+
48
+ public done ( ) {
49
+ if ( ! this . request ) {
50
+ fail ( "sendRequest hasn't been called." ) ;
51
+ return ;
52
+ }
53
+
54
+ this . checks . forEach ( c => c ( this . request ) ) ;
55
+ this . callback ( this . status , this . message , this . data , this . headers ) ;
56
+ }
57
+ }
58
+
59
+ describe ( 'DefaultSubmissionClient' , ( ) => {
60
+
61
+ var adapter :TestAdapter ;
62
+ var config :Configuration ;
63
+ var submissionClient : ISubmissionClient ;
64
+
65
+ beforeEach ( ( ) => {
66
+
67
+ var apiKey = 'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw' ;
68
+ var serverUrl = 'http://localhost:50000'
69
+
70
+ submissionClient = new DefaultSubmissionClient ( ) ;
36
71
37
- var config = new Configuration ( { apiKey :'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw' , serverUrl :'http://localhost:50000' } ) ;
38
- var event :IEvent = { type : 'log' , message : 'From js client' , reference_id : '123454321' , data : {
72
+ config = new Configuration ( {
73
+ apiKey,
74
+ serverUrl
75
+ } ) ;
76
+
77
+ adapter = new TestAdapter ( r => {
78
+ expect ( r . apiKey ) . toBe ( apiKey ) ;
79
+ expect ( r . serverUrl ) . toBe ( serverUrl ) ;
80
+ } ) ;
81
+
82
+ config . submissionAdapter = adapter ;
83
+ } ) ;
84
+
85
+ it ( 'should submit events' , ( done ) => {
86
+ var events = [ { type : 'log' , message : 'From js client' , reference_id : '123454321' } ] ;
87
+
88
+ adapter . withCheck ( r => {
89
+ expect ( r . data ) . toBe ( JSON . stringify ( events ) ) ;
90
+ expect ( r . method ) . toBe ( 'POST' ) ;
91
+ expect ( r . path ) . toBe ( '/api/v2/events' ) ;
92
+ } ) ;
93
+
94
+ submissionClient . postEvents ( events , config , ( ) => done ( ) ) ;
95
+
96
+ adapter . done ( ) ;
97
+ } ) ;
98
+
99
+ it ( 'should submit invalid object data' , ( done ) => {
100
+ var events :IEvent [ ] = [ { type : 'log' , message : 'From js client' , reference_id : '123454321' , data : {
39
101
name : 'blake' ,
40
102
age : function ( ) { throw new Error ( 'Test' ) ; }
41
- } } ;
103
+ } } ] ;
42
104
43
- var submissionClient = new DefaultSubmissionClient ( ) ;
44
- submissionClient . postEvents ( [ event ] , config , processResponse ) ;
45
- } , 5000 ) ;
105
+ adapter . withCheck ( r => {
106
+ expect ( r . data ) . toBe ( JSON . stringify ( events ) ) ;
107
+ expect ( r . method ) . toBe ( 'POST' ) ;
108
+ expect ( r . path ) . toBe ( '/api/v2/events' ) ;
109
+ } ) ;
110
+
111
+ submissionClient . postEvents ( events , config , ( ) => done ( ) ) ;
112
+
113
+ adapter . done ( ) ;
114
+ } ) ;
46
115
47
116
it ( 'should submit user description' , ( done ) => {
48
- function processResponse ( response :SubmissionResponse ) {
49
- if ( response . success ) {
50
- expect ( response . message ) . toBe ( null ) ;
51
- } else {
52
- expect ( response . message ) . toBe ( 'Unable to connect to server.' ) ;
53
- }
117
+ var description = {
118
+ email_address :
'[email protected] ' ,
119
+ description : 'unit test'
120
+ } ;
54
121
55
- done ( ) ;
56
- }
122
+ adapter . withCheck ( r => {
123
+ expect ( r . data ) . toBe ( JSON . stringify ( description ) ) ;
124
+ expect ( r . method ) . toBe ( 'POST' ) ;
125
+ expect ( r . path ) . toBe ( '/api/v2/events/by-ref/123454321/user-description' )
126
+ } ) ;
57
127
58
- var config = new Configuration ( { apiKey : 'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw' , serverUrl : 'http://localhost:50000' } ) ;
59
- var submissionClient = new DefaultSubmissionClient ( ) ;
60
- submissionClient . postUserDescription ( '123454321' , { email_address : '[email protected] ' , description : 'unit test' } , config , processResponse )
61
- } , 5000 ) ;
128
+ submissionClient . postUserDescription ( '123454321' , description , config , ( ) => done ( ) ) ;
129
+
130
+ adapter . done ( ) ;
131
+ } ) ;
62
132
63
133
it ( 'should get project settings' , ( done ) => {
64
- function processResponse ( response : SettingsResponse ) {
65
- if ( response . success ) {
66
- expect ( response . message ) . toBe ( null ) ;
67
- expect ( response . settings ) . not . toBe ( null ) ;
68
- expect ( response . settingsVersion ) . toBeGreaterThan ( - 1 ) ;
69
- } else {
70
- expect ( response . message ) . toBe ( 'Unable to connect to server.' ) ;
71
- }
134
+
135
+ adapter . withResponse ( 200 , null , JSON . stringify ( { version : 1 } ) ) ;
136
+
137
+ submissionClient . getSettings ( config , response => {
138
+ expect ( response . success ) . toBe ( true ) ;
139
+ expect ( response . message ) . toBe ( null ) ;
140
+ expect ( response . settings ) . not . toBe ( null ) ;
141
+ expect ( response . settingsVersion ) . toBeGreaterThan ( - 1 ) ;
72
142
73
143
done ( ) ;
74
- }
144
+ } ) ;
75
145
76
- var config = new Configuration ( { apiKey :'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw' , serverUrl :'http://localhost:50000' } ) ;
77
- var submissionClient = new DefaultSubmissionClient ( ) ;
78
- submissionClient . getSettings ( config , processResponse ) ;
79
- } , 5000 ) ;
146
+ adapter . done ( ) ;
147
+ } ) ;
80
148
} ) ;
0 commit comments