/
utils_test.ts
246 lines (226 loc) · 5.23 KB
/
utils_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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { loadSync } from "https://deno.land/std@0.170.0/dotenv/mod.ts";
import {
afterAll,
beforeAll,
describe,
it,
} from "https://deno.land/std@0.170.0/testing/bdd.ts";
import {
assertSpyCalls,
resolvesNext,
Stub,
stub,
} from "https://deno.land/std@0.170.0/testing/mock.ts";
import {
assertEquals,
assertMatch,
assertRejects,
} from "https://deno.land/std@0.170.0/testing/asserts.ts";
import {
_internals,
buildUrl,
execute,
extractNextParameters,
haveParametersChanged,
} from "../src/utils.ts";
loadSync({ export: true });
const BASE_URL = Deno.env.get("ENV_TYPE") === "local"
? "http://localhost:3000"
: "https://serpapi.com";
describe("extractNextParameters", () => {
it("with serpapi_pagination property", () => {
assertEquals(
extractNextParameters<"google">({
serpapi_pagination: {
next:
"https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=Austin%2C+Texas%2C+United+States&q=coffee&start=10",
},
}),
{
device: "desktop",
gl: "us",
google_domain: "google.com",
hl: "en",
location: "Austin, Texas, United States",
q: "coffee",
start: "10",
},
);
});
it("with pagination property", () => {
assertEquals(
extractNextParameters<"google_scholar_profiles">(
{
pagination: {
next:
"https://serpapi.com/search.json?after_author=rZlDAYoq__8J&engine=google_scholar_profiles&hl=en&mauthors=Mike",
},
},
),
{
after_author: "rZlDAYoq__8J",
hl: "en",
mauthors: "Mike",
},
);
});
});
describe("haveParametersChanged", () => {
it("with different number of properties", () => {
assertEquals(
haveParametersChanged({ q: "coffee" }, {
kl: "us-en",
q: "coffee",
start: "26",
}),
true,
);
assertEquals(
haveParametersChanged({ kl: "us-en", q: "coffee", start: "26" }, {
q: "coffee",
}),
true,
);
});
it("with same number of properties, but different properties", () => {
assertEquals(
haveParametersChanged({
kl: "us-en",
q: "coffee",
safe: "1",
}, {
kl: "us-en",
q: "coffee",
start: "26",
}),
true,
);
});
it("with same properties, but different values", () => {
assertEquals(
haveParametersChanged({
kl: "us-en",
q: "coffee",
start: "30",
}, {
kl: "us-en",
q: "coffee",
start: "26",
}),
true,
);
assertEquals(
haveParametersChanged({
kl: "us-en",
q: "coffee",
start: "26",
}, {
kl: "us-en",
q: "coffee",
start: "30",
}),
true,
);
});
it("with same properties and same values, regardless of type", () => {
assertEquals(
haveParametersChanged({
kl: "us-en",
q: "coffee",
start: "30",
}, {
kl: "us-en",
q: "coffee",
start: "30",
}),
false,
);
assertEquals(
haveParametersChanged({
kl: "us-en",
q: "coffee",
start: 30,
}, {
kl: "us-en",
q: "coffee",
start: "30",
}),
false,
);
assertEquals(
haveParametersChanged({
kl: "us-en",
q: "coffee",
start: "30",
}, {
kl: "us-en",
q: "coffee",
start: 30,
}),
false,
);
});
});
describe("buildUrl", () => {
let urlStub: Stub;
beforeAll(() => {
urlStub = stub(_internals, "getBaseUrl", () => BASE_URL);
});
afterAll(() => {
urlStub.restore();
});
it("with blank path and empty parameters", () => {
assertEquals(buildUrl("", {}), `${BASE_URL}?`);
});
it("with path and empty parameters", () => {
assertEquals(buildUrl("/", {}), `${BASE_URL}/?`);
});
it("with path and parameters", () => {
assertEquals(
buildUrl("/search", { q: "coffee", gl: "us" }),
`${BASE_URL}/search?q=coffee&gl=us`,
);
});
it("with undefined parameters", () => {
assertEquals(
buildUrl("/search", { q: "coffee", gl: undefined, hl: null }),
`${BASE_URL}/search?q=coffee&hl=null`,
);
});
});
describe("execute", {
sanitizeOps: false,
sanitizeResources: false,
}, () => {
let urlStub: Stub;
beforeAll(() => {
urlStub = stub(_internals, "getBaseUrl", () => BASE_URL);
});
afterAll(() => {
urlStub.restore();
});
it("with path and parameters calls fetch with source appended", async () => {
const fetchStub = stub(
_internals,
"fetch",
resolvesNext([new Response("data")]),
);
try {
await execute("/search", { q: "coffee", gl: "us" }, 4000);
} finally {
fetchStub.restore();
}
assertSpyCalls(fetchStub, 1);
const url = fetchStub.calls[0].args[0] as string;
// e.g. deno@1.28.2,serpapi@1.0.0
assertMatch(
url,
/source=(nodejs|deno)%40\d+\.\d+\.\d+%2Cserpapi%40\d+\.\d+\.\d+$/,
);
});
it("with short timeout", () => {
assertRejects(async () =>
await execute("/search", { q: "coffee", gl: "us" }, 1)
);
});
});