diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-11-20 06:45:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-11-20 06:45:41 +0000 |
commit | 9d45e42dc0298ea8241132142d3100358fe99dc4 (patch) | |
tree | 8435105a23089a82f8298490dff6e3e4218930b4 /tests | |
parent | Initial commit. (diff) | |
download | decko-upstream.tar.xz decko-upstream.zip |
Adding upstream version 1.2.0.upstream/1.2.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bind.js | 37 | ||||
-rw-r--r-- | tests/debounce.js | 71 | ||||
-rw-r--r-- | tests/index.ts | 17 | ||||
-rw-r--r-- | tests/memoize.js | 72 |
4 files changed, 197 insertions, 0 deletions
diff --git a/tests/bind.js b/tests/bind.js new file mode 100644 index 0000000..c76a0f4 --- /dev/null +++ b/tests/bind.js @@ -0,0 +1,37 @@ +import { bind } from '..'; +import { expect } from 'chai'; + +/*global describe,it*/ + +describe('bind()', () => { + it('should bind when used as a simple decorator', next => { + let c = { + @bind + foo() { + return this; + } + }; + + expect(c.foo()).to.equal(c); + + let p = c.foo; + expect(p()).to.equal(c); + + let a = {}; + expect(c.foo.call(a)).to.equal(c); + + next(); + }); + + it('should bind when used as a function', next => { + let ctx = {}, + c = bind(function(){ return this; }, ctx); + + expect(c()).to.equal(ctx); + + let a = {}; + expect(c.call(a)).to.equal(ctx); + + next(); + }); +}); diff --git a/tests/debounce.js b/tests/debounce.js new file mode 100644 index 0000000..1c8bcf0 --- /dev/null +++ b/tests/debounce.js @@ -0,0 +1,71 @@ +import { debounce } from '..'; +import { expect } from 'chai'; + +/*global describe,it*/ + +describe('debounce()', () => { + it('should debounce when used as a simple decorator', next => { + let c = { + calls: 0, + args: null, + + @debounce + foo(...args) { + c.calls++; + c.args = args; + c.context = this; + } + }; + + expect(c).to.have.property('calls', 0); + c.foo(1); + expect(c).to.have.property('calls', 0); + c.foo(2); + c.foo(3); + setTimeout( () => { + expect(c).to.have.property('calls', 1); + expect(c.args).to.deep.equal([3]); + expect(c.context).to.equal(c); + + next(); + }, 20); + }); + + it('should debounce when used as a function', next => { + let c = debounce( (...args) => { + m.calls++; + m.args = args; + }), + m = { calls:0, args:null }; + + expect(m).to.have.property('calls', 0); + c(1); + expect(m).to.have.property('calls', 0); + c(2); + c(3); + setTimeout( () => { + expect(m).to.have.property('calls', 1); + expect(m.args).to.deep.equal([3]); + + next(); + }, 20); + }); + + it('should support passing a delay', next => { + let c = debounce(5, (...args) => { + m.calls.push(args); + }), + m = { calls:[] }; + + c(1); + setTimeout(()=> c(2), 1); + setTimeout(()=> c(3), 10); + setTimeout(()=> c(4), 14); + setTimeout(()=> c(5), 22); + expect(m.calls).to.have.length(0); + setTimeout( () => { + expect(m.calls).to.deep.equal([ [2], [4], [5] ]); + next(); + }, 30); + }); +}); diff --git a/tests/index.ts b/tests/index.ts new file mode 100644 index 0000000..14c4efe --- /dev/null +++ b/tests/index.ts @@ -0,0 +1,17 @@ +import { bind, debounce, memoize } from '..'; +class C { + @bind + foo() { } + + @debounce + moo() { } + + @debounce(1000) + mooWithCustomDelay() { } + + @memoize + mem() { } + + @memoize(true) + memWithConfig() { } +}
\ No newline at end of file diff --git a/tests/memoize.js b/tests/memoize.js new file mode 100644 index 0000000..98f3678 --- /dev/null +++ b/tests/memoize.js @@ -0,0 +1,72 @@ +import { memoize } from '..'; +import { expect } from 'chai'; + +/*global describe,it*/ + +describe('memoize()', () => { + it('should memoize when used as a simple decorator', next => { + let c = { + @memoize + foo(key) { + c[key] = (c[key] || 0) + 1; + } + }; + + expect(c).not.to.have.property('a'); + c.foo('a'); + expect(c).to.have.property('a', 1); + c.foo('a'); + c.foo('a'); + expect(c).to.have.property('a', 1); + + next(); + }); + + it('should memoize when used as a function', next => { + let c = memoize( key => { + m[key] = (m[key] || 0) + 1; + }), + m = {}; + + expect(m).not.to.have.property('a'); + c('a'); + expect(m).to.have.property('a', 1); + c('a'); + c('a'); + expect(m).to.have.property('a', 1); + + next(); + }); + + it('should memoize when called without arguments', next => { + let c = memoize( key => { + m[key] = (m[key] || 0) + 1; + }), + m = {}; + + expect(m).not.to.have.property('undefined'); + c(); + expect(m).to.have.property('undefined', 1); + c(); + c(); + expect(m).to.have.property('undefined', 1); + + next(); + }); + + it('should memoize when called with an empty string', next => { + let c = memoize( key => { + m[key] = (m[key] || 0) + 1; + }), + m = {}; + + expect(m).not.to.have.property(''); + c(''); + expect(m).to.have.property('', 1); + c(''); + c(''); + expect(m).to.have.property('', 1); + + next(); + }); +}); |