-
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.
* Handle class field * Add class fields docs * Update docs for consistency with obeservable-object * Use steal-conditional * remove additional line break
- Loading branch information
1 parent
895b47c
commit 138cbe9
Showing
6 changed files
with
141 additions
and
1 deletion.
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,12 @@ | ||
let supportsClassFields; | ||
|
||
try { | ||
eval(`class Foo { | ||
field = "value" | ||
}`); | ||
supportsClassFields = true; | ||
} catch(e) { | ||
supportsClassFields = false; | ||
} | ||
|
||
module.exports = supportsClassFields; |
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
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,77 @@ | ||
const ObservableArray = require("../src/can-observable-array"); | ||
const QUnit = require("steal-qunit"); | ||
|
||
QUnit.module('can-observable-array-class-fields'); | ||
|
||
QUnit.test("Class properties default value", function(assert) { | ||
const done = assert.async(); | ||
|
||
class MyList extends ObservableArray { | ||
/* jshint ignore:start */ | ||
prop = ['foo', 'bar']; | ||
/* jshint ignore:end */ | ||
} | ||
|
||
const aList = new MyList(); | ||
assert.deepEqual(aList.prop, ['foo', 'bar'], 'Default value works'); | ||
|
||
aList.on('prop', function(ev, newVal, oldVal) { | ||
assert.deepEqual(oldVal, ['foo', 'bar'], 'Old value is correct'); | ||
assert.deepEqual(newVal, ['baz'], 'Value is updated'); | ||
assert.ok(ev, 'Age is observable'); | ||
done(); | ||
}); | ||
|
||
aList.prop = ['baz']; | ||
}); | ||
|
||
|
||
QUnit.test('Throws on class field property named items', function (assert) { | ||
class MyList extends ObservableArray { | ||
/* jshint ignore:start */ | ||
items = ['foo', 'bar']; | ||
/* jshint ignore:end */ | ||
|
||
static get items() { | ||
return String; | ||
} | ||
} | ||
|
||
try { | ||
new MyList(); | ||
} catch (error) { | ||
assert.ok(error, 'it throws'); | ||
assert.equal( | ||
error.message, | ||
'ObservableArray does not support a class field named items. Try using a different name or using static items', | ||
'Message is correct' | ||
); | ||
} | ||
|
||
}); | ||
|
||
QUnit.test('handle descriptor getter', function(assert) { | ||
assert.expect(4); | ||
|
||
const foo = new ObservableArray(); | ||
|
||
let _bar = "Hello"; | ||
Object.defineProperty(foo, "bar", { | ||
get() { | ||
return _bar; | ||
}, | ||
set(val) { | ||
_bar = val; | ||
} | ||
}); | ||
|
||
assert.equal(foo.bar, 'Hello'); | ||
|
||
foo.on('bar', function (ev, newVal, oldVal) { | ||
assert.equal(oldVal, 'Hello', 'Old value is correct'); | ||
assert.equal(newVal, 'Hola', 'Value is updated'); | ||
assert.ok(ev, 'The class field is observable'); | ||
}); | ||
|
||
foo.bar = 'Hola'; | ||
}); |