From d0b2a4ce73ce47030e1bc49e9b68a2bad9069ac3 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 11 Nov 2019 16:57:24 -0500 Subject: bin/server: Convert to TypeScript --- test/conf/ConfLoaderTest.js | 136 -------------------------------------- test/conf/ConfLoaderTest.ts | 156 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 136 deletions(-) delete mode 100644 test/conf/ConfLoaderTest.js create mode 100644 test/conf/ConfLoaderTest.ts (limited to 'test') diff --git a/test/conf/ConfLoaderTest.js b/test/conf/ConfLoaderTest.js deleted file mode 100644 index b942216..0000000 --- a/test/conf/ConfLoaderTest.js +++ /dev/null @@ -1,136 +0,0 @@ -/** - * Tests ConfLoader - */ - -'use strict'; - -const chai = require( 'chai' ); -const expect = chai.expect; -const { - conf: { - ConfLoader: Sut, - }, - store: { - MemoryStore: Store, - }, -} = require( '../../' ); - -chai.use( require( 'chai-as-promised' ) ); - - -describe( 'ConfLoader', () => -{ - it( "loads Store'd configuration from file", () => - { - const expected_path = "/foo/bar/baz.json"; - const expected_data = '{ "foo": "bar" }'; - - const fs = { - readFile( path, encoding, callback ) - { - expect( path ).to.equal( expected_path ); - expect( encoding ).to.equal( 'utf8' ); - - callback( null, expected_data ); - }, - }; - - return expect( - Sut( fs, Store ) - .fromFile( expected_path ) - .then( conf => conf.get( 'foo' ) ) - ).to.eventually.deep.equal( JSON.parse( expected_data ).foo ); - } ); - - - it( "fails on read error", () => - { - const expected_err = Error( 'rejected' ); - - const fs = { - readFile( _, __, callback ) - { - callback( expected_err, null ); - }, - }; - - return expect( Sut( fs ).fromFile( '' ) ) - .to.eventually.be.rejectedWith( expected_err ); - } ); - - - it( "can override #parseConfData for custom parser", () => - { - const result = { foo: {} }; - const input = "foo"; - - const fs = { - readFile( _, __, callback ) - { - callback( null, input ); - }, - }; - - const sut = Sut.extend( - { - 'override parseConfData'( given_input ) - { - expect( given_input ).to.equal( input ); - return Promise.resolve( result ); - }, - } )( fs, Store ); - - return expect( - sut.fromFile( '' ) - .then( conf => conf.get( 'foo' ) ) - ).to.eventually.equal( result.foo ); - } ); - - - it( 'rejects promise on parsing error', () => - { - const expected_err = SyntaxError( 'test parsing error' ); - - const fs = { - readFile( _, __, callback ) - { - // make async so that we clear the stack, and therefore - // try/catch - process.nextTick( () => callback( null, '' ) ); - }, - }; - - const sut = Sut.extend( - { - 'override parseConfData'( given_input ) - { - throw expected_err; - }, - } )( fs, Store ); - - return expect( sut.fromFile( '' ) ) - .to.eventually.be.rejectedWith( expected_err ); - } ); - - - it( "rejects promise on Store ctor error", () => - { - const expected_err = Error( 'test Store ctor error' ); - - const fs = { - readFile: ( _, __, callback ) => callback( null, '' ), - }; - - const badstore = () => { throw expected_err }; - - return expect( Sut( fs, badstore ).fromFile( '' ) ) - .to.eventually.be.rejectedWith( expected_err ); - } ); - - - it( "rejects promise on bad fs call", () => - { - return expect( Sut( {}, Store ).fromFile( '' ) ) - .to.eventually.be.rejected; - } ); -} ); diff --git a/test/conf/ConfLoaderTest.ts b/test/conf/ConfLoaderTest.ts new file mode 100644 index 0000000..4d71301 --- /dev/null +++ b/test/conf/ConfLoaderTest.ts @@ -0,0 +1,156 @@ +/** + * Tests ConfLoader + * + * Copyright (C) 2010-2019 R-T Specialty, LLC. + * + * This file is part of the Liza Data Collection Framework. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +const chai = require( 'chai' ); +const expect = chai.expect; + +import { readFile } from "fs"; + +import { ConfLoader as Sut } from "../../src/conf/ConfLoader"; + +type FsLike = { readFile: typeof readFile }; + +const { + store: { + MemoryStore: Store, + }, +} = require( '../../' ); + +chai.use( require( 'chai-as-promised' ) ); + + +describe( 'ConfLoader', () => +{ + it( "loads Store'd configuration from file", () => + { + const expected_path = "/foo/bar/baz.json"; + const expected_data = '{ "foo": "bar" }'; + + const fs = { + readFile( path: string, encoding: string, callback: any ) + { + expect( path ).to.equal( expected_path ); + expect( encoding ).to.equal( 'utf8' ); + + callback( null, expected_data ); + }, + }; + + return expect( + new Sut( fs, Store ) + .fromFile( expected_path ) + .then( conf => conf.get( 'foo' ) ) + ).to.eventually.deep.equal( JSON.parse( expected_data ).foo ); + } ); + + + it( "fails on read error", () => + { + const expected_err = Error( 'rejected' ); + + const fs = { + readFile( _: any, __: any, callback: any ) + { + callback( expected_err, null ); + }, + }; + + return expect( new Sut( fs, Store ).fromFile( '' ) ) + .to.eventually.be.rejectedWith( expected_err ); + } ); + + + it( "can override #parseConfData for custom parser", () => + { + const result = { foo: {} }; + const input = "foo"; + + const fs = { + readFile( _: any, __: any, callback: any ) + { + callback( null, input ); + }, + }; + + const sut = new class extends Sut + { + parseConfData( given_input: string ) + { + expect( given_input ).to.equal( input ); + return Promise.resolve( result ); + } + }( fs, Store ); + + return expect( + sut.fromFile( '' ) + .then( conf => conf.get( 'foo' ) ) + ).to.eventually.equal( result.foo ); + } ); + + + it( 'rejects promise on parsing error', () => + { + const expected_err = SyntaxError( 'test parsing error' ); + + const fs = { + readFile( _: any, __: any, callback: any ) + { + // make async so that we clear the stack, and therefore + // try/catch + process.nextTick( () => callback( null, '' ) ); + }, + }; + + const sut = new class extends Sut + { + parseConfData( _given_input: string ): never + { + throw expected_err; + } + }( fs, Store ); + + return expect( sut.fromFile( '' ) ) + .to.eventually.be.rejectedWith( expected_err ); + } ); + + + it( "rejects promise on Store ctor error", () => + { + const expected_err = Error( 'test Store ctor error' ); + + const fs = { + readFile: ( _: any, __: any, callback: any ) => + callback( null, '' ), + }; + + const badstore = () => { throw expected_err }; + + return expect( new Sut( fs, badstore ).fromFile( '' ) ) + .to.eventually.be.rejectedWith( expected_err ); + } ); + + + it( "rejects promise on bad fs call", () => + { + return expect( new Sut( {}, Store ).fromFile( '' ) ) + .to.eventually.be.rejected; + } ); +} ); -- cgit v1.2.1