Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix teardown all to support teardowns that trigger other teardowns. #194

Merged
merged 3 commits into from
Oct 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ define(

componentInfo && Object.keys(componentInfo.instances).forEach(function(k) {
var info = componentInfo.instances[k];
info.instance.teardown();
// It's possible that a previous teardown caused another component to teardown,
// so we can't assume that the instances object is as it was.
if (info && info.instance) {
info.instance.teardown();
}
});
}

Expand Down
29 changes: 29 additions & 0 deletions test/spec/constructor_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,34 @@ define(['lib/component'], function (defineComponent) {
}).toThrow('utils.push attempted to overwrite "core" while running in protected mode');
});

describe('teardownAll', function () {

it('should teardown all instances', function () {
var TestComponent = defineComponent(testComponent);
var instance1 = (new TestComponent).initialize(document.body)
var instance2 = (new TestComponent).initialize(document.body);
spyOn(instance1, 'teardown').andCallThrough();
spyOn(instance2, 'teardown').andCallThrough();
TestComponent.teardownAll();
expect(instance1.teardown).toHaveBeenCalled();
expect(instance2.teardown).toHaveBeenCalled();
});

it('should support teardowns that cause other teardowns', function () {
var TestComponent = defineComponent(testComponent);
var instance1 = (new TestComponent).initialize(document.body)
var instance2 = (new TestComponent).initialize(document.body);
var original = instance1.teardown;
instance1.teardown = function () {
instance2.teardown();
original.call(this);
}.bind(instance1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this missing assertions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah. Man. I was asleep.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to note – this actually didn't really need assertions because the test was just that it didn't throw – but better to make it explicit.

expect(function () {
TestComponent.teardownAll();
}).not.toThrow();
});

});

});
});