forked from ariatemplates/hashspace
-
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.
fix(components) $dispose not always called
Let's consider a template like that ``` <template test(data)> <#data.ref /> </template> ``` When changing the component template behinf `data.ref` corresponding `$dispose()` method (if any defined in the controller class) were not executed. Closes ariatemplates#306
- Loading branch information
Benoit Charbonnier
committed
Sep 24, 2014
1 parent
711d77d
commit a4d9770
Showing
2 changed files
with
118 additions
and
13 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,98 @@ | ||
/* | ||
* Copyright 2013 Amadeus s.a.s. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
var klass=require("hsp/klass"), | ||
ht=require("hsp/utils/hashtester"); | ||
|
||
var constructor_counter = 0; | ||
var dispose_counter = 0; | ||
|
||
var LifeCycleController = klass({ | ||
$constructor: function() { | ||
constructor_counter++; | ||
}, | ||
$dispose: function() { | ||
dispose_counter++; | ||
} | ||
}); | ||
|
||
<template lifecycle1 using ctrl:LifeCycleController> | ||
<div class="content">I am component 1</div> | ||
</template> | ||
|
||
<template lifecycle2 using ctrl:LifeCycleController> | ||
<div class="content">I am component 2</div> | ||
</template> | ||
|
||
<template wrapper(data)> | ||
{if data.visible} | ||
<#lifecycle1 /> | ||
{/if} | ||
</template> | ||
|
||
<template dynamicWrapper(data)> | ||
<#data.cpt /> | ||
</template> | ||
|
||
var data = { | ||
visible: true, | ||
cpt: lifecycle1 | ||
}; | ||
|
||
|
||
describe("Component lifecycle", function () { | ||
beforeEach(function() { | ||
constructor_counter = dispose_counter = 0; | ||
}); | ||
it("should invoke $contructor and $dispose anytime a new instance is needed", function() { | ||
var h=ht.newTestContext(); | ||
|
||
wrapper(data).render(h.container); | ||
expect(h(".content").text()).to.equal('I am component 1'); | ||
expect(constructor_counter).to.equal(1); | ||
h.$set(data, "visible", false); | ||
h.$set(data, "visible", true); | ||
expect(constructor_counter).to.equal(2); | ||
|
||
expect(dispose_counter).to.equal(1); | ||
|
||
h.$dispose(); | ||
}); | ||
}); | ||
|
||
describe.only("Component used as dynamic <#ref >", function() { | ||
beforeEach(function() { | ||
constructor_counter = dispose_counter = 0; | ||
}); | ||
|
||
it("should invoke $constructor at instance creation", function() { | ||
var h=ht.newTestContext(); | ||
|
||
dynamicWrapper(data).render(h.container); | ||
expect(h(".content").text()).to.equal('I am component 1'); | ||
expect(constructor_counter).to.equal(1); | ||
|
||
h.$set(data, "cpt", lifecycle2); | ||
expect(h(".content").text()).to.equal('I am component 2'); | ||
expect(constructor_counter).to.equal(2); | ||
|
||
h.$set(data, "cpt", lifecycle1); | ||
|
||
expect(dispose_counter).to.equal(2); | ||
|
||
h.$dispose(); | ||
}); | ||
|
||
}); |