From d494aec2d0f213d04111314c7dc7fd032b596179 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Thu, 28 Sep 2023 18:10:15 -0300 Subject: [PATCH] Add tests Fix #4 --- jest.config.js | 7 ++++ src/js/lib/response.ts | 2 - test/ErrorRenderer.test.tsx | 24 ++++++++++++ test/ResponseRenderer.test.tsx | 70 ++++++++++++++++++++++++++++++++++ test/mocks/chrome.i18n.ts | 8 ++++ tsconfig.json | 2 +- 6 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 jest.config.js create mode 100644 test/ErrorRenderer.test.tsx create mode 100644 test/ResponseRenderer.test.tsx create mode 100644 test/mocks/chrome.i18n.ts diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..2020076 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,7 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: "jsdom", + "moduleNameMapper": { + "^~/(.*)$": "/src/js/$1" + } +}; diff --git a/src/js/lib/response.ts b/src/js/lib/response.ts index a493f1e..5e58d7d 100644 --- a/src/js/lib/response.ts +++ b/src/js/lib/response.ts @@ -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; diff --git a/test/ErrorRenderer.test.tsx b/test/ErrorRenderer.test.tsx new file mode 100644 index 0000000..b182003 --- /dev/null +++ b/test/ErrorRenderer.test.tsx @@ -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(); + }); + + afterEach(() => { + global.chrome = undefined; + }); + + test("an error is rendered", () => { + expect(screen.getByText("Error")).toBeInTheDocument(); + expect(screen.getByText(error.message)).toBeInTheDocument(); + }); +}); diff --git a/test/ResponseRenderer.test.tsx b/test/ResponseRenderer.test.tsx new file mode 100644 index 0000000..8bec523 --- /dev/null +++ b/test/ResponseRenderer.test.tsx @@ -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(); + }); + + 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(); + }); + + test("isTorExitNode is rendered as Yes", () => { + expect(screen.getByText("Tor exit node")).toBeInTheDocument(); + expect(screen.getByText("Yes")).toBeInTheDocument(); + }) + }); +}); diff --git a/test/mocks/chrome.i18n.ts b/test/mocks/chrome.i18n.ts new file mode 100644 index 0000000..01cf661 --- /dev/null +++ b/test/mocks/chrome.i18n.ts @@ -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; +} diff --git a/tsconfig.json b/tsconfig.json index 5b3b790..fe67d0c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,8 +10,8 @@ "jsx": "react", "allowJs": true, "lib": [ "ES2020", "DOM" ], - "baseUrl": "src/", "paths": { "~/*": ["js/*"] }, + "types": ["node", "jest", "@testing-library/jest-dom", "@types/chrome"] } }