next-affected
Version:
CLI tool to list Next.js pages affected by changes
95 lines (94 loc) • 4.08 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const utils_1 = require("./utils");
jest.mock("path", () => ({
join: jest.fn(),
resolve: jest.fn(),
}));
describe("isPage", () => {
const config = {
pagesDirectories: ["pages", "src/pages"],
excludedExtensions: [".css", ".scss"],
excludedPaths: [],
};
beforeEach(() => {
jest.clearAllMocks();
path_1.default.join.mockImplementation((...args) => args.join("/"));
path_1.default.resolve.mockImplementation((p) => p);
});
it("should return true if modulePath is in a pages directory", () => {
const projectDir = "/project";
const modulePath = "/project/pages/home.js";
expect((0, utils_1.isPage)(modulePath, projectDir, config)).toBe(true);
});
it("should return false if modulePath is not in a pages directory", () => {
const projectDir = "/project";
const modulePath = "/project/components/header.js";
expect((0, utils_1.isPage)(modulePath, projectDir, config)).toBe(false);
});
it("should normalize paths before comparison", () => {
const projectDir = "/project";
const modulePath = "/project/pages\\home.js"; // Using backslash to test normalization
expect((0, utils_1.isPage)(modulePath, projectDir, config)).toBe(true);
});
});
describe("getRouteFromPage", () => {
const config = {
pagesDirectories: ["pages", "src/pages"],
excludedExtensions: [".css", ".scss"],
excludedPaths: [],
};
beforeEach(() => {
jest.clearAllMocks();
path_1.default.join.mockImplementation((...args) => args.join("/"));
path_1.default.resolve.mockImplementation((p) => p);
});
it("should return the correct route for a page", () => {
const projectDir = "/project";
const pagePath = "/project/pages/home.js";
expect((0, utils_1.getRouteFromPage)(pagePath, projectDir, config)).toBe("/home");
});
it("should return root route for index.js", () => {
const projectDir = "/project";
const pagePath = "/project/pages/index.js";
expect((0, utils_1.getRouteFromPage)(pagePath, projectDir, config)).toBe("/index");
});
it("should normalize paths and handle various extensions", () => {
const projectDir = "/project";
const pagePath = "/project/src/pages/blog/post.tsx";
expect((0, utils_1.getRouteFromPage)(pagePath, projectDir, config)).toBe("/blog/post");
});
});
describe("normalizePath", () => {
it("should normalize Windows paths to POSIX format", () => {
const windowsPath = "C:\\project\\pages\\home.js";
path_1.default.resolve.mockReturnValue(windowsPath);
expect((0, utils_1.normalizePath)(windowsPath)).toBe("C:/project/pages/home.js");
});
it("should resolve and normalize paths", () => {
const filePath = "pages/home.js";
path_1.default.resolve.mockReturnValue("/project/pages/home.js");
expect((0, utils_1.normalizePath)(filePath)).toBe("/project/pages/home.js");
});
});
describe("shouldExcludeModule", () => {
const config = {
pagesDirectories: ["pages", "src/pages"],
excludedExtensions: [".css", ".scss"],
excludedPaths: [],
};
it("should return true if the module path ends with an excluded extension", () => {
const modulePath = "/project/styles/main.css";
const projectDir = "/project";
expect((0, utils_1.shouldExcludeModule)(modulePath, config, projectDir)).toBe(true);
});
it("should return false if the module path does not end with an excluded extension", () => {
const modulePath = "/project/pages/home.js";
const projectDir = "/project";
expect((0, utils_1.shouldExcludeModule)(modulePath, config, projectDir)).toBe(false);
});
});