UNPKG

declapract

Version:

A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.

180 lines 9.43 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const domain_1 = require("../../../../../domain"); const FileCheckDeclarationInput_1 = require("../../../../../domain/objects/FileCheckDeclarationInput"); const doesFileExist_1 = require("../../../../../utils/fileio/doesFileExist"); const importExportsFromFile_1 = require("../../../../../utils/fileio/importExportsFromFile"); const UserInputError_1 = require("../../../../UserInputError"); const getHydratedCheckInputsForFile_1 = require("./getHydratedCheckInputsForFile"); jest.mock('../../../../../utils/fileio/doesFileExist'); const doesFileExistMock = doesFileExist_1.doesFileExist; jest.mock('../../../../../utils/fileio/importExportsFromFile'); const importExportsFromFileMock = importExportsFromFile_1.importExportsFromFile; describe('getHydratedCheckInputsForFile', () => { beforeEach(() => { jest.resetAllMocks(); // reset to remove inter test state doesFileExistMock.mockReturnValue(false); // default to file does not exist importExportsFromFileMock.mockReturnValue({}); // default to no exports specified }); it('should lookup the file at the correct file path', () => __awaiter(void 0, void 0, void 0, function* () { yield (0, getHydratedCheckInputsForFile_1.getHydratedCheckInputsForFile)({ declaredProjectDirectory: '__dir__', declaredFileCorePath: '__path__', }); expect(doesFileExistMock).toHaveBeenCalledTimes(1); expect(doesFileExistMock).toHaveBeenCalledWith({ filePath: '__dir__/__path__.declapract.ts', }); })); it('should return null if the file does not exist', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, getHydratedCheckInputsForFile_1.getHydratedCheckInputsForFile)({ declaredProjectDirectory: '__dir__', declaredFileCorePath: '__path__', }); expect(result).toEqual({ declaredCheckInputs: null, declaredContentsFunction: null, declaredFixFunction: null, }); })); it('should throw an error if file exists but check was not exported', () => __awaiter(void 0, void 0, void 0, function* () { doesFileExistMock.mockReturnValue(true); try { yield (0, getHydratedCheckInputsForFile_1.getHydratedCheckInputsForFile)({ declaredProjectDirectory: '__dir__', declaredFileCorePath: '__path__', }); fail('should not reach here'); } catch (error) { expect(error).toBeInstanceOf(UserInputError_1.UserInputError); } })); describe('shorthands', () => { it('should define correctly when type shorthand is used', () => __awaiter(void 0, void 0, void 0, function* () { doesFileExistMock.mockReturnValue(true); importExportsFromFileMock.mockResolvedValue({ check: domain_1.FileCheckType.CONTAINS, }); const result = yield (0, getHydratedCheckInputsForFile_1.getHydratedCheckInputsForFile)({ declaredProjectDirectory: '__dir__', declaredFileCorePath: '__path__', }); expect(result).toEqual({ declaredCheckInputs: new FileCheckDeclarationInput_1.FileCheckDeclarationInput({ type: domain_1.FileCheckType.CONTAINS, }), declaredContentsFunction: null, declaredFixFunction: null, }); })); it('should throw an error if CUSTOM was used as a type shorthand', () => __awaiter(void 0, void 0, void 0, function* () { doesFileExistMock.mockReturnValue(true); importExportsFromFileMock.mockResolvedValue({ check: domain_1.FileCheckType.CUSTOM, }); try { yield (0, getHydratedCheckInputsForFile_1.getHydratedCheckInputsForFile)({ declaredProjectDirectory: '__dir__', declaredFileCorePath: '__path__', }); fail('should not reach here'); } catch (error) { expect(error).toBeInstanceOf(UserInputError_1.UserInputError); expect(error.message).toContain('File check type can not be CUSTOM without the function being specified'); } })); it('should define correctly when function shorthand is used', () => __awaiter(void 0, void 0, void 0, function* () { doesFileExistMock.mockReturnValue(true); importExportsFromFileMock.mockResolvedValue({ check: () => __awaiter(void 0, void 0, void 0, function* () { }) }); const result = yield (0, getHydratedCheckInputsForFile_1.getHydratedCheckInputsForFile)({ declaredProjectDirectory: '__dir__', declaredFileCorePath: '__path__', }); expect(result).toEqual({ declaredCheckInputs: { type: domain_1.FileCheckType.CUSTOM, function: expect.any(Function), }, declaredContentsFunction: null, declaredFixFunction: null, }); })); }); describe('full definitions', () => { it('should allow user to specify an optional check', () => __awaiter(void 0, void 0, void 0, function* () { doesFileExistMock.mockReturnValue(true); importExportsFromFileMock.mockResolvedValue({ check: { optional: true }, }); const result = yield (0, getHydratedCheckInputsForFile_1.getHydratedCheckInputsForFile)({ declaredProjectDirectory: '__dir__', declaredFileCorePath: '__path__', }); expect(result).toEqual({ declaredCheckInputs: { optional: true }, declaredContentsFunction: null, declaredFixFunction: null, }); })); it('should allow user to specify an optional contains check', () => __awaiter(void 0, void 0, void 0, function* () { doesFileExistMock.mockReturnValue(true); importExportsFromFileMock.mockResolvedValue({ check: { type: domain_1.FileCheckType.CONTAINS, optional: true }, }); const result = yield (0, getHydratedCheckInputsForFile_1.getHydratedCheckInputsForFile)({ declaredProjectDirectory: '__dir__', declaredFileCorePath: '__path__', }); expect(result).toEqual({ declaredCheckInputs: { type: domain_1.FileCheckType.CONTAINS, optional: true }, declaredContentsFunction: null, declaredFixFunction: null, }); })); it('should allow user to specify an optional custom check', () => __awaiter(void 0, void 0, void 0, function* () { doesFileExistMock.mockReturnValue(true); importExportsFromFileMock.mockResolvedValue({ check: { optional: true, function: () => { } }, }); const result = yield (0, getHydratedCheckInputsForFile_1.getHydratedCheckInputsForFile)({ declaredProjectDirectory: '__dir__', declaredFileCorePath: '__path__', }); expect(result).toEqual({ declaredCheckInputs: { optional: true, function: expect.any(Function) }, declaredContentsFunction: null, declaredFixFunction: null, }); })); it('should throw an error if user tries to specify type other than CUSTOM and also provides a custom function', () => __awaiter(void 0, void 0, void 0, function* () { doesFileExistMock.mockReturnValue(true); importExportsFromFileMock.mockResolvedValue({ check: { type: domain_1.FileCheckType.EQUALS, function: () => { } }, declaredFixFunction: null, }); try { yield (0, getHydratedCheckInputsForFile_1.getHydratedCheckInputsForFile)({ declaredProjectDirectory: '__dir__', declaredFileCorePath: '__path__', }); fail('should not reach here'); } catch (error) { expect(error).toBeInstanceOf(UserInputError_1.UserInputError); expect(error.message).toContain('If check.function is defined then the type can not be defined as anything but FileCheckType.CUSTOM'); } })); }); }); //# sourceMappingURL=getHydratedCheckInputsForFile.test.js.map