Add tests

Fix #4
This commit is contained in:
0x1eef 2023-09-28 18:10:15 -03:00
parent ff7447e7ec
commit d494aec2d0
6 changed files with 110 additions and 3 deletions

7
jest.config.js Normal file
View file

@ -0,0 +1,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: "jsdom",
"moduleNameMapper": {
"^~/(.*)$": "<rootDir>/src/js/$1"
}
};

View file

@ -1,6 +1,5 @@
export type TResponse = {
IPAddress: string;
Location: string;
ISP: string;
City: string;
Country: string;
@ -20,7 +19,6 @@ export function Response(res: TServerResponse): TResponse {
const self = Object.create(null);
self.IPAddress = res.YourFuckingIPAddress;
self.Location = res.YourFuckingLocation;
self.ISP = res.YourFuckingISP;
self.City = res.YourFuckingCity;
self.Country = res.YourFuckingCountry;

View file

@ -0,0 +1,24 @@
import React from "react";
import '@testing-library/jest-dom'
import { render, screen } from "@testing-library/react";
import { ErrorRenderer } from "~/components/ErrorRenderer";
import { getMessage } from "./mocks/chrome.i18n";
describe("ErrorRenderer.tsx", () => {
const error = new Error("This is an example error message");
beforeEach(() => {
const chrome: any = {i18n: {getMessage}};
global.chrome = chrome;
render(<ErrorRenderer error={error}/>);
});
afterEach(() => {
global.chrome = undefined;
});
test("an error is rendered", () => {
expect(screen.getByText("Error")).toBeInTheDocument();
expect(screen.getByText(error.message)).toBeInTheDocument();
});
});

View file

@ -0,0 +1,70 @@
import React from "react";
import '@testing-library/jest-dom'
import { render, screen } from "@testing-library/react";
import { ResponseRenderer } from "~/components/ResponseRenderer";
import { getMessage } from "./mocks/chrome.i18n";
describe("ResponseRenderer.tsx", () => {
beforeEach(() => {
const chrome: any = {i18n: {getMessage}};
global.chrome = chrome;
});
afterEach(() => {
global.chrome = undefined;
});
const defaultResponse = {
IPAddress: "89.222.123.45",
ISP: "FooBar Ltd",
City: "FooBar City",
Country: "United States of FooBar",
isTorExitNode: false
};
describe("when isTorExitNode is false", () => {
const response = {...defaultResponse};
beforeEach(() => {
render(<ResponseRenderer response={response}/>);
});
test("an IP address is rendered", () => {
expect(screen.getByText("IP Address")).toBeInTheDocument();
expect(screen.getByText(response.IPAddress)).toBeInTheDocument();
});
test("an ISP is rendered", () => {
expect(screen.getByText("ISP")).toBeInTheDocument();
expect(screen.getByText(response.ISP)).toBeInTheDocument();
});
test("a city is rendered", () => {
expect(screen.getByText("City")).toBeInTheDocument();
expect(screen.getByText(response.City)).toBeInTheDocument();
});
test("a country is rendered", () => {
expect(screen.getByText("Country")).toBeInTheDocument();
expect(screen.getByText(response.Country)).toBeInTheDocument();
});
test("isTorExitNode is rendered as No", () => {
expect(screen.getByText("Tor exit node")).toBeInTheDocument();
expect(screen.getByText("No")).toBeInTheDocument();
});
});
describe("when isTorExitNode is true", () => {
const response = {...defaultResponse, isTorExitNode: true};
beforeEach(() => {
render(<ResponseRenderer response={response}/>);
});
test("isTorExitNode is rendered as Yes", () => {
expect(screen.getByText("Tor exit node")).toBeInTheDocument();
expect(screen.getByText("Yes")).toBeInTheDocument();
})
});
});

View file

@ -0,0 +1,8 @@
import { readFileSync } from "fs";
import path from "path";
export function getMessage(key: string) {
const buf = readFileSync(path.resolve("src/_locales/en/messages.json"));
const messages = JSON.parse(buf.toString());
return messages[key].message;
}

View file

@ -10,8 +10,8 @@
"jsx": "react",
"allowJs": true,
"lib": [ "ES2020", "DOM" ],
"baseUrl": "src/",
"paths": { "~/*": ["js/*"] },
"types": ["node", "jest", "@testing-library/jest-dom", "@types/chrome"]
}
}