Start tests

This commit is contained in:
2021-11-26 14:31:59 -06:00
parent d251f8bc15
commit bd7d6a2dbd
7 changed files with 857 additions and 5 deletions

View File

@@ -0,0 +1,249 @@
import { expect } from 'chai'
import * as sinon from 'sinon'
import { Pipe, AsyncPipe } from '../../../lib'
describe('util -> support -> Pipe', () => {
describe('static.wrap', () => {
it('should return a Pipe containing the input value', () => {
const sym = Symbol()
const pipe = Pipe.wrap(sym)
expect(pipe).to.be.an.instanceof(Pipe)
expect(pipe.get()).to.be.equal(sym)
})
})
describe('constructor', () => {
it('should instantiate a Pipe with the input value', () => {
const sym = Symbol()
const pipe = new Pipe(sym)
expect(pipe).to.be.an.instanceof(Pipe)
expect(pipe.get()).to.be.equal(sym)
})
})
describe('tap', () => {
const pipe = Pipe.wrap(123)
it('should call the input operator with the value of the pipe', () => {
const spy = sinon.spy()
pipe.tap(spy)
expect(spy.callCount).to.be.equal(1)
expect(spy.firstCall.firstArg).to.be.equal(123)
})
it('should return a new Pipe', () => {
const ret = pipe.tap(x => x)
expect(ret).to.be.an.instanceof(Pipe)
expect(ret).to.not.be.equal(pipe)
})
it('should return the operator value in the new pipe', () => {
const ret = pipe.tap(() => 456)
expect(ret.get()).to.be.equal(456)
})
it('should not modify the base pipe', () => {
pipe.tap(() => false)
expect(pipe.get()).to.be.equal(123)
})
})
describe('peek', () => {
const pipe = Pipe.wrap(123)
it('should call the input operator with the value of the pipe', () => {
const spy = sinon.spy()
pipe.peek(spy)
expect(spy.callCount).to.be.equal(1)
expect(spy.firstCall.firstArg).to.be.equal(123)
})
it('should return the same pipe', () => {
const ret = pipe.peek(x => x)
expect(ret).to.be.eql(pipe)
})
it('should not modify the base pipe', () => {
pipe.peek(() => false)
expect(pipe.get()).to.be.equal(123)
})
})
describe('when', () => {
const pipe = Pipe.wrap(123)
describe('condition is true', () => {
it('should call the input operator with the value of the pipe', () => {
const spy = sinon.spy()
pipe.when(true, spy)
expect(spy.callCount).to.be.equal(1)
expect(spy.firstCall.firstArg).to.be.equal(123)
})
it('should return a new Pipe', () => {
const ret = pipe.when(true, x => x)
expect(ret).to.be.an.instanceof(Pipe)
expect(ret).to.not.be.equal(pipe)
})
it('should return the operator value in the new pipe', () => {
const ret = pipe.when(true, () => 456)
expect(ret.get()).to.be.equal(456)
})
it('should not modify the base pipe', () => {
pipe.when(true, () => 456)
expect(pipe.get()).to.be.equal(123)
})
})
describe('condition evaluates to true', () => {
it('should call the input operator with the value of the pipe', () => {
const spy = sinon.spy()
pipe.when(() => true, spy)
expect(spy.callCount).to.be.equal(1)
expect(spy.firstCall.firstArg).to.be.equal(123)
})
it('should return a new Pipe', () => {
const ret = pipe.when(() => true, x => x)
expect(ret).to.be.an.instanceof(Pipe)
expect(ret).to.not.be.equal(pipe)
})
it('should return the operator value in the new pipe', () => {
const ret = pipe.when(() => true, () => 456)
expect(ret.get()).to.be.equal(456)
})
it('should not modify the base pipe', () => {
pipe.when(() => true, () => 456)
expect(pipe.get()).to.be.equal(123)
})
})
describe('condition is false', () => {
it('should not call the input operator with the value of the pipe', () => {
const spy = sinon.spy()
pipe.when(false, spy)
expect(spy.callCount).to.be.equal(0)
})
it('should return the same Pipe', () => {
const ret = pipe.when(false, x => x)
expect(ret).to.be.equal(pipe)
})
})
describe('condition evaluates to false', () => {
it('should not call the input operator with the value of the pipe', () => {
const spy = sinon.spy()
pipe.when(() => false, spy)
expect(spy.callCount).to.be.equal(0)
})
it('should return the same Pipe', () => {
const ret = pipe.when(() => false, x => x)
expect(ret).to.be.equal(pipe)
})
})
})
describe('unless', () => {
const pipe = Pipe.wrap(123)
describe('condition is false', () => {
it('should call the input operator with the value of the pipe', () => {
const spy = sinon.spy()
pipe.unless(false, spy)
expect(spy.callCount).to.be.equal(1)
expect(spy.firstCall.firstArg).to.be.equal(123)
})
it('should return a new Pipe', () => {
const ret = pipe.unless(false, x => x)
expect(ret).to.be.an.instanceof(Pipe)
expect(ret).to.not.be.equal(pipe)
})
it('should return the operator value in the new pipe', () => {
const ret = pipe.unless(false, () => 456)
expect(ret.get()).to.be.equal(456)
})
it('should not modify the base pipe', () => {
pipe.unless(false, () => 456)
expect(pipe.get()).to.be.equal(123)
})
})
describe('condition evaluates to false', () => {
it('should call the input operator with the value of the pipe', () => {
const spy = sinon.spy()
pipe.unless(() => false, spy)
expect(spy.callCount).to.be.equal(1)
expect(spy.firstCall.firstArg).to.be.equal(123)
})
it('should return a new Pipe', () => {
const ret = pipe.unless(() => false, x => x)
expect(ret).to.be.an.instanceof(Pipe)
expect(ret).to.not.be.equal(pipe)
})
it('should return the operator value in the new pipe', () => {
const ret = pipe.unless(() => false, () => 456)
expect(ret.get()).to.be.equal(456)
})
it('should not modify the base pipe', () => {
pipe.unless(() => false, () => 456)
expect(pipe.get()).to.be.equal(123)
})
})
describe('condition is true', () => {
it('should not call the input operator with the value of the pipe', () => {
const spy = sinon.spy()
pipe.unless(true, spy)
expect(spy.callCount).to.be.equal(0)
})
it('should return the same Pipe', () => {
const ret = pipe.unless(true, x => x)
expect(ret).to.be.equal(pipe)
})
})
describe('condition evaluates to true', () => {
it('should not call the input operator with the value of the pipe', () => {
const spy = sinon.spy()
pipe.unless(() => true, spy)
expect(spy.callCount).to.be.equal(0)
})
it('should return the same Pipe', () => {
const ret = pipe.unless(() => true, x => x)
expect(ret).to.be.equal(pipe)
})
})
})
describe('get', () => {
it('should return the value in the pipe', () => {
expect(Pipe.wrap(123).get())
.to.be.equal(123)
const sym = Symbol()
expect(Pipe.wrap(sym).get())
.to.be.eql(sym)
})
})
describe('async', () => {
it('should return an async pipe', () => {
expect(Pipe.wrap(123).async())
.to.be.an.instanceof(AsyncPipe)
})
})
})

