From 4e27b7ba3a3221f932a8a2cbbd671d72d66abcde Mon Sep 17 00:00:00 2001 From: Nico264 Date: Tue, 26 Jun 2018 18:25:45 +0200 Subject: [PATCH 1/2] require and dependsOn in Relationships Corrects a bug regarding the dependsOn/require options interaction in Relationship Fields (follows the enhancement of https://github.com/keystonejs/keystone/pull/2200) --- fields/types/relationship/RelationshipType.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fields/types/relationship/RelationshipType.js b/fields/types/relationship/RelationshipType.js index cc18e94b4c..ebf9b70777 100644 --- a/fields/types/relationship/RelationshipType.js +++ b/fields/types/relationship/RelationshipType.js @@ -72,9 +72,9 @@ relationship.prototype.addToSchema = function (schema) { var def = { type: this._nativeType, ref: this.options.ref, - index: (this.options.index ? true : false), - required: (this.options.required ? true : false), - unique: (this.options.unique ? true : false), + index: (this.options.index ? this.options.index : false), + required: (this.options.required ? this.options.required : false), + unique: (this.options.unique ? this.options.unique : false), }; this.paths = { refList: this.options.refListPath || this.path + 'RefList', From ac13e259dd0ceda669b6aa319d30d77eea0de4e6 Mon Sep 17 00:00:00 2001 From: Nicolas Guichard Date: Tue, 26 Jun 2018 19:44:56 +0200 Subject: [PATCH 2/2] test for dependsOn and require and Relationship --- test/models/DependsOn.js | 1 + test/unit/lib/list/dependsOn.js | 57 +++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/test/models/DependsOn.js b/test/models/DependsOn.js index f6d683dbab..976d07c6cb 100755 --- a/test/models/DependsOn.js +++ b/test/models/DependsOn.js @@ -11,6 +11,7 @@ DependsOn.add({ title: { type: String, required: true, default: '' }, state: { type: Types.Select, options: 'draft, published, archived', default: 'draft' }, publishedDate: { type: Types.Date, dependsOn: {state: 'published'}, required: true, initial: false }, + relativeDependsOn: { type: Types.Relationship, ref: 'DependsOn', dependsOn: {state: 'published'}, required: true, initial: false }, }); DependsOn.register(); diff --git a/test/unit/lib/list/dependsOn.js b/test/unit/lib/list/dependsOn.js index 7d7ab13240..5a672e4031 100644 --- a/test/unit/lib/list/dependsOn.js +++ b/test/unit/lib/list/dependsOn.js @@ -10,26 +10,24 @@ var DependsOn = keystone.list('DependsOn'); describe('Test dependsOn and required', function () { it('Ignore required if evalDependsOn is not `true` by setting `state` to `draft`', function (done) { - // remove any Post documents + // remove any DependsOn documents DependsOn.model.find({}).remove(function (error) { if (error) { done(error); } - var newPost = new DependsOn.model({ + var newDependsOn = new DependsOn.model({ title: 'new post', state: 'draft' }); - newPost.save(done); + newDependsOn.save(done); }); }); - - it('Save will fail if `state` set to `published` and `publishedDate` is not defined', function (done) { - // remove any Post documents + // remove any DependsOn documents DependsOn.model.find({}).remove(function (error) { if (error) { done(error); @@ -39,13 +37,43 @@ describe('Test dependsOn and required', function () { const backupLog = console.error; console.error = () => null; - var newPost = new DependsOn.model({ + var newDependsOn = new DependsOn.model({ title: 'new post', state: 'published', publishedDate: undefined, }); + + newDependsOn.relativeDependsOn = newDependsOn._id; + + newDependsOn.save(function (err) { + demand(err).be.a.object(); + + console.error = backupLog; + done(); + }); + }); + + }); + + it('Save will fail if `state` set to `published` and `relativeDependsOn` is not defined', function (done) { + // remove any DependsOn documents + DependsOn.model.find({}).remove(function (error) { + if (error) { + done(error); + } + + // suppressing console log output + const backupLog = console.error; + console.error = () => null; + + var newDependsOn = new DependsOn.model({ + title: 'new post', + state: 'published', + publishedDate: new Date(), + relativeDependsOn: undefined, + }); - newPost.save(function (err) { + newDependsOn.save(function (err) { demand(err).be.a.object(); console.error = backupLog; @@ -55,20 +83,23 @@ describe('Test dependsOn and required', function () { }); - it('Save will succeed if `state` set to `published` and `publishedDate` is defined', function (done) { + it('Save will succeed if `state` set to `published` and `publishedDate` and `relativeDependsOn` are defined', function (done) { - // remove any Post documents + // remove any DependsOn documents DependsOn.model.find({}).remove(function (error) { if (error) { done(error); } - var newPost = new DependsOn.model({ + var newDependsOn = new DependsOn.model({ title: 'new post', state: 'published', - publishedDate: new Date() + publishedDate: new Date(), }); - newPost.save(done); + + newDependsOn.relativeDependsOn = newDependsOn._id; + + newDependsOn.save(done); }); });