From 7f6c2d4bc702c7a80de1e58304107e6b2c38575b Mon Sep 17 00:00:00 2001 From: Rich Hodgkins Date: Tue, 16 Oct 2018 11:07:10 +0100 Subject: [PATCH 1/3] Failing tests for using .toJSON() --- test/spec/lib/database/store.js | 36 ++++++++++++++++++++++++++++++++- test/spec/lib/util.js | 8 +++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/test/spec/lib/database/store.js b/test/spec/lib/database/store.js index 37aab3b..31c1ecc 100644 --- a/test/spec/lib/database/store.js +++ b/test/spec/lib/database/store.js @@ -62,7 +62,41 @@ describe('store', function() { }); }); - [new Date(), /foo/].forEach(function(v) { + it('should let .toJSON() be used as value', function() { + class Something { + constructor() { + Object.defineProperty(this, 'key', {value: 'definedValue', enumberable: false}); + Object.defineProperty(this, 'key2', {value: 'definedValue'}); + } + toJSON() { + return {key: 'val'}; + } + } + + expect(store.create(new Something()).$value()).to.eql({key: 'val'}); + expect(store.create({v: new Something()}).$value()).to.eql({v: {key: 'val'}}); + }); + + it('should not let classes without .toJSON() be used as value', function() { + class Something { + constructor() { + Object.defineProperty(this, 'key', {value: 'definedValue', enumberable: false}); + Object.defineProperty(this, 'key2', {value: 'definedValue'}); + } + } + + expect(() => store.create(new Something())).to.throw(); + expect(() => store.create({v: new Something()})).to.throw(); + }); + + [new Date()].forEach(function(v) { + it(`should let ${v.constructor.name} be used as value`, function() { + expect(store.create(v).$value()).to.eql(v.toJSON()); + expect(store.create({v}).$value()).to.eql({v: v.toJSON()}); + }); + }); + + [/foo/].forEach(function(v) { it(`should not let ${v.constructor.name} be used as value`, function() { expect(() => store.create(v)).to.throw(); expect(() => store.create({v})).to.throw(); diff --git a/test/spec/lib/util.js b/test/spec/lib/util.js index 7cbe4bb..bdeb5fd 100644 --- a/test/spec/lib/util.js +++ b/test/spec/lib/util.js @@ -8,9 +8,15 @@ describe('util', function() { describe('setFirebaseData', function() { - it('should throw on invalid data', function() { + it('should not throw on invalid data', function() { expect( () => util.setFirebaseData(new Date()) + ).to.not.throw(); + }); + + it('should throw on invalid data', function() { + expect( + () => util.setFirebaseData(/regex/) ).to.throw(); }); From 035abd701cbadb3f6f72c79ee139f81a9740f983 Mon Sep 17 00:00:00 2001 From: Rich Hodgkins Date: Tue, 16 Oct 2018 11:22:50 +0100 Subject: [PATCH 2/3] Use .toJSON when serialising data --- lib/database/store.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/database/store.js b/lib/database/store.js index 8b3d770..e7a472b 100644 --- a/lib/database/store.js +++ b/lib/database/store.js @@ -171,6 +171,10 @@ class DataNode { return new DataNode({'.value': value, '.priority': priority}); } + if (typeof value === 'object' && typeof value.toJSON === 'function') { + return this.from(value.toJSON(), priority, now); + } + if (!isObject(value) && !Array.isArray(value)) { throw new Error(`Invalid data node type: ${value} (${typeof value})`); } From 90ad3f4d6afff04d48494fba1d7ed59bdc94f694 Mon Sep 17 00:00:00 2001 From: Rich Hodgkins Date: Tue, 16 Oct 2018 11:25:55 +0100 Subject: [PATCH 3/3] Added node 8 to test matrix --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 333fabe..ff2736f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,10 @@ language: node_js node_js: - "4" - "6" + - "8" script: - > - if [ "$TRAVIS_EVENT_TYPE" == "cron" ] && node --version | grep v6\. ; then + if [ "$TRAVIS_EVENT_TYPE" == "cron" ] && node --version | grep --invert-match v4\. ; then ./bin/build.sh test:live; else echo "skipping live tests";