-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathService.test.ts
118 lines (108 loc) · 2.86 KB
/
Service.test.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
import { config } from "dotenv";
import Service from "./Service";
import { IServiceOptions } from "./Service.types";
import {
IChange,
ICountryParam,
IDateRangeParams,
IFormatParam,
IMainDomainOnlyParam,
IMeta,
IRankings,
} from "./components/Component.types";
export const loadConfig = () => {
const result = config();
if (result.error) {
throw result.error;
}
};
let service: Service;
export const getService = (): Service => {
if (service) {
return service;
}
loadConfig();
const onInvalidCredentials = jest.fn(() => {});
const options: IServiceOptions = {
apiKey: process.env.API_KEY,
onInvalidCredentials: onInvalidCredentials,
};
service = new Service(options);
return service;
};
export const expectWebsiteMeta = (
meta: IMeta<any>["meta"],
domain: string,
options: Partial<ICountryParam> &
Partial<IMainDomainOnlyParam> &
Partial<IFormatParam> &
Partial<IDateRangeParams> = {}
) => {
expectMeta(meta, options);
expect(meta.request.domain).toBe(domain);
};
export const expectKeywordsMeta = (
meta: IMeta<any>["meta"],
keyword: string,
options: Partial<ICountryParam> &
Partial<IMainDomainOnlyParam> &
Partial<IFormatParam> &
Partial<IDateRangeParams> = {}
) => {
expectMeta(meta, options);
expect(meta.request.keyword).toBe(keyword);
};
export const expectMeta = (
meta: IMeta<any>["meta"],
options: Partial<ICountryParam> &
Partial<IMainDomainOnlyParam> &
Partial<IFormatParam> &
Partial<IDateRangeParams> = {}
) => {
expect(meta).toBeTruthy();
expect(meta.status).toBe("Success");
if (options.country) {
expect(meta.request.country).toBe(options.country);
}
if (options.main_domain_only) {
expect(meta.request.main_domain_only).toBe(options.main_domain_only);
}
if (options.start_date) {
expect(meta.request.start_date).toMatch(options.start_date);
}
if (options.end_date) {
expect(meta.request.end_date).toMatch(options.end_date);
}
if (options.format) {
expect(meta.request.format).toBe(options.format);
}
};
export const expectRankings = (data: IRankings) => {
expect(data.global_ranking).toBeGreaterThan(0);
expect(data.category).toBeTruthy();
expect(data.category_ranking).toBeGreaterThan(0);
};
export const expectChange = (data: IChange) => {
if (typeof data.change === "number") {
return expect(true).toBeTruthy();
}
if (typeof data.change === "string") {
return expect(
["NaN", "Infinity"].indexOf(data.change)
).toBeGreaterThanOrEqual(0);
}
expect(data.change).toBeNull();
};
/**
* The tests
*/
describe("service", () => {
const service = getService();
it("should be an instance of Service", () => {
expect(service).toBeInstanceOf(Service);
});
it("should format dates", () => {
const date = new Date("2020-02-03T08:44:00Z");
expect(Service.formatDate(date)).toBe("2020-02-03");
});
});