OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 Google Inc. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style |
| 5 * license that can be found in the LICENSE file or at |
| 6 * https://developers.google.com/open-source/licenses/bsd |
| 7 */ |
| 8 |
| 9 part of charted.test.selection; |
| 10 |
| 11 testSelectionScope() { |
| 12 String markup = '<div class="charted-scope-root">' |
| 13 '<div class="charted-scope-inner">' |
| 14 '<div class="charted-scope-leaf"></div>' |
| 15 '<div class="charted-scope-leaf"></div>' |
| 16 '</div>' |
| 17 '</div>'; |
| 18 Element root, inner; |
| 19 SelectionScope scope1, scope2, scope3; |
| 20 |
| 21 _setup() { |
| 22 root = new Element.html(markup); |
| 23 inner = root.querySelector('.charted-scope-inner'); |
| 24 |
| 25 document.documentElement.append(root); |
| 26 |
| 27 scope1 = new SelectionScope.selector('.charted-scope-root'); |
| 28 scope2 = new SelectionScope.element(root); |
| 29 scope3 = new SelectionScope(); |
| 30 }; |
| 31 |
| 32 _teardown() { |
| 33 root.remove(); |
| 34 } |
| 35 |
| 36 group('Creating SelectionScope', () { |
| 37 setUp(_setup); |
| 38 tearDown(_teardown); |
| 39 |
| 40 test('by selector and element should have the same root', () { |
| 41 expect(scope1.root, equals(scope2.root)); |
| 42 }); |
| 43 |
| 44 test('should use documentElement as root when nothing is specified', () { |
| 45 expect(scope3.root, equals(document.documentElement)); |
| 46 }); |
| 47 }); |
| 48 |
| 49 group('SelectionScope.associate', () { |
| 50 setUp(_setup); |
| 51 tearDown(_teardown); |
| 52 |
| 53 test('should associate data to an element', () { |
| 54 scope1.associate(inner, 10); |
| 55 expect(scope1.datum(inner), equals(10)); |
| 56 }); |
| 57 |
| 58 test('should store the associations on scope', () { |
| 59 scope1.associate(inner, 10); |
| 60 scope2.associate(inner, 20); |
| 61 expect(scope1.datum(inner), equals(10)); |
| 62 expect(scope2.datum(inner), equals(20)); |
| 63 }); |
| 64 }); |
| 65 |
| 66 group('SelectionScope.select', () { |
| 67 setUp(_setup); |
| 68 tearDown(_teardown); |
| 69 |
| 70 test('must create a selection containing atmost one element', () { |
| 71 var selection1 = scope1.select('.charted-scope-inner'), |
| 72 selection2 = scope1.select('.charted-scope-leaf'); |
| 73 expect(selection1.length, equals(1)); |
| 74 expect(selection2.length, equals(1)); |
| 75 }); |
| 76 |
| 77 test('must create a empty selection when nothing matches', () { |
| 78 var selection = scope1.select('.charted-scope-invalid'); |
| 79 expect(selection.length, equals(0)); |
| 80 }); |
| 81 }); |
| 82 |
| 83 group('SelectionScope.selectAll', () { |
| 84 setUp(_setup); |
| 85 tearDown(_teardown); |
| 86 |
| 87 test('must create a selection containing all matching elements', () { |
| 88 var selection1 = scope1.selectAll('.charted-scope-inner'), |
| 89 selection2 = scope1.selectAll('.charted-scope-leaf'), |
| 90 selection3 = scope1.selectAll('.charted-scope-invalid'); |
| 91 |
| 92 expect(selection1.length, equals(1)); |
| 93 expect(selection1.first.className, equals('charted-scope-inner')); |
| 94 expect(selection2.length, equals(2)); |
| 95 expect(selection2.first.className, equals('charted-scope-leaf')); |
| 96 expect(selection3.length, equals(0)); |
| 97 }); |
| 98 }); |
| 99 |
| 100 group('SelectionScope.selectElements', () { |
| 101 setUp(_setup); |
| 102 tearDown(_teardown); |
| 103 |
| 104 test('must create a selection containing passed elements', () { |
| 105 var elements = scope1.root.querySelectorAll('.charted-scope-leaf'), |
| 106 selection = scope1.selectElements(elements.toList()); |
| 107 expect(selection.length, equals(elements.toList().length)); |
| 108 |
| 109 var selected = []; |
| 110 selection.each((d,i,e) => selected.add(e)); |
| 111 expect(selected, unorderedEquals(elements.toList())); |
| 112 }); |
| 113 }); |
| 114 |
| 115 group('SelectionScope.append', () { |
| 116 setUp(_setup); |
| 117 tearDown(_teardown); |
| 118 |
| 119 test('must append and element and create a selection', () { |
| 120 var selection1 = scope1.append('charted-test-dummy'); |
| 121 expect(selection1.length, equals(1)); |
| 122 expect(selection1.first.tagName, |
| 123 equalsIgnoringCase('charted-test-dummy')); |
| 124 expect(scope1.root.querySelector('charted-test-dummy'), |
| 125 isNot(equals(null))); |
| 126 }); |
| 127 }); |
| 128 } |
OLD | NEW |