Skip to content

Commit

Permalink
fix: Provide full coverage for util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Oct 16, 2020
1 parent 63f891c commit c999abb
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion test/unit/util/Util.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
Expand All @@ -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')),
]);
});
});
});

0 comments on commit c999abb

Please sign in to comment.