UNPKG

firmament-yargs

Version:

Typescript classes for building CLI node applications

120 lines 4.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("reflect-metadata"); const inversify_config_1 = require("../inversify.config"); const chai_1 = require("chai"); const mocha = require("mocha"); const testJsonObject = { how: 'now', brown: 'cow', dog: { bark: 'loud', weight: 50 } }; describe('Testing SafeJson Creation/Force Error', () => { let safeJson; beforeEach(() => { safeJson = inversify_config_1.default.get('SafeJson'); safeJson.forceError = false; safeJson.forceException = false; safeJson.forceExceptionWaitCount = 0; }); afterEach(() => { }); mocha.it('use mocha instance to avoid linter warning', (done) => { done(); }); }); describe('Testing SafeJsonImpl.safeParse', () => { let safeJson; beforeEach(() => { safeJson = inversify_config_1.default.get('SafeJson'); safeJson.forceError = false; safeJson.forceException = false; safeJson.forceExceptionWaitCount = 0; }); it('should have callback with error', (done) => { safeJson.forceError = true; safeJson.safeParse(undefined, (err, obj) => { chai_1.expect(err).to.exist; chai_1.expect(err.message).to.equal('force error: SafeJsonImpl.safeParse'); chai_1.expect(obj).to.not.exist; done(); }); }); it('should catch forcedException', (done) => { safeJson.forceException = true; safeJson.safeParse(undefined, (err, obj) => { chai_1.expect(err).to.exist; chai_1.expect(err.message).to.equal('forceException'); chai_1.expect(obj).to.not.exist; done(); }); }); it('should have callback with error when jsonString is undefined', (done) => { safeJson.safeParse(undefined, (err, obj) => { chai_1.expect(err).to.exist; chai_1.expect(err.message).to.equal('Unexpected token u in JSON at position 0'); chai_1.expect(obj).to.not.exist; done(); }); }); it('should have callback with error when jsonString is null', (done) => { safeJson.safeParse(null, (err, obj) => { chai_1.expect(err).to.exist; chai_1.expect(err.message).to.equal('expected null to exist'); chai_1.expect(obj).to.not.exist; done(); }); }); it('should have callback with error when jsonString is not valid JSON', (done) => { const json = 'how now brown cow'; safeJson.safeParse(json, (err, obj) => { chai_1.expect(err).to.exist; chai_1.expect(err.message).to.equal('Unexpected token h in JSON at position 0'); chai_1.expect(obj).to.not.exist; done(); }); }); it('should have callback with null error and correct object when jsonString is valid', (done) => { const json = JSON.stringify(testJsonObject); safeJson.safeParse(json, (err, obj) => { chai_1.expect(err).to.not.exist; chai_1.expect(obj).to.eql(testJsonObject); done(); }); }); }); describe('Testing SafeJsonImpl.writeFile + SafeJsonImpl.readFile (no options)', () => { let safeJson; beforeEach(() => { safeJson = inversify_config_1.default.get('SafeJson'); safeJson.forceError = false; safeJson.forceException = false; safeJson.forceExceptionWaitCount = 0; }); it('should writeTest object to tmp file, read it back (no options)', (done) => { safeJson.writeFile('/tmp/testJson.json', testJsonObject, (err) => { chai_1.expect(err).to.not.exist; safeJson.readFile('/tmp/testJson.json', (err, obj) => { chai_1.expect(err).to.not.exist; chai_1.expect(obj).to.eql(testJsonObject); done(); }); }); }); }); describe('Testing SafeJsonImpl.writeFile + SafeJsonImpl.readFile (with options)', () => { let safeJson; beforeEach(() => { safeJson = inversify_config_1.default.get('SafeJson'); safeJson.forceError = false; safeJson.forceException = false; safeJson.forceExceptionWaitCount = 0; }); it('should writeTest object to tmp file, read it back (no options)', (done) => { safeJson.writeFile('/tmp/testJson.json', testJsonObject, { spaces: 2 }, (err) => { chai_1.expect(err).to.not.exist; safeJson.readFile('/tmp/testJson.json', {}, (err, obj) => { chai_1.expect(err).to.not.exist; chai_1.expect(obj).to.eql(testJsonObject); done(); }); }); }); }); //# sourceMappingURL=safe-json.test.js.map