declapract
Version:
A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.
131 lines • 7.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const domain_1 = require("../../../../domain");
const logger_1 = require("../../../../utils/logger");
const dirPath_1 = require("../../../__test_assets__/dirPath");
const readPracticeDeclaration_1 = require("../../../declaration/readPracticeDeclarations/readPracticeDeclaration/readPracticeDeclaration");
const getPlansForProject_1 = require("../getPlansForProject");
const displayPlan_1 = require("./displayPlan");
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => logger_1.log.debug); // swap to log debug so its not displaying during tests by default
describe('displayPlan', () => {
beforeEach(() => jest.clearAllMocks());
it('should show no change plans correctly', async () => {
const practice = await (0, readPracticeDeclaration_1.readPracticeDeclaration)({
declaredPracticeDirectory: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/src/practices/prettier`,
});
const plans = await (0, getPlansForProject_1.getPlansForProject)({
projectRootDirectory: `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-prettier`,
practices: [practice],
projectVariables: {},
});
// console.log(JSON.stringify(plans, null, 2));
// grab the no change plan
const plan = plans.find((thisPlan) => thisPlan.action === domain_1.RequiredAction.NO_CHANGE);
if (!plan)
throw new Error('expected to find the plan here');
// now display the plan
await (0, displayPlan_1.displayPlan)({ plan });
// check that it looks right
expect(logSpy).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('[NO_CHANGE]'));
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('package.json'));
expect(logSpy.mock.calls[0]).toMatchSnapshot();
});
it('should show automatically fixable plans correctly', async () => {
const practice = await (0, readPracticeDeclaration_1.readPracticeDeclaration)({
declaredPracticeDirectory: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/src/practices/prettier`,
});
const plans = await (0, getPlansForProject_1.getPlansForProject)({
projectRootDirectory: `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-prettier`,
practices: [practice],
projectVariables: {},
});
// console.log(JSON.stringify(plans, null, 2));
// grab the no change plan
const plan = plans.find((thisPlan) => thisPlan.action === domain_1.RequiredAction.FIX_AUTOMATIC);
if (!plan)
throw new Error('expected to find the plan here');
// now display the plan
await (0, displayPlan_1.displayPlan)({ plan });
// check that it looks right
expect(logSpy).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('[FIX_AUTOMATIC]'));
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('.prettierignore'));
expect(logSpy.mock.calls).toMatchSnapshot();
});
it('should show manually fixable plans correctly', async () => {
const practice = await (0, readPracticeDeclaration_1.readPracticeDeclaration)({
declaredPracticeDirectory: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/src/practices/dates-and-times`,
});
const plans = await (0, getPlansForProject_1.getPlansForProject)({
projectRootDirectory: `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-prettier`,
practices: [practice],
projectVariables: {},
});
// console.log(JSON.stringify(plans, null, 2));
// grab the no change plan
const plan = plans.find((thisPlan) => thisPlan.action === domain_1.RequiredAction.FIX_MANUAL);
if (!plan)
throw new Error('expected to find the plan here');
// now display the plan
await (0, displayPlan_1.displayPlan)({ plan });
// check that it looks right
expect(logSpy).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('[FIX_MANUAL]'));
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('package.json'));
expect(logSpy.mock.calls).toMatchSnapshot();
});
it('should show failing bad practice correctly', async () => {
const practices = await Promise.all([
await (0, readPracticeDeclaration_1.readPracticeDeclaration)({
declaredPracticeDirectory: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/src/practices/dates-and-times`,
}),
]);
const plans = await (0, getPlansForProject_1.getPlansForProject)({
projectRootDirectory: `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-dates-and-times`,
practices,
projectVariables: {},
});
// console.log(JSON.stringify(plans, null, 2));
// grab the no change plan
const plan = plans.find((thisPlan) => thisPlan.action === domain_1.RequiredAction.FIX_MANUAL);
if (!plan)
throw new Error('expected to find the plan here');
// now display the plan
await (0, displayPlan_1.displayPlan)({ plan });
// check that it looks right
expect(logSpy).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('[FIX_MANUAL]'));
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('package.json'));
expect(logSpy.mock.calls).toMatchSnapshot();
});
it('should show failing multiple practices on the same file correctly', async () => {
const practices = await Promise.all([
await (0, readPracticeDeclaration_1.readPracticeDeclaration)({
declaredPracticeDirectory: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/src/practices/dates-and-times`,
}),
await (0, readPracticeDeclaration_1.readPracticeDeclaration)({
declaredPracticeDirectory: `${dirPath_1.testAssetsDirectoryPath}/example-best-practices-repo/src/practices/prettier`,
}),
]);
const plans = await (0, getPlansForProject_1.getPlansForProject)({
projectRootDirectory: `${dirPath_1.testAssetsDirectoryPath}/example-project-fails-dates-and-times`,
practices,
projectVariables: {},
});
// console.log(JSON.stringify(plans, null, 2));
// grab the no change plan
const plan = plans.find((thisPlan) => thisPlan.action === domain_1.RequiredAction.FIX_MANUAL);
if (!plan)
throw new Error('expected to find the plan here');
// now display the plan
await (0, displayPlan_1.displayPlan)({ plan });
// check that it looks right
expect(logSpy).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('[FIX_MANUAL]'));
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('package.json'));
expect(logSpy.mock.calls).toMatchSnapshot();
});
it.todo('should show failing both manually and automatically correctly');
});
//# sourceMappingURL=displayPlan.integration.test.js.map