View File

@@ -0,0 +1,15 @@
import { expect } from 'chai'
import { isJSONState } from '../../../lib'
describe('util -> support -> Rehydratable -> isJSONState', () => {
it('should return true for valid JSON', () => {
expect(isJSONState('this is a string'))
.to.be.true
expect(isJSONState(1234))
.to.be.true
expect(isJSONState({ name: true }))
.to.be.true
})
})

View File

@@ -0,0 +1,75 @@
import { padLeft, padRight, padCenter, stringToPascal } from '../../../lib'
import { expect } from 'chai'
describe('util -> support -> string -> padLeft', () => {
it('should return the original string greater than the min', () => {
expect(padLeft('a longer string', 5))
.to.be.equal('a longer string')
})
it('should add spaces to the left of the string', () => {
expect(padLeft('string', 10))
.to.be.equal(' string')
})
it('should respect the pad parameter', () => {
expect(padLeft('string', 10, '*'))
.to.be.equal('****string')
})
})
describe('util -> support -> string -> padRight', () => {
it('should return the original string greater than the min', () => {
expect(padRight('a longer string', 5))
.to.be.equal('a longer string')
})
it('should add spaces to the right of the string', () => {
expect(padRight('string', 10))
.to.be.equal('string ')
})
it('should respect the pad parameter', () => {
expect(padRight('string', 10, '*'))
.to.be.equal('string****')
})
})
describe('util -> support -> string -> padCenter', () => {
it('should return the original string greater than the min', () => {
expect(padCenter('a longer string', 5))
.to.be.equal('a longer string')
})
it('should add spaces to both sides of the string', () => {
expect(padCenter('string', 10))
.to.be.equal(' string ')
})
it('should respect the pad parameter', () => {
expect(padCenter('string', 10, '*'))
.to.be.equal('**string**')
})
})
describe('util -> support -> string -> stringToPascal', () => {
it('should split inputs on spaces', () => {
expect(stringToPascal('foo bar baz'))
.to.be.equal('FooBarBaz')
})
it('should split inputs on underscores', () => {
expect(stringToPascal('foo_bar_baz'))
.to.be.equal('FooBarBaz')
})
it('should split inputs on dashes', () => {
expect(stringToPascal('foo-bar-baz'))
.to.be.equal('FooBarBaz')
})
it('should work with all delimiters', () => {
expect(stringToPascal('foo_bar-baz bin'))
.to.be.equal('FooBarBazBin')
})
})

