@critters/next
Version:
Secure bug reporting library for Next.js applications
181 lines (180 loc) • 8.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Unit tests for BugTrackerConfig
*/
const config_1 = require("../src/config");
// Store original process.env
const originalProcessEnv = process.env;
describe("BugTrackerConfig", () => {
beforeEach(() => {
// Reset module registry for each test
jest.resetModules();
// Reset process.env before each test
process.env = Object.assign({}, originalProcessEnv);
// Reset singleton instance between tests using private method
// @ts-ignore - Ignoring TypeScript error to access private static property for testing
config_1.BugTrackerConfig.instance = undefined;
});
afterAll(() => {
// Restore original process.env
process.env = originalProcessEnv;
});
describe("init and getInstance", () => {
it("should create a new instance with defaults when initialized", () => {
const config = config_1.BugTrackerConfig.init({ apiKey: "test-api-key" });
expect(config).toBeInstanceOf(config_1.BugTrackerConfig);
expect(config.getApiKey()).toBe("test-api-key");
expect(config.isEnabled()).toBe(true);
expect(config.isDebugMode()).toBe(false);
expect(config.getEnvironment()).toBe("development");
});
it("should respect environment from process.env", () => {
// Use Object.assign to set read-only property for testing
Object.assign(process.env, { NODE_ENV: "production" });
const config = config_1.BugTrackerConfig.init({ apiKey: "test-api-key" });
expect(config.getEnvironment()).toBe("production");
});
it("should override default values with provided config", () => {
const options = {
apiKey: "test-api-key",
endpoint: "https://custom-endpoint.example.com",
secretKey: "test-secret-key",
projectId: "test-project",
environment: "staging",
enabled: false,
debug: true,
captureConsole: false,
captureNetworkRequests: false,
captureUserInteractions: false,
captureApplicationState: false,
};
const config = config_1.BugTrackerConfig.init(options);
expect(config.getApiKey()).toBe("test-api-key");
expect(config.getEndpoint()).toBe("https://custom-endpoint.example.com");
expect(config.getSecretKey()).toBe("test-secret-key");
expect(config.getProjectId()).toBe("test-project");
expect(config.getEnvironment()).toBe("staging");
expect(config.isEnabled()).toBe(false);
expect(config.isDebugMode()).toBe(true);
expect(config.shouldCaptureConsole()).toBe(false);
expect(config.shouldCaptureNetworkRequests()).toBe(false);
expect(config.shouldCaptureUserInteractions()).toBe(false);
expect(config.shouldCaptureApplicationState()).toBe(false);
});
it("should throw error when getInstance called before init", () => {
expect(() => config_1.BugTrackerConfig.getInstance()).toThrow("BugTrackerConfig not initialized. Call init() first.");
});
it("should return the same instance when init called multiple times", () => {
const config1 = config_1.BugTrackerConfig.init({ apiKey: "test-api-key" });
const config2 = config_1.BugTrackerConfig.init({ apiKey: "another-key" });
expect(config1).toBe(config2);
// Should update with new values
expect(config2.getApiKey()).toBe("another-key");
});
it("should return the same instance via getInstance after init", () => {
const config1 = config_1.BugTrackerConfig.init({ apiKey: "test-api-key" });
const config2 = config_1.BugTrackerConfig.getInstance();
expect(config1).toBe(config2);
});
});
describe("configuration getters", () => {
let config;
beforeEach(() => {
config = config_1.BugTrackerConfig.init({
apiKey: "test-api-key",
secretKey: "test-secret-key",
projectId: "test-project",
environment: "testing",
});
});
it("should get API key", () => {
expect(config.getApiKey()).toBe("test-api-key");
});
it("should get secret key", () => {
expect(config.getSecretKey()).toBe("test-secret-key");
});
it("should get project ID", () => {
expect(config.getProjectId()).toBe("test-project");
});
it("should get environment", () => {
expect(config.getEnvironment()).toBe("testing");
});
it("should get default endpoint when not specified", () => {
expect(config.getEndpoint()).toBe("https://bugtrack.critters.dev/api/bugs");
});
it("should get custom endpoint when specified", () => {
config.update({ endpoint: "https://custom.example.com" });
expect(config.getEndpoint()).toBe("https://custom.example.com");
});
});
describe("configuration setters", () => {
let config;
beforeEach(() => {
config = config_1.BugTrackerConfig.init({ apiKey: "initial-api-key" });
});
it("should update configuration", () => {
config.update({
apiKey: "updated-api-key",
secretKey: "updated-secret-key",
projectId: "updated-project-id",
environment: "updated-env",
enabled: false,
debug: true,
});
expect(config.getApiKey()).toBe("updated-api-key");
expect(config.getSecretKey()).toBe("updated-secret-key");
expect(config.getProjectId()).toBe("updated-project-id");
expect(config.getEnvironment()).toBe("updated-env");
expect(config.isEnabled()).toBe(false);
expect(config.isDebugMode()).toBe(true);
});
it("should set secret key", () => {
config.setSecretKey("direct-secret-update");
expect(config.getSecretKey()).toBe("direct-secret-update");
});
it("should set project ID", () => {
config.setProjectId("direct-project-update");
expect(config.getProjectId()).toBe("direct-project-update");
});
});
describe("feature flag getters", () => {
it("should return default feature flags", () => {
const config = config_1.BugTrackerConfig.init({ apiKey: "test-api-key" });
expect(config.shouldCaptureConsole()).toBe(true);
expect(config.shouldCaptureNetworkRequests()).toBe(true);
expect(config.shouldCaptureUserInteractions()).toBe(true);
expect(config.shouldCaptureApplicationState()).toBe(true);
});
it("should return custom feature flags", () => {
const config = config_1.BugTrackerConfig.init({
apiKey: "test-api-key",
captureConsole: false,
captureNetworkRequests: true,
captureUserInteractions: false,
captureApplicationState: true,
});
expect(config.shouldCaptureConsole()).toBe(false);
expect(config.shouldCaptureNetworkRequests()).toBe(true);
expect(config.shouldCaptureUserInteractions()).toBe(false);
expect(config.shouldCaptureApplicationState()).toBe(true);
});
});
describe("getConfig", () => {
it("should return a copy of the config", () => {
const config = config_1.BugTrackerConfig.init({
apiKey: "test-api-key",
secretKey: "test-secret-key",
});
const configCopy = config.getConfig();
expect(configCopy).toEqual(expect.objectContaining({
apiKey: "test-api-key",
secretKey: "test-secret-key",
enabled: true,
}));
// Ensure it's a copy, not a reference
configCopy.apiKey = "modified-key";
expect(config.getApiKey()).toBe("test-api-key");
});
});
});