@jungvonmatt/sb-migrate
Version:
CLI tool for managing Storyblok schema and content migrations
139 lines • 5.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const component_1 = require("../../utils/component");
const lodash_1 = require("lodash");
// Mock the @sindresorhus/is module
vitest_1.vi.mock("@sindresorhus/is", () => ({
isString: vitest_1.vi.fn(),
isNumber: vitest_1.vi.fn(),
isObject: vitest_1.vi.fn(),
}));
(0, vitest_1.describe)("Component Utilities", () => {
(0, vitest_1.describe)("bloksEmpty", () => {
(0, vitest_1.it)("should return true for empty array", () => {
(0, vitest_1.expect)((0, component_1.bloksEmpty)([])).toBe(true);
});
(0, vitest_1.it)("should return true for non-array input", () => {
(0, vitest_1.expect)((0, component_1.bloksEmpty)(null)).toBe(true);
(0, vitest_1.expect)((0, component_1.bloksEmpty)(undefined)).toBe(true);
(0, vitest_1.expect)((0, component_1.bloksEmpty)("not an array")).toBe(true);
});
(0, vitest_1.it)("should return false for non-empty array", () => {
(0, vitest_1.expect)((0, component_1.bloksEmpty)([1, 2, 3])).toBe(false);
});
});
(0, vitest_1.describe)("getPreview", () => {
let isString;
let isNumber;
let isObject;
(0, vitest_1.beforeEach)(async () => {
vitest_1.vi.clearAllMocks();
const module = await import("@sindresorhus/is");
isString = module.isString;
isNumber = module.isNumber;
isObject = module.isObject;
});
(0, vitest_1.it)("should handle string input", async () => {
// Arrange
const input = "This is a long string that should be truncated";
isString.mockReturnValue(true);
isNumber.mockReturnValue(false);
isObject.mockReturnValue(false);
// Act
const result = await (0, component_1.getPreview)(input);
// Assert
(0, vitest_1.expect)(result).toBe((0, lodash_1.truncate)(input));
(0, vitest_1.expect)(isString).toHaveBeenCalledWith(input);
});
(0, vitest_1.it)("should handle number input", async () => {
// Arrange
const input = 42;
isString.mockReturnValue(false);
isNumber.mockReturnValue(true);
isObject.mockReturnValue(false);
// Act
const result = await (0, component_1.getPreview)(input);
// Assert
(0, vitest_1.expect)(result).toBe(input);
(0, vitest_1.expect)(isNumber).toHaveBeenCalledWith(input);
});
(0, vitest_1.it)("should handle array input", async () => {
// Arrange
const input = ["first", "second", "third"];
isString.mockReturnValue(false);
isNumber.mockReturnValue(false);
isObject.mockReturnValue(false);
// Mock the recursive call for the first element
isString.mockReturnValueOnce(true);
// Act
const result = await (0, component_1.getPreview)(input);
// Assert
(0, vitest_1.expect)(result).toBe(input.join(","));
});
(0, vitest_1.it)("should handle object input", async () => {
// Arrange
const input = { key: "value", nested: { data: 123 } };
isString.mockReturnValue(false);
isNumber.mockReturnValue(false);
isObject.mockReturnValue(true);
// Act
const result = await (0, component_1.getPreview)(input);
// Assert
(0, vitest_1.expect)(result).toBe((0, lodash_1.truncate)(JSON.stringify(input)));
(0, vitest_1.expect)(isObject).toHaveBeenCalledWith(input);
});
(0, vitest_1.it)("should handle null input", async () => {
// Arrange
const input = null;
isString.mockReturnValue(false);
isNumber.mockReturnValue(false);
isObject.mockReturnValue(false);
// Act
const result = await (0, component_1.getPreview)(input);
// Assert
(0, vitest_1.expect)(result).toBe(input);
});
(0, vitest_1.it)("should handle undefined input", async () => {
// Arrange
const input = undefined;
isString.mockReturnValue(false);
isNumber.mockReturnValue(false);
isObject.mockReturnValue(false);
// Act
const result = await (0, component_1.getPreview)(input);
// Assert
(0, vitest_1.expect)(result).toBe(input);
});
(0, vitest_1.it)("should handle nested array input", async () => {
// Arrange
const input = [["nested", "array"], "second"];
isString.mockReturnValue(false);
isNumber.mockReturnValue(false);
isObject.mockReturnValue(false);
// Mock the recursive calls
isString.mockReturnValueOnce(false);
isString.mockReturnValueOnce(true);
// Act
const result = await (0, component_1.getPreview)(input);
// Assert
(0, vitest_1.expect)(result).toBe(`[${input[0].join(",")}]`);
});
(0, vitest_1.it)("should handle complex nested object input", async () => {
// Arrange
const input = {
name: "Test",
items: [1, 2, 3],
nested: { key: "value" },
};
isString.mockReturnValue(false);
isNumber.mockReturnValue(false);
isObject.mockReturnValue(true);
// Act
const result = await (0, component_1.getPreview)(input);
// Assert
(0, vitest_1.expect)(result).toBe((0, lodash_1.truncate)(JSON.stringify(input)));
});
});
});
//# sourceMappingURL=component.test.js.map