Skip to content

Commit

Permalink
fix(test): cover async, buffer, map, and reflect utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Mar 30, 2020
1 parent 89c13b6 commit 852045f
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
15 changes: 15 additions & 0 deletions test/utils/TestAsync.ts
@@ -0,0 +1,15 @@
import { expect } from 'chai';

import { defer, timeout } from '../../src/utils/Async';
import { describeLeaks, itLeaks } from '../helpers/async';
import { TimeoutError } from '../../src';

describeLeaks('async utils', async () => {
describeLeaks('defer', async () => {
itLeaks('should resolve', async () => expect(defer(10, true)).to.eventually.equal(true));
});

describeLeaks('timeout', async () => {
itLeaks('should reject slow promises', async () => expect(timeout(10, defer(20))).to.eventually.be.rejectedWith(TimeoutError));
});
});
33 changes: 33 additions & 0 deletions test/utils/TestBuffer.ts
@@ -0,0 +1,33 @@
import { expect } from 'chai';

import { concat, encode } from '../../src/utils/Buffer';
import { describeLeaks, itLeaks } from '../helpers/async';

describeLeaks('buffer utils', async () => {
describeLeaks('concat', async () => {
itLeaks('should append chunk buffers', async () => {
expect(concat([
Buffer.from('hello'),
Buffer.from('world'),
])).to.deep.equal(Buffer.from('helloworld'));
});
});

describeLeaks('encode', async () => {
itLeaks('should encode chunk buffers', async () => {
expect(encode([
Buffer.from('hello world'),
], 'utf-8')).to.equal('hello world');
});

itLeaks('should encode no buffers', async () => {
expect(encode([], 'utf-8')).to.equal('');
});

itLeaks('should encode empty buffers', async () => {
expect(encode([
new Buffer(0),
], 'utf-8')).to.equal('');
});
});
});
68 changes: 67 additions & 1 deletion test/utils/TestMap.ts
Expand Up @@ -10,6 +10,9 @@ import {
makeMap,
mustGet,
pairsToMap,
setOrPush,
mergeMap,
pushMergeMap,
} from '../../src/utils/Map';
import { describeLeaks, itLeaks } from '../helpers/async';

Expand Down Expand Up @@ -102,7 +105,10 @@ describeLeaks('map utils', async () => {
expect(getOrDefault(singleItem, 'missing', DEFAULT_VALUE)).to.equal(DEFAULT_VALUE);
});

xit('should return the default for nil values');
it('should return the default for nil values', () => {
expect(getOrDefault(multiItem, 'nilKey', DEFAULT_VALUE)).to.equal(DEFAULT_VALUE);
});

xit('should return falsy values for existing keys');
});

Expand Down Expand Up @@ -140,4 +146,64 @@ describeLeaks('map utils', async () => {
expect(entriesOf(undefined)).to.deep.equal([]);
});
});

describe('set or push helper', () => {
xit('should insert new lists');

it('should wrap new items', () => {
const values = new Map<string, Array<string>>([]);
setOrPush(values, mapKey, 'fin');

expect(Array.from(values.keys()).length).to.equal(1);
expect(mustGet(values, mapKey).length).to.equal(1);
});

it('should append to existing items', () => {
const values = new Map(multiItem);
setOrPush(values, mapKey, 'fin');

expect(Array.from(values.keys()).length).to.equal(3);
expect(mustGet(values, mapKey).length).to.equal(2);
});
});

describe('merge map helper', () => {
it('should combine two maps', () => {
const firstValue = new Map([[mapKey, mapValue]]);
const otherValue = new Map([['foo', 'bar']]);

const merged = mergeMap(firstValue, otherValue);

expect(Array.from(merged.keys()).length).to.equal(2);
});
});

describe('push merge map helper', () => {
it('should combine two overlapping maps', () => {
const firstValue = new Map([[mapKey, [1]]]);
const otherValue = new Map([[mapKey, [2, 3]]]);

const merged = pushMergeMap(firstValue, otherValue);
expect(merged).to.have.keys(mapKey);
expect(merged.get(mapKey)).to.deep.equal([1, 2, 3]);
});
});

describe('make map helper', () => {
it('should make an empty map', () => {
expect(makeMap(undefined).size).to.equal(0);
});

it('should copy an existing map', () => {
const copy = makeMap(singleItem);
expect(copy).to.deep.equal(singleItem);
expect(copy).not.to.equal(singleItem);
});

it('should copy a dict', () => {
expect(makeMap({
[mapKey]: mapValue,
})).to.deep.equal(singleItem);
});
});
});
18 changes: 18 additions & 0 deletions test/utils/TestReflect.ts
@@ -0,0 +1,18 @@
import { expect } from 'chai';
import { getMethods } from '../../src/utils/Reflect';

describe('reflect utils', () => {
describe('get methods helper', () => {
it('should collect method functions', () => {
class Test {
public foo() { /* noop */ }
public bar() { /* noop */ }
}

const methods = getMethods(new Test()).values();
/* eslint-disable @typescript-eslint/unbound-method */
expect(methods).to.include(Test.prototype.foo);
expect(methods).to.include(Test.prototype.bar);
});
});
});

0 comments on commit 852045f

Please sign in to comment.