UNPKG

validata

Version:

Type safe data validation and sanitization

65 lines 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.expectValue = exports.expectSuccess = exports.expectIssue = exports.runTests = void 0; const types_1 = require("./types"); const runTests = (fut, ...tests) => { tests.forEach((test) => { const result = fut.process(test.input); if (test.issues) { expect(result).toBeDefined(); expect((0, types_1.isIssue)(result)).toBeTruthy(); if (result && (0, types_1.isIssue)(result)) { test.issues.forEach((issue) => { expect(result.issues).toEqual(expect.arrayContaining([ expect.objectContaining({ path: issue.path || [], reason: issue.reason, }), ])); }); } } else { expect(result).toBeDefined(); expect((0, types_1.isIssue)(result)).toBeFalsy(); if (result && !(0, types_1.isIssue)(result)) { expect(result.value).toEqual(test.expect); } } }); }; exports.runTests = runTests; const expectIssue = (fut, value, reason, path = []) => { const result = fut.process(value); if (!(0, types_1.isIssue)(result)) { throw new Error('no issue'); } expect(result.issues).toEqual(expect.arrayContaining([ expect.objectContaining({ path, reason, }), ])); }; exports.expectIssue = expectIssue; const expectSuccess = (fut, value) => { const result = fut.process(value); expect(result).toBeDefined(); if ((0, types_1.isIssue)(result)) { throw new Error(`Unexpected issue: ${JSON.stringify(result)}`); } return result; }; exports.expectSuccess = expectSuccess; const expectValue = (fut, value, coerced) => { const result = fut.process(value); expect(result).toBeDefined(); if ((0, types_1.isIssue)(result)) { throw new Error(`Unexpected issue: ${JSON.stringify(result)}`); } if (result) { expect(result.value).toEqual(coerced); } }; exports.expectValue = expectValue; //# sourceMappingURL=test-helpers.js.map