Tests for translate utils
This commit is contained in:
14
tests/pages/Error.test.tsx
Normal file
14
tests/pages/Error.test.tsx
Normal file
@@ -0,0 +1,14 @@
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import faker from "faker";
|
||||
import Error from "next/error";
|
||||
|
||||
it("renders a not found message on 404 code", () => {
|
||||
render(<Error statusCode={404} />);
|
||||
expect(screen.getByText(/this page could not be found/i)).toBeVisible();
|
||||
});
|
||||
|
||||
it("renders the correct status code", () => {
|
||||
const code = faker.random.number({ min: 100, max: 599 });
|
||||
render(<Error statusCode={code} />);
|
||||
expect(screen.getByText(code)).toBeVisible();
|
||||
});
|
||||
3
tests/setupTests.ts
Normal file
3
tests/setupTests.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import '@testing-library/jest-dom';
|
||||
import jestFetchMock from 'jest-fetch-mock';
|
||||
jestFetchMock.enableMocks();
|
||||
75
tests/utils/translate.test.ts
Normal file
75
tests/utils/translate.test.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import faker from "faker";
|
||||
import { googleScrape, extractSlug } from "../../utils/translate";
|
||||
|
||||
const source = faker.random.locale();
|
||||
const target = faker.random.locale();
|
||||
const query = faker.random.words();
|
||||
|
||||
describe("googleScrape", () => {
|
||||
beforeEach(() => {
|
||||
fetchMock.resetMocks();
|
||||
});
|
||||
|
||||
it("parses html response correctly", async () => {
|
||||
const translation = faker.random.words();
|
||||
const className = "result-container";
|
||||
const html = `
|
||||
<div class=${className}>
|
||||
${translation}
|
||||
</div>
|
||||
`;
|
||||
fetchMock.mockResponseOnce(async () => ({ body: html }));
|
||||
|
||||
expect(await googleScrape(source, target, query)).toStrictEqual({ translation });
|
||||
});
|
||||
|
||||
it("returns status code on request error", async () => {
|
||||
const status = faker.random.number({ min: 400, max: 499 });
|
||||
fetchMock.mockResponseOnce(async () => ({ status }));
|
||||
|
||||
expect(await googleScrape(source, target, query)).toStrictEqual({ statusCode: status });
|
||||
});
|
||||
|
||||
it("returns correct message on network error", async () => {
|
||||
fetchMock.mockRejectOnce();
|
||||
|
||||
const res = await googleScrape(source, target, query);
|
||||
expect(res?.errorMsg).toMatch(/retrieving/);
|
||||
});
|
||||
|
||||
it("returns correct message on parsing wrong class", async () => {
|
||||
const translation = faker.random.words();
|
||||
const className = "wrong-container";
|
||||
const html = `
|
||||
<div class=${className}>
|
||||
${translation}
|
||||
</div>
|
||||
`;
|
||||
fetchMock.mockResponseOnce(async () => ({ body: html }));
|
||||
|
||||
const res = await googleScrape(source, target, query);
|
||||
expect(res?.errorMsg).toMatch(/parsing/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractSlug", () => {
|
||||
it("returns 'query' for 1 param", () => {
|
||||
expect(extractSlug([query])).toStrictEqual({ query });
|
||||
});
|
||||
|
||||
it("returns 'target' & 'query' resp. for 2 params", () => {
|
||||
expect(extractSlug([target, query])).toStrictEqual({ target, query });
|
||||
});
|
||||
|
||||
it("returns 'source', 'target' & 'query' resp. for 3 param", () => {
|
||||
expect(extractSlug([source, target, query])).toStrictEqual({ source, target, query });
|
||||
});
|
||||
|
||||
it("returns empty object on 0 or >4 params", () => {
|
||||
expect(extractSlug([])).toStrictEqual({});
|
||||
|
||||
const length = faker.random.number({ min: 4, max: 50 });
|
||||
const array = Array(length).fill("");
|
||||
expect(extractSlug(array)).toStrictEqual({});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user