From c4c508fdcc1ca186832dc4ae5ed5deb1a5953956 Mon Sep 17 00:00:00 2001 From: mum-never-proud Date: Fri, 3 Apr 2020 13:37:13 +0530 Subject: [PATCH] add support for target --- addon/helpers/pipe.js | 8 +-- tests/dummy/app/services/calculate.js | 3 + tests/integration/helpers/pipe-action-test.js | 61 ++++++++++++++++ tests/integration/helpers/pipe-test.js | 69 +++++++++++++++++++ 4 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 tests/dummy/app/services/calculate.js diff --git a/addon/helpers/pipe.js b/addon/helpers/pipe.js index 3db0d5d3..f00e6447 100644 --- a/addon/helpers/pipe.js +++ b/addon/helpers/pipe.js @@ -6,17 +6,17 @@ export function invokeFunction(acc, curr) { return acc.then(curr); } - return curr(acc); + return curr.call(this, acc); } -export function pipe(actions = []) { +export function pipe(actions = [], { target } = {}) { return function(...args) { return actions.reduce((acc, curr, idx) => { if (idx === 0) { - return curr(...args); + return curr.call(target || this, ...args); } - return invokeFunction(acc, curr); + return invokeFunction.call(target || this, acc, curr); }, undefined); }; } diff --git a/tests/dummy/app/services/calculate.js b/tests/dummy/app/services/calculate.js new file mode 100644 index 00000000..21dc0a2d --- /dev/null +++ b/tests/dummy/app/services/calculate.js @@ -0,0 +1,3 @@ +import Service from '@ember/service'; + +export default Service.extend({}); diff --git a/tests/integration/helpers/pipe-action-test.js b/tests/integration/helpers/pipe-action-test.js index dd7aeff1..0c09e463 100644 --- a/tests/integration/helpers/pipe-action-test.js +++ b/tests/integration/helpers/pipe-action-test.js @@ -29,4 +29,65 @@ module('Integration | Helper | {{pipe-action}}', function(hooks) { await click('button'); assert.equal(value, '6', 'should render 6'); }); + + test('it resolves in the given context', async function(assert) { + const Component = { + calculateService: this.owner.lookup('service:calculate'), + setValue(x) { this.set('value', x); }, + actions: { + add(x, y) { return x + y; }, + square(x) { return x * x; }, + squareRoot(x) { return Math.sqrt(x); } + } + }; + + Object.assign(this, Component); + + await render(hbs` + {{perform-calculation + calculate=(pipe-action + (action "add") + (action "square") + (action "squareRoot") + setValue + target=calculateService + ) + }} + `); + + assert.equal(this.calculateService.get('value'), undefined, 'precond - should render 0'); + await click('button'); + assert.equal(this.calculateService.get('value'), '6', 'should render 6'); + }); + + test('it handles mixed contexts', async function(assert) { + const Component = { + calculateService: this.owner.lookup('service:calculate'), + setValue(x) { this.set('value', x); }, + actions: { + square(x) { return x * x; }, + squareRoot(x) { return Math.sqrt(x); } + } + }; + + Object.assign(this, Component); + + this.calculateService.actions = { add(x, y) { return x + y; } } + + await render(hbs` + {{perform-calculation + calculate=(pipe-action + (action "add" target=calculateService) + (action "square") + (action "squareRoot") + setValue + target=calculateService + ) + }} + `); + + assert.equal(this.calculateService.get('value'), undefined, 'precond - should render 0'); + await click('button'); + assert.equal(this.calculateService.get('value'), '6', 'should render 6'); + }); }); diff --git a/tests/integration/helpers/pipe-test.js b/tests/integration/helpers/pipe-test.js index 95338b19..7cea6a27 100644 --- a/tests/integration/helpers/pipe-test.js +++ b/tests/integration/helpers/pipe-test.js @@ -47,4 +47,73 @@ module('Integration | Helper | {{pipe}}', function(hooks) { run(async () => await click('button')); assert.equal(find('p').textContent.trim(), '6', 'should render 6'); }); + + test('it resolves the action in the given context', async function(assert) { + const Component = { + calculateService: this.owner.lookup('service:calculate'), + value: 0, + add(x, y) { return x + y; }, + square(x) { return x * x; }, + squareRoot(x) { this.set('value', Math.sqrt(x)); } + }; + + Object.assign(this, Component); + + await render(hbs` +

{{value}}

+ + `); + + assert.equal(find('p').textContent.trim(), '0', 'precond - should render 0'); + await click('button'); + assert.equal(this.calculateService.get('value'), '6', 'value in calculateService should equal 6'); + }); + + test('it resolves the action in the current context', async function(assert) { + const Component = { + value: 0, + add(x, y) { return x + y; }, + square(x) { return x * x; }, + squareRoot(x) { this.set('value', Math.sqrt(x)); } + }; + + Object.assign(this, Component); + + await render(hbs` +

{{value}}

+ + `); + + assert.equal(find('p').textContent.trim(), '0', 'precond - should render 0'); + await click('button'); + assert.equal(this.get('value'), '6', 'value in current component should equal 6'); + }); + + test('it handles mixed contexts', async function(assert) { + const Component = { + calculateService: this.owner.lookup('service:calculate'), + value: 0, + square(x) { return x * x; }, + squareRoot(x) { this.set('value', Math.sqrt(x)); } + }; + + Object.assign(this, Component); + + this.calculateService.actions = { add(x, y) { return x + y; } } + + await render(hbs` +

{{value}}

+ + `); + + assert.equal(find('p').textContent.trim(), '0', 'precond - should render 0'); + await click('button'); + assert.equal(this.get('value'), '6', 'value in current component should equal 6'); + }); });