summaryrefslogtreecommitdiffstats
path: root/tests/memoize.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-11-20 06:45:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-11-20 06:45:41 +0000
commit9d45e42dc0298ea8241132142d3100358fe99dc4 (patch)
tree8435105a23089a82f8298490dff6e3e4218930b4 /tests/memoize.js
parentInitial commit. (diff)
downloaddecko-9d45e42dc0298ea8241132142d3100358fe99dc4.tar.xz
decko-9d45e42dc0298ea8241132142d3100358fe99dc4.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/memoize.js')
-rw-r--r--tests/memoize.js72
1 files changed, 72 insertions, 0 deletions
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();
+ });
+});