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) }) }) })