Skip to content

Commit

Permalink
add writable macro and reads alias
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden committed Dec 18, 2016
1 parent 77d9e5f commit 7c19e1c
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
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);
});

0 comments on commit 7c19e1c

Please sign in to comment.