From c999abb7b074bd2f0b93c9cfca198324ec9b43ef Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Wed, 14 Oct 2020 16:30:53 +0200 Subject: [PATCH] fix: Provide full coverage for util functions --- test/unit/util/Util.test.ts | 46 ++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/test/unit/util/Util.test.ts b/test/unit/util/Util.test.ts index ab903016f..b0344f8b2 100644 --- a/test/unit/util/Util.test.ts +++ b/test/unit/util/Util.test.ts @@ -1,9 +1,12 @@ +import { PassThrough } from 'stream'; +import { DataFactory } from 'n3'; +import type { Quad } from 'rdf-js'; import streamifyArray from 'streamify-array'; import { decodeUriPathComponents, encodeUriPathComponents, ensureTrailingSlash, - matchingMediaType, + matchingMediaType, pipeStreamsAndErrors, pushQuad, readableToString, toCanonicalUriPath, } from '../../../src/util/Util'; @@ -39,6 +42,37 @@ describe('Util function', (): void => { }); }); + describe('pipeStreamsAndErrors', (): void => { + it('pipes data from one stream to the other.', async(): Promise => { + const input = streamifyArray([ 'data' ]); + const output = new PassThrough(); + pipeStreamsAndErrors(input, output); + await expect(readableToString(output)).resolves.toEqual('data'); + }); + + it('pipes errors from one stream to the other.', async(): Promise => { + const input = streamifyArray([ 'data' ]); + input.read = (): any => { + input.emit('error', new Error('error')); + return null; + }; + const output = new PassThrough(); + pipeStreamsAndErrors(input, output); + await expect(readableToString(output)).rejects.toThrow(new Error('error')); + }); + + it('supports mapping errors to something else.', async(): Promise => { + const input = streamifyArray([ 'data' ]); + input.read = (): any => { + input.emit('error', new Error('error')); + return null; + }; + const output = new PassThrough(); + pipeStreamsAndErrors(input, output, (): any => new Error('other error')); + await expect(readableToString(output)).rejects.toThrow(new Error('other error')); + }); + }); + describe('UriPath functions', (): void => { it('makes sure only the necessary parts are encoded with toCanonicalUriPath.', async(): Promise => { expect(toCanonicalUriPath('/a%20path&/name')).toEqual('/a%20path%26/name'); @@ -52,4 +86,14 @@ describe('Util function', (): void => { expect(encodeUriPathComponents('/a%20path&/name')).toEqual('/a%2520path%26/name'); }); }); + + describe('pushQuad', (): void => { + it('creates a quad and adds it to the given array.', async(): Promise => { + const quads: Quad[] = []; + pushQuad(quads, DataFactory.namedNode('sub'), DataFactory.namedNode('pred'), DataFactory.literal('obj')); + expect(quads).toEqualRdfQuadArray([ + DataFactory.quad(DataFactory.namedNode('sub'), DataFactory.namedNode('pred'), DataFactory.literal('obj')), + ]); + }); + }); });