From fd0ad4314a64a5d41b6d97fa898105bdf7eda605 Mon Sep 17 00:00:00 2001 From: NAKAMURA Masayuki Date: Fri, 7 Jul 2017 21:32:28 +0900 Subject: [PATCH 1/2] Add ctx.options as second argument of callback --- lib/computed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/computed.js b/lib/computed.js index 4fd4a36..b671dc2 100644 --- a/lib/computed.js +++ b/lib/computed.js @@ -38,7 +38,7 @@ module.exports = (Model, options) => { // `Promise.resolve` will normalize promises and raw values return Promise - .resolve(Model[callback](ctx.data)) + .resolve(Model[callback](ctx.data, ctx.options)) .then(value => (ctx.data[property] = value)) }) }) From 338f9ecd517f43d8fcdebd8a550dd90f5942d8db Mon Sep 17 00:00:00 2001 From: NAKAMURA Masayuki Date: Fri, 7 Jul 2017 23:18:56 +0900 Subject: [PATCH 2/2] Add test for options --- test/test.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/test/test.js b/test/test.js index 096e893..28ad618 100644 --- a/test/test.js +++ b/test/test.js @@ -29,15 +29,23 @@ const Item = loopback.PersistedModel.extend('item', { readonly: 'computedReadonly', requestedAt: 'computedRequestedAt', promised: 'computedPromised', + accessToken: 'computedAccessToken', }, }, }, }) +const testOptions = { + accessToken: { + id: 'ACCESS_TOKEN', + }, +} + // Define computed property callbacks. Item.computedReadonly = item => Boolean(item.status === 'archived') Item.computedRequestedAt = () => now Item.computedPromised = item => Promise.resolve(`${item.name}: As promised I get back to you!`) +Item.computedAccessToken = (item, options) => options.accessToken // Attach model to db. Item.attachTo(dbConnector) @@ -63,10 +71,13 @@ describe('loopback computed property', function() { }) before(function() { - return Promise.join(Item.findById(this.itemOne.id), Item.findById(this.itemTwo.id), (itemOne, itemTwo) => { - this.itemOne = itemOne - this.itemTwo = itemTwo - }) + return Promise.join( + Item.findById(this.itemOne.id), + Item.findById(this.itemTwo.id, null, testOptions), + (itemOne, itemTwo) => { + this.itemOne = itemOne + this.itemTwo = itemTwo + }) }) it('should set the model property to the value returned by the defined callback', function() { @@ -80,4 +91,10 @@ describe('loopback computed property', function() { expect(this.itemOne.promised).to.equal('Item 1: As promised I get back to you!') expect(this.itemTwo.promised).to.equal('Item 2: As promised I get back to you!') }) + + it('should set the model property to the value returned by the defined callback with options', function() { + /* eslint-disable no-unused-expressions */ + expect(this.itemOne.accessToken).to.be.undefined + expect(this.itemTwo.accessToken.id).to.equal('ACCESS_TOKEN') + }) })