|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | | -import hosting from "./hosting-node.js"; |
4 | | -import nock from "nock"; |
| 3 | +import hosting from "./hosting-api.js"; |
5 | 4 | /* eslint-disable jest/no-disabled-tests */ |
| 5 | + |
| 6 | +process.env.CO2JS_VERSION = "1.2.34"; |
| 7 | +const requestHeaderComment = "TestRunner"; |
| 8 | + |
| 9 | +global.fetch = jest.fn(() => |
| 10 | + Promise.resolve({ |
| 11 | + json: () => Promise.resolve({ green: true }), |
| 12 | + }) |
| 13 | +); |
| 14 | + |
6 | 15 | describe("hostingAPI", () => { |
| 16 | + beforeEach(() => { |
| 17 | + fetch.mockClear(); |
| 18 | + }); |
7 | 19 | describe("checking a single domain with #check", () => { |
8 | | - it.skip("using the API", async () => { |
9 | | - const scope = nock("https://api.thegreenwebfoundation.org/") |
10 | | - .get("/greencheck/google.com") |
11 | | - .reply(200, { |
12 | | - url: "google.com", |
13 | | - green: true, |
14 | | - }); |
| 20 | + it("using the API", async () => { |
15 | 21 | const res = await hosting.check("google.com"); |
| 22 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 23 | + expect(fetch).toHaveBeenLastCalledWith( |
| 24 | + expect.any(String), |
| 25 | + expect.objectContaining({ |
| 26 | + headers: { "User-Agent": "co2js/1.2.34 " }, |
| 27 | + }) |
| 28 | + ); |
| 29 | + expect(res).toEqual(true); |
| 30 | + }); |
| 31 | + it("sets the correct user agent header", async () => { |
| 32 | + const res = await hosting.check("google.com", requestHeaderComment); |
| 33 | + |
| 34 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 35 | + expect(fetch).toHaveBeenLastCalledWith( |
| 36 | + expect.any(String), |
| 37 | + expect.objectContaining({ |
| 38 | + headers: { "User-Agent": "co2js/1.2.34 TestRunner" }, |
| 39 | + }) |
| 40 | + ); |
16 | 41 | expect(res).toEqual(true); |
17 | 42 | }); |
18 | 43 | }); |
19 | 44 | describe("implicitly checking multiple domains with #check", () => { |
20 | | - it.skip("using the API", async () => { |
21 | | - const scope = nock("https://api.thegreenwebfoundation.org/") |
22 | | - .get("/v2/greencheckmulti/[%22google.com%22,%22kochindustries.com%22]") |
23 | | - .reply(200, { |
24 | | - "google.com": { |
25 | | - url: "google.com", |
26 | | - green: true, |
27 | | - }, |
28 | | - "kochindustries.com": { |
29 | | - url: "kochindustries.com", |
30 | | - green: null, |
31 | | - }, |
32 | | - }); |
| 45 | + it("using the API", async () => { |
| 46 | + fetch.mockImplementation(() => |
| 47 | + Promise.resolve({ |
| 48 | + json: () => |
| 49 | + Promise.resolve({ |
| 50 | + "google.com": { url: "google.com", green: true }, |
| 51 | + }), |
| 52 | + }) |
| 53 | + ); |
33 | 54 | const res = await hosting.check(["google.com", "kochindustries.com"]); |
| 55 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 56 | + expect(res).toContain("google.com"); |
| 57 | + }); |
| 58 | + it("sets the correct user agent header", async () => { |
| 59 | + const res = await hosting.check( |
| 60 | + ["google.com", "kochindustries.com"], |
| 61 | + requestHeaderComment |
| 62 | + ); |
| 63 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 64 | + expect(fetch).toHaveBeenLastCalledWith( |
| 65 | + expect.any(String), |
| 66 | + expect.objectContaining({ |
| 67 | + headers: { "User-Agent": "co2js/1.2.34 TestRunner" }, |
| 68 | + }) |
| 69 | + ); |
34 | 70 | expect(res).toContain("google.com"); |
35 | 71 | }); |
36 | 72 | }); |
|
0 commit comments