UNPKG

mobitel-json-schema-template

Version:
150 lines (120 loc) 3.81 kB
/* eslint no-undefined: 0, func-names: 0, prefer-arrow-callback: 0, max-nested-callbacks: 0 */ const assert = require('assert'); const jst = require('../../index.js'); const has = function objHasProperty(obj, property) { return Object.prototype.hasOwnProperty.call(obj, property); }; module.exports = function testsRef() { it('should be function', function() { assert.equal(typeof jst.ref, 'function'); }); describe('should throw error when', function() { const test = (...args) => { let error; try { if (args.length > 0) { jst.ref(...args); } else { jst.ref(); } } catch (err) { error = err; } assert.equal((error instanceof Error), true, 'No Error instance'); assert.equal(error.name, 'JSONSchemaTemplateError', 'No JSONSchemaTemplateError instance'); assert.equal(!!error.message, true, 'No error message'); }; it('call without arguments', function() { test(); }); it('call with null', function() { test(null); }); it('call with true', function() { test(true); }); it('call with false', function() { test(false); }); it('call with NaN', function() { test(NaN); }); it('call with Function', function() { test(() => 10); }); it('call with Number', function() { test(1); }); it('call with Number as 0', function() { test(0); }); it('call with float Number', function() { test(1.2); }); it('call with String', function() { test('string'); }); it('call with empty String', function() { test(''); }); it('call with Array', function() { test( [ {type: 'string'}, {type: 'number'}, {type: 'null'}, ] ); }); it('call with empty Array', function() { test([]); }); it('call with Array where one value is wrong', function() { test( [ {type: 'string'}, {type: 'number'}, 3, ] ); }); it('call with Object', function() { test({type: 'string'}); }); it('call with empty Object', function() { test({}); }); it('call with Symbol', function() { test(Symbol('test')); }); it('call with RegExp', function() { test(/^\d*$/); }); it('call with multiple arguments where one argument is wrong', function() { test( [ {type: 'string'}, {type: 'number'}, 3, ], {type: 'integer'}, {type: 'boolean'}, {type: 'string'} ); }); }); describe('should return object when', function() { const test = (...args) => { const result = jst.ref(...args); assert.equal(typeof result, 'object'); assert.equal(has(result, '$ref'), true); assert.equal(result.$ref, args[0]); }; it(`call with '#/definitions/address'`, function() { test('#/definitions/address'); }); it(`call with 'definitions.json#/address'`, function() { test('definitions.json#/address'); }); }); };