@@ -6,8 +6,6 @@ import { CompanyInsightsController } from "../../lib/controllers/company-insight
6
6
describe ( "companyInsightsController" , ( ) => {
7
7
const insightsUrlEndpoint = "/api/insights?ipAddress=%s" ;
8
8
const currentIp = "8.8.8.8" ;
9
- const profileDir = "profileDir" ;
10
- const cacheTimeout = 30 * 24 * 60 * 60 * 1000 ; // 2 days in milliseconds
11
9
const defaultCompanyData = {
12
10
company : {
13
11
name : "Progress" ,
@@ -29,26 +27,6 @@ describe("companyInsightsController", () => {
29
27
employeeCount : "500"
30
28
} ;
31
29
32
- const defaultExpectedDataPassedToGetSetting : any [ ] = [ { settingName : `companyInformation_${ currentIp } ` , cacheOpts : { cacheTimeout } } ] ;
33
- const defaultExpectedDataPassedToSaveSetting : any [ ] = [
34
- {
35
- cacheOpts : {
36
- useCaching : true
37
- } ,
38
- key : "companyInformation_8.8.8.8" ,
39
- value : {
40
- country : "Bulgaria" ,
41
- employeeCount : "500" ,
42
- industries : "Software__Software 2" ,
43
- name : "Progress" ,
44
- revenue : "123131"
45
- }
46
- }
47
- ] ;
48
-
49
- let dataPassedToGetSettingValue : { settingName : string , cacheOpts ?: ICacheTimeoutOpts } [ ] = [ ] ;
50
- let dataPassedToSaveSettingValue : { key : string , value : any , cacheOpts ?: IUseCacheOpts } [ ] = [ ] ;
51
- let getSettingValueResult : IDictionary < any > = null ;
52
30
let httpRequestCounter = 0 ;
53
31
let httpRequestResult : any = null ;
54
32
let testInjector : IInjector = null ;
@@ -68,43 +46,24 @@ describe("companyInsightsController", () => {
68
46
} ) ;
69
47
70
48
injector . register ( "logger" , LoggerStub ) ;
71
- injector . register ( "injector" , injector ) ;
72
49
injector . register ( "ipService" , {
73
50
getCurrentIPv4Address : async ( ) : Promise < string > => currentIp
74
51
} ) ;
75
52
76
- injector . register ( "settingsService" , {
77
- getProfileDir : ( ) : string => profileDir
78
- } ) ;
79
-
80
- injector . register ( "jsonFileSettingsService" , {
81
- getSettingValue : async ( settingName : string , cacheOpts ?: ICacheTimeoutOpts ) : Promise < any > => {
82
- dataPassedToGetSettingValue . push ( { settingName, cacheOpts } ) ;
83
- return getSettingValueResult ;
84
- } ,
85
-
86
- saveSetting : async ( key : string , value : any , cacheOpts ?: IUseCacheOpts ) : Promise < void > => {
87
- dataPassedToSaveSettingValue . push ( { key, value, cacheOpts } ) ;
88
- }
89
- } ) ;
90
-
91
53
injector . register ( "companyInsightsController" , CompanyInsightsController ) ;
92
54
93
55
return injector ;
94
56
} ;
95
57
96
58
beforeEach ( ( ) => {
97
- dataPassedToGetSettingValue = [ ] ;
98
- dataPassedToSaveSettingValue = [ ] ;
99
- getSettingValueResult = null ;
100
59
httpRequestCounter = 0 ;
101
60
httpRequestResult = defaultCompanyData ;
102
61
testInjector = createTestInjector ( ) ;
103
62
companyInsightsController = testInjector . resolve < ICompanyInsightsController > ( "companyInsightsController" ) ;
104
63
} ) ;
105
64
106
65
describe ( "getCompanyData" , ( ) => {
107
- describe ( "returns null when data does not exist in the cache and " , ( ) => {
66
+ describe ( "returns null when" , ( ) => {
108
67
it ( "the http client fails to get data" , async ( ) => {
109
68
const httpClient = testInjector . resolve < Server . IHttpClient > ( "httpClient" ) ;
110
69
const errMsg = "custom error" ;
@@ -146,31 +105,13 @@ describe("companyInsightsController", () => {
146
105
const companyData = await companyInsightsController . getCompanyData ( ) ;
147
106
assert . deepEqual ( companyData , null ) ;
148
107
assert . equal ( httpRequestCounter , 0 , "We should not have any http request" ) ;
149
- assert . deepEqual ( dataPassedToGetSettingValue , [ ] , "When we are unable to get IP, we should not try to get value from the cache." ) ;
150
- assert . deepEqual ( dataPassedToSaveSettingValue , [ ] , "When we are unable to get IP, we should not persist anything." ) ;
151
108
} ) ;
152
109
} ) ;
153
110
154
111
describe ( "returns correct data when" , ( ) => {
155
- it ( "data for current ip exist in the cache" , async ( ) => {
156
- httpRequestResult = null ;
157
-
158
- getSettingValueResult = defaultExpectedCompanyData ; // data in the file should be in the already parsed format
159
- const companyData = await companyInsightsController . getCompanyData ( ) ;
160
- assert . deepEqual ( companyData , defaultExpectedCompanyData ) ;
161
-
162
- assert . equal ( httpRequestCounter , 0 , "In case we have data for the company in our cache, we should not make any http requests" ) ;
163
- assert . deepEqual ( dataPassedToGetSettingValue , defaultExpectedDataPassedToGetSetting ) ;
164
- assert . deepEqual ( dataPassedToSaveSettingValue , [ ] ) ;
165
- } ) ;
166
-
167
- describe ( "data for current ip does not exist in the cache and" , ( ) => {
168
-
169
112
it ( "response contains company property" , async ( ) => {
170
113
const companyData = await companyInsightsController . getCompanyData ( ) ;
171
114
assert . deepEqual ( companyData , defaultExpectedCompanyData ) ;
172
- assert . deepEqual ( dataPassedToGetSettingValue , defaultExpectedDataPassedToGetSetting ) ;
173
- assert . deepEqual ( dataPassedToSaveSettingValue , defaultExpectedDataPassedToSaveSetting ) ;
174
115
} ) ;
175
116
176
117
it ( "response contains company property and industries in it are not populated" , async ( ) => {
@@ -191,26 +132,8 @@ describe("companyInsightsController", () => {
191
132
industries : null ,
192
133
employeeCount : "500"
193
134
} ) ;
194
-
195
- assert . deepEqual ( dataPassedToGetSettingValue , defaultExpectedDataPassedToGetSetting ) ;
196
- assert . deepEqual ( dataPassedToSaveSettingValue , [
197
- {
198
- cacheOpts : {
199
- useCaching : true
200
- } ,
201
- key : "companyInformation_8.8.8.8" ,
202
- value : {
203
- country : "Bulgaria" ,
204
- employeeCount : "500" ,
205
- industries : null ,
206
- name : "Progress" ,
207
- revenue : "123131"
208
- }
209
- }
210
- ] ) ;
211
135
} ) ;
212
136
213
- } ) ;
214
137
} ) ;
215
138
216
139
it ( "is called only once per process" , async ( ) => {
0 commit comments