-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kelly Selden
committed
Dec 18, 2016
1 parent
77d9e5f
commit 7c19e1c
Showing
3 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './writable'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |