From 8b957875e6c7dbb6114a35de7c4c7e65bd724e3d Mon Sep 17 00:00:00 2001 From: Italo Izaac Date: Fri, 12 Apr 2019 11:50:51 -0300 Subject: [PATCH] feat(relations): fix coverage decreased and remove unnecessary if statement. --- src/Lucid/Relations/BelongsToMany.js | 4 ---- test/unit/lucid-belongs-to-many.spec.js | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Lucid/Relations/BelongsToMany.js b/src/Lucid/Relations/BelongsToMany.js index c8267454..21dfb43f 100644 --- a/src/Lucid/Relations/BelongsToMany.js +++ b/src/Lucid/Relations/BelongsToMany.js @@ -383,10 +383,6 @@ class BelongsToMany extends BaseRelation { */ let hidden = _.get(pivotModel, '$hidden', []) - if (hidden.length === 0 && _.hasIn(pivotModel, 'constructor.hidden')) { - hidden = pivotModel.constructor.hidden - } - return ( (_.isArray(hidden) && hidden.includes('$pivot') === true) || this.$pivotAttribute === false diff --git a/test/unit/lucid-belongs-to-many.spec.js b/test/unit/lucid-belongs-to-many.spec.js index b7cf0efc..fb50ca6d 100644 --- a/test/unit/lucid-belongs-to-many.spec.js +++ b/test/unit/lucid-belongs-to-many.spec.js @@ -2961,4 +2961,26 @@ test.group('Relations | Belongs To Many', (group) => { const userPosts = user.posts() assert.equal(userPosts.$pivotAttribute, 'pivot') }) + + test('throw exception when pivotModel is defined and calling pivotPrimaryKey', (assert) => { + class Post extends Model { + } + + class PostUser extends Model { + } + + class User extends Model { + posts () { + return this.belongsToMany(Post).pivotModel(PostUser).pivotPrimaryKey('key') + } + } + + User._bootIfNotBooted() + Post._bootIfNotBooted() + PostUser._bootIfNotBooted() + + const user = new User() + const fn = () => user.posts() + assert.throw(fn, 'E_INVALID_RELATION_METHOD: Cannot call pivotPrimaryKey since pivotModel has been defined') + }) })