parent
954ab54493
commit
b04ed9d628
@ -0,0 +1,21 @@
|
||||
const nextJest = require("next/jest");
|
||||
|
||||
const createJestConfig = nextJest({ dir: "./" });
|
||||
|
||||
const customJestConfig = {
|
||||
testEnvironment: "jsdom",
|
||||
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
|
||||
moduleDirectories: ["node_modules"],
|
||||
moduleNameMapper: {
|
||||
"^components/(.*)$": "<rootDir>/src/components/$1",
|
||||
"^utils/(.*)$": "<rootDir>/src/utils/$1",
|
||||
"^widgets/(.*)$": "<rootDir>/src/widgets/$1",
|
||||
},
|
||||
transformIgnorePatterns: ["/node_modules/"],
|
||||
testPathIgnorePatterns: ["<rootDir>/node_modules/", "config/"],
|
||||
collectCoverage: true,
|
||||
coverageDirectory: "coverage",
|
||||
clearMocks: true,
|
||||
};
|
||||
|
||||
module.exports = createJestConfig(customJestConfig);
|
@ -0,0 +1,45 @@
|
||||
import i18n from "i18next";
|
||||
import { initReactI18next, setI18n } from "react-i18next";
|
||||
import rawTranslationEN from "./public/locales/en/common.json";
|
||||
import i18nConfig from "./next-i18next.config.js";
|
||||
|
||||
const translationEN = {
|
||||
common: rawTranslationEN,
|
||||
};
|
||||
|
||||
// Set up formatter service
|
||||
i18n.services = i18n.services || {};
|
||||
i18n.services.formatter = {
|
||||
add: (name, fn) => {
|
||||
i18n.formatters = i18n.formatters || {};
|
||||
i18n.formatters[name] = fn;
|
||||
},
|
||||
format: (value, format, lng, options) => {
|
||||
return i18n.formatters?.[format]?.(value, lng, options) ?? value;
|
||||
},
|
||||
};
|
||||
|
||||
// Register custom formatters
|
||||
i18nConfig.use[0].init(i18n);
|
||||
|
||||
// Initialize i18n
|
||||
i18n.use(initReactI18next).init({
|
||||
lng: "en",
|
||||
fallbackLng: "en",
|
||||
debug: false,
|
||||
initImmediate: false, // make sure init is synchronous in tests
|
||||
interpolation: {
|
||||
escapeValue: false,
|
||||
format: (value, format, lng, options) => i18n.services.formatter.format(value, format, lng, options),
|
||||
},
|
||||
resources: {
|
||||
en: translationEN,
|
||||
},
|
||||
ns: ["common"],
|
||||
defaultNS: "common",
|
||||
returnNull: false,
|
||||
});
|
||||
|
||||
setI18n(i18n);
|
||||
|
||||
export default i18n;
|
@ -0,0 +1,20 @@
|
||||
import "@testing-library/jest-dom";
|
||||
import "./jest.i18n.js";
|
||||
|
||||
jest.mock("utils/proxy/use-widget-api");
|
||||
|
||||
jest.mock("next-i18next", () => {
|
||||
const reactI18next = require("react-i18next");
|
||||
const i18n = require("./jest.i18n").default;
|
||||
|
||||
return {
|
||||
useTranslation: () => reactI18next.useTranslation("", { i18n }),
|
||||
Trans: reactI18next.Trans,
|
||||
I18nextProvider: reactI18next.I18nextProvider,
|
||||
appWithTranslation: (Component) => (props) => (
|
||||
<reactI18next.I18nextProvider i18n={i18n}>
|
||||
<Component {...props} />
|
||||
</reactI18next.I18nextProvider>
|
||||
),
|
||||
};
|
||||
});
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,17 @@
|
||||
let mockResponses = {};
|
||||
|
||||
export function __setEndpointMockData(endpoint, response) {
|
||||
mockResponses[endpoint] = response;
|
||||
}
|
||||
|
||||
export function __setEndpointMockError(endpoint, error) {
|
||||
mockResponses[endpoint] = error;
|
||||
}
|
||||
|
||||
export function __clearMocks() {
|
||||
mockResponses = {};
|
||||
}
|
||||
|
||||
export default function useWidgetAPI(widget, endpoint) {
|
||||
return mockResponses[endpoint] ?? { data: undefined, error: undefined };
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import Component from "widgets/gitlab/component";
|
||||
import { SettingsContext } from "utils/contexts/settings";
|
||||
import React from "react";
|
||||
|
||||
import { __setEndpointMockData, __clearMocks } from "utils/proxy/use-widget-api";
|
||||
|
||||
const mockSettings = {
|
||||
hideErrors: false,
|
||||
};
|
||||
|
||||
const service = {
|
||||
name: "Gitlab",
|
||||
widget: {
|
||||
type: "gitlab",
|
||||
key: "gitlabapikey",
|
||||
url: "https://127.0.0.1",
|
||||
},
|
||||
};
|
||||
|
||||
describe("Gitlab Widget", () => {
|
||||
beforeEach(() => {
|
||||
__clearMocks();
|
||||
});
|
||||
|
||||
it("renders", () => {
|
||||
render(
|
||||
<SettingsContext.Provider value={{ settings: mockSettings }}>
|
||||
<Component service={service} />
|
||||
</SettingsContext.Provider>,
|
||||
);
|
||||
|
||||
expect(screen.getByText(/Groups/i)).toBeInTheDocument();
|
||||
expect(screen.getByText(/Issues/i)).toBeInTheDocument();
|
||||
expect(screen.getByText(/Merge Requests/i)).toBeInTheDocument();
|
||||
expect(screen.getByText(/Projects/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders with sample data", () => {
|
||||
__setEndpointMockData("counts", {
|
||||
data: {
|
||||
groups_count: 3,
|
||||
issues_count: 7,
|
||||
merge_requests_count: 2,
|
||||
projects_count: 5,
|
||||
},
|
||||
});
|
||||
|
||||
render(
|
||||
<SettingsContext.Provider value={{ settings: {} }}>
|
||||
<Component service={service} />
|
||||
</SettingsContext.Provider>,
|
||||
);
|
||||
|
||||
expect(screen.getByText("3")).toBeInTheDocument();
|
||||
expect(screen.getByText("7")).toBeInTheDocument();
|
||||
expect(screen.getByText("2")).toBeInTheDocument();
|
||||
expect(screen.getByText("5")).toBeInTheDocument();
|
||||
});
|
||||
});
|
Loading…
Reference in new issue