-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathcompany-insights-controller.ts
149 lines (126 loc) · 4.66 KB
/
company-insights-controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { assert } from "chai";
import { Yok } from "../../lib/common/yok";
import { LoggerStub } from "../stubs";
import { CompanyInsightsController } from "../../lib/controllers/company-insights-controller";
describe("companyInsightsController", () => {
const insightsUrlEndpoint = "/api/insights?ipAddress=%s";
const currentIp = "8.8.8.8";
const defaultCompanyData = {
company: {
name: "Progress",
country: "Bulgaria",
revenue: "123131",
industries: [
"Software",
"Software 2"
],
employeeCount: "500"
}
};
const defaultExpectedCompanyData: ICompanyData = {
name: "Progress",
country: "Bulgaria",
revenue: "123131",
industries: "Software__Software 2",
employeeCount: "500"
};
let httpRequestCounter = 0;
let httpRequestResult: any = null;
let testInjector: IInjector = null;
let companyInsightsController: ICompanyInsightsController = null;
const createTestInjector = (): IInjector => {
const injector = new Yok();
injector.register("config", {
INSIGHTS_URL_ENDPOINT: insightsUrlEndpoint
});
injector.register("httpClient", {
httpRequest: async (options: any, proxySettings?: IProxySettings): Promise<any> => {
httpRequestCounter++;
return { body: JSON.stringify(httpRequestResult) };
}
});
injector.register("logger", LoggerStub);
injector.register("ipService", {
getCurrentIPv4Address: async (): Promise<string> => currentIp
});
injector.register("companyInsightsController", CompanyInsightsController);
return injector;
};
beforeEach(() => {
httpRequestCounter = 0;
httpRequestResult = defaultCompanyData;
testInjector = createTestInjector();
companyInsightsController = testInjector.resolve<ICompanyInsightsController>("companyInsightsController");
});
describe("getCompanyData", () => {
describe("returns null when", () => {
it("the http client fails to get data", async () => {
const httpClient = testInjector.resolve<Server.IHttpClient>("httpClient");
const errMsg = "custom error";
httpClient.httpRequest = async () => {
throw new Error(errMsg);
};
const companyData = await companyInsightsController.getCompanyData();
assert.isNull(companyData);
const logger = testInjector.resolve<LoggerStub>("logger");
assert.isTrue(logger.traceOutput.indexOf(errMsg) !== -1);
});
it("the body of the response is not a valid JSON", async () => {
const httpClient = testInjector.resolve<Server.IHttpClient>("httpClient");
httpClient.httpRequest = async (): Promise<any> => {
return { body: "invalid JSON" };
};
const companyData = await companyInsightsController.getCompanyData();
assert.isNull(companyData);
const logger = testInjector.resolve<LoggerStub>("logger");
assert.isTrue(logger.traceOutput.indexOf("SyntaxError: Unexpected token") !== -1);
});
it("response does not contain company property", async () => {
httpRequestResult = {
foo: "bar"
};
const companyData = await companyInsightsController.getCompanyData();
assert.deepEqual(companyData, null);
});
it("unable to get current ip address", async () => {
const ipService = testInjector.resolve<IIPService>("ipService");
ipService.getCurrentIPv4Address = async (): Promise<string> => { throw new Error("Unable to get current ip addreess"); };
const companyData = await companyInsightsController.getCompanyData();
assert.deepEqual(companyData, null);
assert.equal(httpRequestCounter, 0, "We should not have any http request");
});
});
describe("returns correct data when", () => {
it("response contains company property", async () => {
const companyData = await companyInsightsController.getCompanyData();
assert.deepEqual(companyData, defaultExpectedCompanyData);
});
it("response contains company property and industries in it are not populated", async () => {
httpRequestResult = {
company: {
name: "Progress",
country: "Bulgaria",
revenue: "123131",
employeeCount: "500"
}
};
const companyData = await companyInsightsController.getCompanyData();
assert.deepEqual(companyData, {
name: "Progress",
country: "Bulgaria",
revenue: "123131",
industries: null,
employeeCount: "500"
});
});
});
it("is called only once per process", async () => {
const companyData = await companyInsightsController.getCompanyData();
assert.deepEqual(companyData, defaultExpectedCompanyData);
assert.equal(httpRequestCounter, 1);
const companyDataSecondCall = await companyInsightsController.getCompanyData();
assert.deepEqual(companyDataSecondCall, defaultExpectedCompanyData);
assert.equal(httpRequestCounter, 1);
});
});
});