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 writable macro and reads alias #15

Merged
merged 1 commit into from Dec 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions addon/reads.js
@@ -0,0 +1 @@
export { default } from './writable';
19 changes: 19 additions & 0 deletions addon/writable.js
@@ -0,0 +1,19 @@
import computed from './computed';

export default function(getter, setterCallback) {
let newCallback = {
get(val) { return val; }
};

if (setterCallback) {
if (setterCallback.set) {
newCallback.set = setterCallback.set;
} else {
newCallback.set = function() {
return setterCallback.apply(this, arguments);
};
}
}

return computed(getter, newCallback);
}
176 changes: 176 additions & 0 deletions tests/integration/reads-test.js
@@ -0,0 +1,176 @@
import reads from 'ember-macro-helpers/reads';
import raw from 'ember-macro-helpers/raw';
import { module, test } from 'qunit';
import sinon from 'sinon';
import compute from 'ember-macro-test-helpers/compute';

const getReturnValue = 'get return value test';
const setReturnValue = 'set return value test';
const newValue = 'new value test';

let setCallback;

module('Integration | Macro | reads', {
beforeEach() {
setCallback = sinon.stub().returns(setReturnValue);
}
});

test('without setter: passes through the getter', function(assert) {
compute({
assert,
computed: reads('key'),
properties: {
key: getReturnValue
},
strictEqual: getReturnValue
});
});

test('without setter: allows computed keys', function(assert) {
compute({
assert,
computed: reads(raw(getReturnValue)),
strictEqual: getReturnValue
});
});

test('without setter: allows setting', function(assert) {
let { subject } = compute({
computed: reads(raw(getReturnValue))
});

subject.set('computed', newValue);

assert.strictEqual(subject.get('computed'), newValue);
});

test('without setter: is no longer a computed', function(assert) {
let { subject } = compute({
computed: reads('key'),
properties: {
key: false
}
});

subject.set('computed', false);
subject.set('key', true);

assert.strictEqual(subject.get('computed'), false);
});

test('with function setter: passes through the getter', function(assert) {
compute({
assert,
computed: reads('key', setCallback),
properties: {
key: getReturnValue
},
strictEqual: getReturnValue
});
});

test('with function setter: allows computed keys', function(assert) {
compute({
assert,
computed: reads(raw(getReturnValue), setCallback),
strictEqual: getReturnValue
});
});

test('with function setter: setter return value is new value', function(assert) {
let { subject } = compute({
computed: reads(raw(getReturnValue), setCallback)
});

subject.set('computed', newValue);

assert.strictEqual(subject.get('computed'), setReturnValue);
});

test('with function setter: `this` is object context', function(assert) {
let { subject } = compute({
computed: reads(raw(getReturnValue), setCallback)
});

subject.set('computed', newValue);

assert.strictEqual(setCallback.thisValues[0], subject);
});

test('with function setter: is still a computed', function(assert) {
let { subject } = compute({
computed: reads('key', setCallback),
properties: {
key: false
}
});

subject.set('computed', false);
subject.set('key', true);

assert.strictEqual(subject.get('computed'), true);
});

test('with object setter: passes through the getter', function(assert) {
compute({
assert,
computed: reads('key', {
set: setCallback
}),
properties: {
key: getReturnValue
},
strictEqual: getReturnValue
});
});

test('with object setter: allows computed keys', function(assert) {
compute({
assert,
computed: reads(raw(getReturnValue), {
set: setCallback
}),
strictEqual: getReturnValue
});
});

test('with object setter: setter return value is new value', function(assert) {
let { subject } = compute({
computed: reads(raw(getReturnValue), {
set: setCallback
})
});

subject.set('computed', newValue);

assert.strictEqual(subject.get('computed'), setReturnValue);
});

test('with object setter: `this` is object context', function(assert) {
let { subject } = compute({
computed: reads(raw(getReturnValue), {
set: setCallback
})
});

subject.set('computed', newValue);

assert.strictEqual(setCallback.thisValues[0], subject);
});

test('with object setter: is still a computed', function(assert) {
let { subject } = compute({
computed: reads('key', {
set: setCallback
}),
properties: {
key: false
}
});

subject.set('computed', false);
subject.set('key', true);

assert.strictEqual(subject.get('computed'), true);
});