-
Notifications
You must be signed in to change notification settings - Fork 786
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Because extend does a shallow copy of object properties, some options in interactable objects were aliased to objects in the default objects. In some cases, setting options on one interactable would bleed over to interactables made subsequently. This commit adds a clone utility which is similar to extend, but makes new objects instead of aliasing them. This allow the newly added tests to pass.
- Loading branch information
Showing
4 changed files
with
105 additions
and
5 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,13 @@ | ||
const is = require('./is'); | ||
|
||
module.exports = function clone (source) { | ||
const dest = {}; | ||
for (const prop in source) { | ||
if (is.object(source[prop])) { | ||
dest[prop] = clone(source[prop]); | ||
} else { | ||
dest[prop] = source[prop]; | ||
} | ||
} | ||
return dest; | ||
}; |
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,84 @@ | ||
const test = require('./test'); | ||
const d = require('./domator'); | ||
|
||
const Interactable = require('../src/Interactable'); | ||
const actions = require('../src/actions/base'); | ||
|
||
test('Interactable copies and extends defaults', t => { | ||
actions.methodDict.test = 'testize'; | ||
Interactable.prototype.testize = function (options) { | ||
this.setPerAction('test', options); | ||
}; | ||
|
||
const defaults = require('../src/defaultOptions'); | ||
defaults.test = { | ||
fromDefault: { a: 1, b: 2 }, | ||
specified: { c: 1, d: 2 }, | ||
}; | ||
|
||
const specified = { specified: 'parent' }; | ||
|
||
const div = d('div'); | ||
const interactable = new Interactable(div, { test: specified }); | ||
|
||
t.deepEqual(interactable.options.test.specified, specified.specified, | ||
'specified options are properly set'); | ||
t.deepEqual(interactable.options.test.fromDefault, defaults.test.fromDefault, | ||
'default options are properly set'); | ||
t.notEqual(interactable.options.test.fromDefault, defaults.test.fromDefault, | ||
'defaults are not aliased'); | ||
|
||
defaults.test.fromDefault.c = 3; | ||
t.notOk('c' in interactable.options.test.fromDefault, | ||
'modifying defaults does not affect constructed interactables'); | ||
|
||
// Undo global changes | ||
delete actions.methodDict.test; | ||
delete Interactable.prototype.testize; | ||
delete defaults.test; | ||
|
||
t.end(); | ||
}); | ||
|
||
test('Interactable copies and extends per action defaults', t => { | ||
actions.methodDict.test = 'testize'; | ||
Interactable.prototype.testize = function (options) { | ||
this.setPerAction('test', options); | ||
}; | ||
|
||
const defaults = require('../src/defaultOptions'); | ||
defaults.perAction.testModifier = { | ||
fromDefault: { a: 1, b: 2 }, | ||
specified: null, | ||
}; | ||
defaults.test = { testModifier: defaults.perAction.testModifier }; | ||
|
||
const div = d('div'); | ||
const interactable = new Interactable(div, {}); | ||
interactable.testize({ testModifier: { specified: 'parent' } }); | ||
|
||
t.deepEqual(interactable.options.test, { testModifier: { | ||
fromDefault: { a: 1, b: 2}, | ||
specified: 'parent', | ||
}}, 'specified options are properly set'); | ||
t.deepEqual( | ||
interactable.options.test.testModifier.fromDefault, | ||
defaults.perAction.testModifier.fromDefault, | ||
'default options are properly set'); | ||
t.notEqual( | ||
interactable.options.test.testModifier.fromDefault, | ||
defaults.perAction.testModifier.fromDefault, | ||
'defaults are not aliased'); | ||
|
||
defaults.perAction.testModifier.fromDefault.c = 3; | ||
t.notOk('c' in interactable.options.test.testModifier.fromDefault, | ||
'modifying defaults does not affect constructed interactables'); | ||
|
||
// Undo global changes | ||
delete actions.methodDict.test; | ||
delete Interactable.prototype.test; | ||
delete defaults.test; | ||
delete defaults.perAction.testModifier; | ||
|
||
t.end(); | ||
}); |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require('./Interactable'); | ||
require('./Interaction'); | ||
|
||
// Legacy browser support | ||
|