Index: client/tests/client/view/view_tests.dart |
diff --git a/client/tests/client/view/view_tests.dart b/client/tests/client/view/view_tests.dart |
index 80c6c332a941d57308a134ef6348b6a397b96ab6..f8519429c41601cd0ae1fa974fe3028a2b7a5408 100644 |
--- a/client/tests/client/view/view_tests.dart |
+++ b/client/tests/client/view/view_tests.dart |
@@ -9,8 +9,195 @@ |
#import('../../../testing/unittest/unittest.dart'); |
#import('../../../view/view.dart'); |
-#source('ViewTests.dart'); |
- |
void main() { |
- new ViewTests().run(); |
+ test('does not render immediately', () { |
+ final view = new TestView(); |
+ expect(view.isRendered).isFalse(); |
+ |
+ view.addToDocument(document.body); |
+ expect(view.isRendered).isTrue(); |
+ }); |
+ |
+ group('addToDocument()', () { |
Anton Muhin
2011/10/18 06:41:53
nice!
|
+ test('causes view to render', () { |
+ final view = new TestView(); |
+ view.addToDocument(document.body); |
+ expect(view.isRendered).isTrue(); |
+ }); |
+ |
+ test('calls afterRender()', () { |
+ var result = ''; |
+ final view = new TestView(); |
+ view.renderFn = () { |
+ result += 'render'; |
+ return new Element.html('<div class="test"></div>'); |
+ }; |
+ |
+ view.afterRenderFn = (node) { result += 'after'; }; |
+ |
+ |
+ view.addToDocument(document.body); |
+ expect(result).equals('renderafter'); |
+ }); |
+ |
+ test('calls enterDocument()', () { |
+ final view = new TestView(); |
+ bool entered = false; |
+ view.enterDocumentFn = () { entered = true; }; |
+ |
+ view.addToDocument(document.body); |
+ expect(entered).isTrue(); |
+ }); |
+ }); |
+ |
+ group('afterRender()', () { |
+ test('passes rendered node', () { |
+ final rendered = new Element.html('<div class="node"></div>'); |
+ final view = new TestView(); |
+ view.renderFn = () => rendered; |
+ view.afterRenderFn = (node) { expect(node).equals(rendered); }; |
+ |
+ view.addToDocument(document.body); |
+ }); |
+ }); |
+ |
+ group('childViewAdded()', () { |
+ test('calls enterDocument() if parent is in document', () { |
+ final parent = new TestView(); |
+ parent.addToDocument(document.body); |
+ |
+ bool entered = false; |
+ final child = new TestView(); |
+ child.enterDocumentFn = () { entered = true; }; |
+ |
+ // Add the child. |
+ parent.childViews = [child]; |
+ parent.childViewAdded(child); |
+ |
+ expect(entered).isTrue(); |
+ }); |
+ |
+ test('does not call enterDocument() if parent is not in document', () { |
+ final parent = new TestView(); |
+ |
+ bool entered = false; |
+ final child = new TestView(); |
+ child.enterDocumentFn = () { entered = true; }; |
+ |
+ // Add the child. |
+ parent.childViews = [child]; |
+ parent.childViewAdded(child); |
+ |
+ expect(entered).isFalse(); |
+ }); |
+ |
+ test('calls enterDocument() each time added', () { |
+ final parent = new TestView(); |
+ parent.addToDocument(document.body); |
+ |
+ var entered = 0; |
+ final child = new TestView(); |
+ child.enterDocumentFn = () { entered++; }; |
+ |
+ // Add the child. |
+ parent.childViews = [child]; |
+ parent.childViewAdded(child); |
+ parent.childViewRemoved(child); |
+ parent.childViewAdded(child); |
+ parent.childViewRemoved(child); |
+ parent.childViewAdded(child); |
+ parent.childViewRemoved(child); |
+ |
+ expect(entered).equals(3); |
+ }); |
+ }); |
+ |
+ group('childViewRemoved()', () { |
+ test('calls exitDocument() if parent is in document', () { |
+ final parent = new TestView(); |
+ parent.addToDocument(document.body); |
+ |
+ bool exited = false; |
+ final child = new TestView(); |
+ child.exitDocumentFn = () { exited = true; }; |
+ |
+ // Remove the child. |
+ parent.childViews = []; |
+ parent.childViewRemoved(child); |
+ |
+ expect(exited).isTrue(); |
+ }); |
+ |
+ test('does not call exitDocument() if parent is not in document', () { |
+ final parent = new TestView(); |
+ |
+ bool exited = false; |
+ final child = new TestView(); |
+ child.exitDocumentFn = () { exited = true; }; |
+ |
+ // Remove the child. |
+ parent.childViews = []; |
+ parent.childViewRemoved(child); |
+ |
+ expect(exited).isFalse(); |
+ }); |
+ |
+ test('calls exitDocument() each time removed', () { |
+ final parent = new TestView(); |
+ parent.addToDocument(document.body); |
+ |
+ var exited = 0; |
+ final child = new TestView(); |
+ child.exitDocumentFn = () { exited++; }; |
+ |
+ // Add the child. |
+ parent.childViews = [child]; |
+ parent.childViewAdded(child); |
+ parent.childViewRemoved(child); |
+ parent.childViewAdded(child); |
+ parent.childViewRemoved(child); |
+ parent.childViewAdded(child); |
+ parent.childViewRemoved(child); |
+ |
+ expect(exited).equals(3); |
+ }); |
+ }); |
+ |
+ group('enterDocument()', () { |
+ test('children are called before parents', () { |
+ var result = ''; |
+ |
+ final parent = new TestView(); |
+ parent.enterDocumentFn = () { result += 'parent'; }; |
+ |
+ final child = new TestView(); |
+ child.enterDocumentFn = () { result += 'child'; }; |
+ |
+ parent.childViews = [child]; |
+ |
+ parent.addToDocument(document.body); |
+ expect(result).equals('childparent'); |
+ }); |
+ }); |
+} |
+ |
+class TestView extends View { |
+ Function renderFn; |
+ Function afterRenderFn; |
+ Function enterDocumentFn; |
+ Function exitDocumentFn; |
+ List<View> childViews; |
+ |
+ TestView() : super(), childViews = [] { |
+ // Default behavior. |
+ renderFn = () => new Element.html('<div class="test"></div>'); |
+ afterRenderFn = (node) {}; |
+ enterDocumentFn = () {}; |
+ exitDocumentFn = () {}; |
+ } |
+ |
+ Element render() => renderFn(); |
+ void afterRender(Element node) { afterRenderFn(node); } |
+ void enterDocument() { enterDocumentFn(); } |
+ void exitDocument() { exitDocumentFn(); } |
} |