Skip to content

Commit

Permalink
check if props exist before running connection.id (#516)
Browse files Browse the repository at this point in the history
* Do not call connection.id if props does not exist
* Add test for no props passed to new DefineMap
* Use can-reflect to ensure map-like props during constructor hydrate
Co-authored-by: Bradley Momberger <[email protected]>
  • Loading branch information
morganheimbeck authored Jul 6, 2022
1 parent 30ef907 commit 816332e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
20 changes: 20 additions & 0 deletions can/constructor-hydrate/constructor-hydrate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,23 @@ QUnit.test("Two objects with no id", function(assert) {
new Hub({name: 'One'});
assert.ok(true, 'Should allow to create two instances without an id (no Max Call Stack error)');
});

QUnit.test("No Props passed to new DefineMap, doesn't blow up", function(assert) {
var Hub = DefineMap.extend({});
Hub.List = DefineList.extend({
'#': { Type: Hub }
});
var HubConnection = connect([
constructorBehavior,
constructorStore,
mapBehavior,
hydrateBehavior,
], { Map: Hub, List: Hub.List });

var hub1 = new Hub();
hub1.name = 'One';
HubConnection.addInstanceReference(hub1);
assert.ok(!HubConnection.instanceStore.has(undefined), 'The instanceStore should not have an "undefined" key item');
new Hub({name: 'One'});
assert.ok(true, 'Should allow to create two instances without an id (no Max Call Stack error)');
});
8 changes: 6 additions & 2 deletions can/constructor-hydrate/constructor-hydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,19 @@

var connect = require("../../can-connect");
var Construct = require("can-construct");
var canReflect = require("can-reflect");

var constructorHydrateBehavior = connect.behavior("can-connect/can/construct-hydrate", function(baseConnect){
return {
init: function(){
var oldSetup = this.Map.prototype.setup;
var connection = this;
this.Map.prototype.setup = function(props){
if (connection.instanceStore.has( connection.id(props) )) {
return new Construct.ReturnValue( connection.hydrateInstance(props) );
if (
canReflect.isMapLike(props) &&
connection.instanceStore.has(connection.id(props))
) {
return new Construct.ReturnValue(connection.hydrateInstance(props));
}
return oldSetup.apply(this, arguments);
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"steal-qunit": "^2.0.0",
"steal-tools": "^1.0.0",
"test-saucelabs": "^0.0.6",
"testee": "^0.9.0"
"testee": "^0.10.2"
},
"steal": {
"plugins": [
Expand Down
2 changes: 1 addition & 1 deletion test/test-saucelabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ SauceLabs({
url: "http://localhost:3000/test/test.html?hidepassed",
platforms: platforms
}],
runInSeries: true,
runInParallel: false,
zeroAssertionsPass: false
});

0 comments on commit 816332e

Please sign in to comment.