Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add target support for pipe #357

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions addon/helpers/pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
Expand Down
3 changes: 3 additions & 0 deletions tests/dummy/app/services/calculate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Service from '@ember/service';

export default Service.extend({});
61 changes: 61 additions & 0 deletions tests/integration/helpers/pipe-action-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
69 changes: 69 additions & 0 deletions tests/integration/helpers/pipe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
<p>{{value}}</p>
<button {{action (pipe add square squareRoot target=calculateService) 2 4}}>
Calculate
</button>
`);

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`
<p>{{value}}</p>
<button {{action (pipe add square squareRoot) 2 4}}>
Calculate
</button>
`);

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`
<p>{{value}}</p>
<button {{action (pipe (action "add" target=calculateService) square squareRoot) 2 4}}>
Calculate
</button>
`);

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');
});
});