fix: Provide full coverage for util functions

This commit is contained in:
Joachim Van Herwegen 2020-10-14 16:30:53 +02:00
parent 63f891c0f1
commit c999abb7b0

View File

@ -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<void> => {
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<void> => {
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<void> => {
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<void> => {
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<void> => {
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')),
]);
});
});
});