Skip to content

Commit fde9891

Browse files
authored
chore: merge pull request #80 from threeal/mock-search-result
Mock Google Search Results for Testing
2 parents dfe2cc3 + 49bcfad commit fde9891

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

.github/workflows/test.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ on:
55
branches: ["*", "!latest"]
66
push:
77
branches: [latest, main]
8+
env:
9+
NODE_OPTIONS: --experimental-import-meta-resolve
810
jobs:
911
package:
1012
runs-on: ${{ matrix.os }}-latest

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"chai": "^4.3.8",
4949
"chai-as-promised": "^7.1.1",
5050
"eslint": "^8.48.0",
51-
"esmock": "^2.3.8",
51+
"esmock": "2.3.8",
5252
"mocha": "^10.2.0",
5353
"nx": "^16.7.4",
5454
"prettier": "^3.0.2",

test/rank.test.mjs

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,59 @@
1-
import { getWebsiteRank } from "../dist/rank.mjs";
1+
import esmock from "esmock";
22

33
describe("rank a website in Google Search", function () {
4-
this.retries(3);
5-
this.timeout(20000);
4+
const mockImport = () =>
5+
esmock("../dist/rank.mjs", {
6+
["google-sr"]: {
7+
searchWithPages: async ({ query, pages }) => {
8+
if (query !== "google") return [];
9+
const websitesPages = [
10+
[
11+
"https://www.google.com/?hl=id",
12+
"https://www.google.co.id/?hl=id",
13+
"https://accounts.google.com/login?hl=id",
14+
"https://play.google.com/store/apps/details?id=com.google.android.googlequicksearchbox&hl=en_US",
15+
"https://myaccount.google.com/privacy?hl=id",
16+
"https://accounts.google.com/",
17+
"https://myaccount.google.com/intro/personal-info?hl=id",
18+
"https://id.wikipedia.org/wiki/Google",
19+
],
20+
[
21+
"https://twitter.com/Google",
22+
"https://accounts.google.com/",
23+
"https://www.youtube.com/user/google",
24+
"https://www.instagram.com/googleindonesia/",
25+
"https://news.google.com/?hl=ID",
26+
"https://about.google/",
27+
"https://blog.google/",
28+
"https://store.google.com/",
29+
"https://support.google.com/",
30+
"https://www.facebook.com/Google/?locale=id_ID",
31+
],
32+
[
33+
"https://www.facebook.com/Google/?locale=id_ID",
34+
"https://io.google/",
35+
"https://www.google.co.uk/",
36+
"https://myactivity.google.com/",
37+
"https://apps.google.com/meet/",
38+
"https://one.google.com/about",
39+
"https://bard.google.com/?hl=in",
40+
"https://ads.google.com/intl/id_id/home/",
41+
"https://classroom.google.com/",
42+
],
43+
];
44+
return websitesPages.slice(0, pages).map((websites) => {
45+
return websites.map((website) => ({
46+
type: "SEARCH",
47+
link: website,
48+
}));
49+
});
50+
},
51+
},
52+
});
653

754
it("should rank a website that is found", async () => {
8-
const prom = getWebsiteRank("github.com", "googlethis");
55+
const { getWebsiteRank } = await mockImport();
56+
const prom = getWebsiteRank("google.com", "google");
957
return prom.should.eventually.not.to.be.undefined.and.then(
1058
({ page, rank }) => {
1159
page.should.be.equal(0);
@@ -15,12 +63,14 @@ describe("rank a website in Google Search", function () {
1563
});
1664

1765
it("should not rank a website that is not found", async () => {
18-
const prom = getWebsiteRank("randomsite.con", "googlethis");
66+
const { getWebsiteRank } = await mockImport();
67+
const prom = getWebsiteRank("randomsite.con", "google");
1968
return prom.should.eventually.to.be.undefined;
2069
});
2170

2271
it("should rank a website that is found on a specific page", async () => {
23-
const prom = getWebsiteRank("facebook.com", "googlethis", { maxPage: 10 });
72+
const { getWebsiteRank } = await mockImport();
73+
const prom = getWebsiteRank("facebook.com", "google", { maxPage: 3 });
2474
return prom.should.eventually.not.to.be.undefined.and.then(
2575
({ page, rank }) => {
2676
page.should.be.above(0);

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,7 @@ __metadata:
13981398
languageName: node
13991399
linkType: hard
14001400

1401-
"esmock@npm:^2.3.8":
1401+
"esmock@npm:2.3.8":
14021402
version: 2.3.8
14031403
resolution: "esmock@npm:2.3.8"
14041404
checksum: 3d2df5cabdcaaeeee683c42ca7d1ee670b3038ff83203434e6aa23058121c047c1d4335b197f21211c56552ce70fa1fd400a1a41d1d95189c792ac34b05fe9dc
@@ -1855,7 +1855,7 @@ __metadata:
18551855
chalk: ^4.1.2
18561856
commander: ^11.0.0
18571857
eslint: ^8.48.0
1858-
esmock: ^2.3.8
1858+
esmock: 2.3.8
18591859
google-sr: ^3.2.1
18601860
mocha: ^10.2.0
18611861
nx: ^16.7.4

0 commit comments

Comments
 (0)