From afb4c79ae357381b160d4807cdf83ef0ccc1af07 Mon Sep 17 00:00:00 2001 From: lilsweetcaligula Date: Wed, 19 May 2021 16:56:37 +0300 Subject: [PATCH] Add the tests --- store-test.js | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/store-test.js b/store-test.js index ee62a91..a9eedb3 100644 --- a/store-test.js +++ b/store-test.js @@ -348,6 +348,56 @@ function basictest(settings) { }) }) + describe('when the id$ arg passed to #save$ is undefined', function () { + it('should generate a new id and save the entity', function (done) { + si.make('foo') + .data$({ p1: 'v1', p2: 'v2' }) + .save$({ id$: undefined }, function (err, foo1) { + if (err) { + return done(err) + } + + expect(typeof foo1.id).to.equal('string') + + foo1.load$( + foo1.id, + verify(done, function (foo2) { + expect(foo2).not.to.be.null() + expect(foo2).not.to.be.undefined() + + expect(foo2.p1).to.equal('v1') + expect(foo2.p2).to.equal('v2') + }) + ) + }) + }) + }) + + describe('when the id$ arg passed to #save$ is null', function () { + it('should generate a new id and save the entity', function (done) { + si.make('foo') + .data$({ p1: 'v1', p2: 'v2' }) + .save$({ id$: null }, function (err, foo1) { + if (err) { + return done(err) + } + + expect(typeof foo1.id).to.equal('string') + + foo1.load$( + foo1.id, + verify(done, function (foo2) { + expect(foo2).not.to.be.null() + expect(foo2).not.to.be.undefined() + + expect(foo2.p1).to.equal('v1') + expect(foo2.p2).to.equal('v2') + }) + ) + }) + }) + }) + it('should update an entity if id provided', function (done) { var foo = si.make('foo') foo.id = 'to-be-updated' @@ -376,6 +426,56 @@ function basictest(settings) { }) }) + describe('when the provided id is null', function () { + it('should generate a new id and save the entity', function (done) { + si.make('foo') + .data$({ id: null, p1: 'v1', p2: 'v2' }) + .save$(function (err, foo1) { + if (err) { + return done(err) + } + + expect(typeof foo1.id).to.equal('string') + + foo1.load$( + foo1.id, + verify(done, function (foo2) { + expect(foo2).not.to.be.null() + expect(foo2).not.to.be.undefined() + + expect(foo2.p1).to.equal('v1') + expect(foo2.p2).to.equal('v2') + }) + ) + }) + }) + }) + + describe('when the provided id is undefined', function () { + it('should generate a new id and save the entity', function (done) { + si.make('foo') + .data$({ id: undefined, p1: 'v1', p2: 'v2' }) + .save$(function (err, foo1) { + if (err) { + return done(err) + } + + expect(typeof foo1.id).to.equal('string') + + foo1.load$( + foo1.id, + verify(done, function (foo2) { + expect(foo2).not.to.be.null() + expect(foo2).not.to.be.undefined() + + expect(foo2.p1).to.equal('v1') + expect(foo2.p2).to.equal('v2') + }) + ) + }) + }) + }) + it("should save an entity if id provided but original doesn't exist", function (done) { var foo = si.make('foo') foo.id = 'will-be-inserted'