-
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
Nov 14, 2018
1 parent
91b8fdb
commit 3500728
Showing
18 changed files
with
223 additions
and
128 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,61 @@ | ||
import Component from '@ember/component'; | ||
import { get, set, setProperties } from '@ember/object'; | ||
|
||
export default function({ | ||
assert, | ||
baseClass = Component, | ||
computed, | ||
properties, | ||
strictEqual, | ||
deepEqual, | ||
assertion, | ||
assertReadOnly | ||
}) { | ||
let MyComponent = baseClass.extend({ | ||
computed | ||
}); | ||
let subject; | ||
try { | ||
subject = MyComponent.create({ | ||
renderer: {} | ||
}); | ||
} catch (err) { | ||
// this is for ember < 2.10 | ||
// can remove once only support 2.12 | ||
subject = MyComponent.create(); | ||
} | ||
|
||
// compute initial value | ||
// to test recomputes | ||
get(subject, 'computed'); | ||
|
||
setProperties(subject, properties); | ||
|
||
let result = get(subject, 'computed'); | ||
|
||
function doAssertion(result) { | ||
if (assertion) { | ||
assert.ok(assertion(result)); | ||
} else if (deepEqual) { | ||
assert.deepEqual(result, deepEqual); | ||
} else if (assertReadOnly) { | ||
let func = () => set(subject, 'computed', 'assert read only'); | ||
assert.throws(func, /Cannot set read-only property/); | ||
} else if (assert) { | ||
assert.strictEqual(result, strictEqual); | ||
} | ||
} | ||
|
||
let promise; | ||
if (result && typeof result === 'object' && typeof result.then === 'function') { | ||
promise = result.then(doAssertion); | ||
} else { | ||
doAssertion(result); | ||
} | ||
|
||
return { | ||
subject, | ||
result, | ||
promise | ||
}; | ||
} |
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,9 @@ | ||
const exclude = [ | ||
'__esModule', | ||
'default' | ||
]; | ||
|
||
// helps prevent forgetting to test a new import | ||
export default function(assert, obj) { | ||
assert.expect(Object.getOwnPropertyNames(obj).filter(p => exclude.indexOf(p) === -1).length); | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,117 @@ | ||
import EmberObject from '@ember/object'; | ||
import { readOnly } from '@ember/object/computed'; | ||
import { resolve } from 'rsvp'; | ||
import { compute } from 'ember-macro-helpers/test-support/compute'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Integration | compute', function() { | ||
module('verify API', function() { | ||
test('it works without properties', function(assert) { | ||
compute({ | ||
assert, | ||
computed: readOnly('key'), | ||
strictEqual: undefined | ||
}); | ||
}); | ||
|
||
test('it accepts a different base class', function(assert) { | ||
compute({ | ||
assert, | ||
baseClass: EmberObject.extend({ | ||
foo: 'bar' | ||
}), | ||
computed: readOnly('foo'), | ||
strictEqual: 'bar' | ||
}); | ||
}); | ||
|
||
test('it uses properties to calculate value', function(assert) { | ||
compute({ | ||
assert, | ||
computed: readOnly('key'), | ||
properties: { | ||
key: 'test value' | ||
}, | ||
strictEqual: 'test value' | ||
}); | ||
}); | ||
|
||
test('it will deep equal', function(assert) { | ||
compute({ | ||
assert, | ||
computed: readOnly('key'), | ||
properties: { | ||
key: ['test value'] | ||
}, | ||
deepEqual: ['test value'] | ||
}); | ||
}); | ||
|
||
test('it will allow you to calculate the assertion', function(assert) { | ||
compute({ | ||
assert, | ||
computed: readOnly('key'), | ||
properties: { | ||
key: ['test value'] | ||
}, | ||
assertion([value]) { | ||
return value === 'test value'; | ||
} | ||
}); | ||
}); | ||
|
||
test('it can assert readOnly', function(assert) { | ||
compute({ | ||
assert, | ||
computed: readOnly('key'), | ||
assertReadOnly: true | ||
}); | ||
}); | ||
|
||
test('it is promise-aware', function(assert) { | ||
return compute({ | ||
assert, | ||
computed: readOnly('key'), | ||
properties: { | ||
key: resolve('test value') | ||
}, | ||
strictEqual: 'test value' | ||
}).promise; | ||
}); | ||
|
||
test('it returns result', function(assert) { | ||
let { result } = compute({ | ||
computed: readOnly('key'), | ||
properties: { | ||
key: 'test value' | ||
} | ||
}); | ||
|
||
assert.strictEqual(result, 'test value'); | ||
}); | ||
|
||
test('it returns subject', function(assert) { | ||
let { subject } = compute({ | ||
computed: readOnly('key'), | ||
properties: { | ||
key: 'test value' | ||
} | ||
}); | ||
|
||
assert.strictEqual(subject.get('computed'), 'test value'); | ||
}); | ||
}); | ||
|
||
module('edge cases', function() { | ||
test('it handles special case null', function(assert) { | ||
compute({ | ||
assert, | ||
computed: readOnly('key'), | ||
properties: { | ||
key: null | ||
}, | ||
strictEqual: null | ||
}); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.