View File

@@ -0,0 +1,54 @@
import { expect } from 'chai'
import * as sinon from 'sinon'
import { withTimeout } from '../../../lib'
const timeoutPromise = (timeout: number) => new Promise(res => {
setTimeout(res, timeout)
})
describe('util -> support -> timeout -> withTimeout', () => {
it('should call the on-time subscriber when promise resolves', async () => {
const onTimeSpy = sinon.spy()
const lateSpy = sinon.spy()
const timeoutSpy = sinon.spy()
await withTimeout(100, timeoutPromise(50))
.onTime(onTimeSpy)
.late(lateSpy)
.timeout(timeoutSpy)
.run()
await timeoutPromise(100)
expect(onTimeSpy.callCount).to.equal(1)
expect(timeoutSpy.callCount).to.equal(0)
expect(lateSpy.callCount).to.equal(0)
})
it('should call the late and timeout subscribers when promise resolves late', async () => {
const onTimeSpy = sinon.spy()
const lateSpy = sinon.spy()
const timeoutSpy = sinon.spy()
await withTimeout(50, timeoutPromise(100))
.onTime(onTimeSpy)
.late(lateSpy)
.timeout(timeoutSpy)
.run()
await timeoutPromise(100)
expect(onTimeSpy.callCount).to.equal(0)
expect(timeoutSpy.callCount).to.equal(1)
expect(lateSpy.callCount).to.equal(1)
})
it('should resolve the run() promise when the promise completes', async () => {
const runSpy = sinon.spy()
await withTimeout(50, timeoutPromise(10))
.run()
.then(runSpy)
await timeoutPromise(100)
expect(runSpy.callCount).to.be.equal(1)
})
})

View File

@@ -0,0 +1,31 @@
import { expect } from 'chai'
import { isKeyof } from '../../../lib'
describe('util -> support -> types -> isKeyof', () => {
it('should return false for invalid key types', () => {
expect(isKeyof(true, {})).to.be.false
expect(isKeyof(1.5, {})).to.be.false
})
it('should return true if the key is in the object', () => {
const sym = Symbol('test')
const obj = {
foo: true,
[sym]: 123,
}
expect(isKeyof('foo', obj)).to.be.true
expect(isKeyof(sym, obj)).to.be.true
})
it('should return false if the key is not in the object', () => {
const sym = Symbol('test')
const obj = {
foo: true,
[Symbol('test2')]: 123,
}
expect(isKeyof('bar', obj)).to.be.false
expect(isKeyof(sym, obj)).to.be.false
})
})