declapract
Version:
A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.
171 lines • 8.29 kB
JavaScript
"use strict";
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', async () => {
await (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', async () => {
const result = await (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', async () => {
doesFileExistMock.mockReturnValue(true);
try {
await (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', async () => {
doesFileExistMock.mockReturnValue(true);
importExportsFromFileMock.mockResolvedValue({
check: domain_1.FileCheckType.CONTAINS,
});
const result = await (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', async () => {
doesFileExistMock.mockReturnValue(true);
importExportsFromFileMock.mockResolvedValue({
check: domain_1.FileCheckType.CUSTOM,
});
try {
await (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', async () => {
doesFileExistMock.mockReturnValue(true);
importExportsFromFileMock.mockResolvedValue({ check: async () => { } });
const result = await (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', async () => {
doesFileExistMock.mockReturnValue(true);
importExportsFromFileMock.mockResolvedValue({
check: { optional: true },
});
const result = await (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', async () => {
doesFileExistMock.mockReturnValue(true);
importExportsFromFileMock.mockResolvedValue({
check: { type: domain_1.FileCheckType.CONTAINS, optional: true },
});
const result = await (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', async () => {
doesFileExistMock.mockReturnValue(true);
importExportsFromFileMock.mockResolvedValue({
check: { optional: true, function: () => { } },
});
const result = await (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', async () => {
doesFileExistMock.mockReturnValue(true);
importExportsFromFileMock.mockResolvedValue({
check: { type: domain_1.FileCheckType.EQUALS, function: () => { } },
declaredFixFunction: null,
});
try {
await (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