-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
3 changed files
with
119 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
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,25 @@ | ||
import Ember from 'ember'; | ||
|
||
export default function preference(dependentKey, options = {}) { | ||
var key = `preferences.${dependentKey}`; | ||
|
||
return Ember.computed(key, { | ||
get() { | ||
var value = this.get(key); | ||
|
||
if (typeof value === 'undefined' || value === null) { | ||
if (typeof options.defaultValue === 'function') { | ||
return options.defaultValue(); | ||
} else { | ||
return options.defaultValue; | ||
} | ||
} | ||
|
||
return value; | ||
}, | ||
|
||
set(_, value) { | ||
return this.set(key, value); | ||
} | ||
}); | ||
} |
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,89 @@ | ||
import Ember from 'ember'; | ||
import preference from 'ember-preferences/utils/preference'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Unit | Utility | preference'); | ||
|
||
function createInstance(options = {}) { | ||
let Class = Ember.Object.extend({ | ||
foo: preference('foo', options.cpOptions) | ||
}); | ||
|
||
let preferences = options.preferences || { foo: 'bar' }; | ||
|
||
let instance = Class.create({ preferences }); | ||
|
||
return instance; | ||
} | ||
|
||
test('returns values from "preference" property', function(assert) { | ||
let instance = createInstance(); | ||
|
||
assert.equal(instance.get('foo'), 'bar'); | ||
}); | ||
|
||
test('updates value when the preference changes', function(assert) { | ||
let instance = createInstance(); | ||
|
||
assert.equal(instance.get('foo'), 'bar'); | ||
|
||
instance.set('preferences.foo', 'baz'); | ||
|
||
assert.equal(instance.get('foo'), 'baz'); | ||
}); | ||
|
||
test('returns default value when preference is null', function(assert) { | ||
let instance = createInstance({ | ||
cpOptions: { | ||
defaultValue: 'qux' | ||
}, | ||
|
||
preferences: { foo: null } | ||
}); | ||
|
||
assert.equal(instance.get('foo'), 'qux'); | ||
}); | ||
|
||
test('returns default value when preference is undefined', function(assert) { | ||
let instance = createInstance({ | ||
cpOptions: { | ||
defaultValue: 'qux' | ||
}, | ||
|
||
preferences: { } | ||
}); | ||
|
||
assert.equal(instance.get('foo'), 'qux'); | ||
}); | ||
|
||
test('evaluates default value when it is a function', function(assert) { | ||
let instance = createInstance({ | ||
cpOptions: { | ||
defaultValue() { | ||
return 'qux'; | ||
} | ||
}, | ||
|
||
preferences: { } | ||
}); | ||
|
||
assert.equal(instance.get('foo'), 'qux'); | ||
}); | ||
|
||
test('writes configuration to preference object', function(assert) { | ||
let instance = createInstance(); | ||
|
||
instance.set('foo', 'baz'); | ||
|
||
assert.equal(instance.get('preferences.foo'), 'baz'); | ||
}); | ||
|
||
test('updates value when the property changes', function(assert) { | ||
let instance = createInstance(); | ||
|
||
assert.equal(instance.get('foo'), 'bar'); | ||
|
||
instance.set('foo', 'baz'); | ||
|
||
assert.equal(instance.get('foo'), 'baz'); | ||
}); |