| 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 library charted.test.selection; | 
 |   10  | 
 |   11 import 'dart:html' show document, Element; | 
 |   12 import 'package:unittest/unittest.dart'; | 
 |   13 import 'package:charted/selection/selection.dart'; | 
 |   14  | 
 |   15 part 'selection_scope_test.dart'; | 
 |   16  | 
 |   17 selectionTests() { | 
 |   18   testSelectionScope(); | 
 |   19   testSelections(); | 
 |   20 } | 
 |   21  | 
 |   22 testSelections() { | 
 |   23   String markup = '<div class="charted-scope-root">' | 
 |   24             '<div class="charted-scope-inner">' | 
 |   25               '<div class="charted-scope-leaf"></div>' | 
 |   26               '<div class="charted-scope-leaf"></div>' | 
 |   27             '</div>' | 
 |   28           '</div>'; | 
 |   29  | 
 |   30   Element root; | 
 |   31   SelectionScope scope; | 
 |   32   Selection empty, single, multiple; | 
 |   33  | 
 |   34   setup() { | 
 |   35     root = new Element.html(markup); | 
 |   36     document.documentElement.append(root); | 
 |   37     scope = new SelectionScope.selector('.charted-scope-root'); | 
 |   38  | 
 |   39     empty = scope.selectAll('.node'); | 
 |   40     single = scope.select('.charted-scope-inner'); | 
 |   41     multiple = scope.selectAll('.charted-scope-leaf'); | 
 |   42   } | 
 |   43  | 
 |   44   teardown() { | 
 |   45     root.remove(); | 
 |   46   } | 
 |   47  | 
 |   48   test('toCallback() creates a callback to return the given value', () { | 
 |   49     num value = 100; | 
 |   50     SelectionCallback<num> cb = toCallback(value); | 
 |   51     expect(cb(null, null, null), equals(value)); | 
 |   52   }); | 
 |   53  | 
 |   54   test('toValueAccessor() creates an accessor to return the given value', () { | 
 |   55     num value = 100; | 
 |   56     SelectionValueAccessor<num> cb = toValueAccessor(value); | 
 |   57     expect(cb(null, null), equals(value)); | 
 |   58   }); | 
 |   59  | 
 |   60   group('Selection created from scope', () { | 
 |   61     setUp(setup); | 
 |   62     tearDown(teardown); | 
 |   63  | 
 |   64     test('has isEmpty=true when the selection is empty', () { | 
 |   65       expect(empty.isEmpty, equals(true)); | 
 |   66       expect(single.isEmpty, equals(false)); | 
 |   67       expect(multiple.isEmpty, equals(false)); | 
 |   68     }); | 
 |   69  | 
 |   70     test('has length set to number of non-null elements', () { | 
 |   71       expect(empty.length, equals(0)); | 
 |   72       expect(single.length, equals(1)); | 
 |   73       expect(multiple.length, equals(2)); | 
 |   74     }); | 
 |   75  | 
 |   76     test('has first=null when there selection is empty', () { | 
 |   77       expect(empty.first, equals(null)); | 
 |   78     }); | 
 |   79  | 
 |   80     test('has first set to first non-null element in selection', () { | 
 |   81       expect(single.first.className, equals('charted-scope-inner')); | 
 |   82       expect(multiple.first.className, equals('charted-scope-leaf')); | 
 |   83     }); | 
 |   84  | 
 |   85     test('has exactly one group', () { | 
 |   86       expect(empty.groups.length, equals(1)); | 
 |   87       expect(single.groups.length, equals(1)); | 
 |   88       expect(multiple.groups.length, equals(1)); | 
 |   89     }); | 
 |   90   }); | 
 |   91  | 
 |   92   group('Selection created from another selection', () { | 
 |   93     Selection selection1, selection2, selection3; | 
 |   94  | 
 |   95     setUp(() { | 
 |   96       setup(); | 
 |   97       selection1 = empty.selectAll('.child-nodes'); | 
 |   98       selection2 = single.selectAll('.child-nodes'); | 
 |   99       selection3 = multiple.selectAll('.child-nodes'); | 
 |  100     }); | 
 |  101     tearDown(teardown); | 
 |  102  | 
 |  103     test('has length set to number of non-null elements', () { | 
 |  104       expect(selection1.length, equals(0)); | 
 |  105       expect(selection2.length, equals(0)); | 
 |  106       expect(selection3.length, equals(0)); | 
 |  107     }); | 
 |  108  | 
 |  109     test('has first=null when there selection is empty', () { | 
 |  110       expect(selection1.first, equals(null)); | 
 |  111       expect(selection2.first, equals(null)); | 
 |  112       expect(selection3.first, equals(null)); | 
 |  113     }); | 
 |  114  | 
 |  115     test('has same number of groups as number of non-null elements ' | 
 |  116         'in source selection', () { | 
 |  117       expect(selection1.groups.length, equals(empty.length)); | 
 |  118       expect(selection2.groups.length, equals(single.length)); | 
 |  119       expect(selection3.groups.length, equals(multiple.length)); | 
 |  120     }); | 
 |  121   }); | 
 |  122  | 
 |  123   group('Selection', () { | 
 |  124     setUp(setup); | 
 |  125     tearDown(teardown); | 
 |  126  | 
 |  127     test('attr sets attribute to the specified value on selected', () { | 
 |  128       multiple.each((d, i, e) { | 
 |  129         expect(e.attributes['height'], isNull); | 
 |  130       }); | 
 |  131       multiple.attr('height', '10'); | 
 |  132       multiple.each((d, i, e) { | 
 |  133         expect(e.attributes['height'], equals('10')); | 
 |  134       }); | 
 |  135     }); | 
 |  136  | 
 |  137     test('attrWithCallBack sets attribute to the specified value ' | 
 |  138          'on selected elements with callback', () { | 
 |  139       multiple.each((d, i, e) { | 
 |  140         expect(e.attributes['height'], isNull); | 
 |  141       }); | 
 |  142       multiple.attrWithCallback('height', (d, i, e) => '${i * 10}'); | 
 |  143       multiple.each((d, i, e) { | 
 |  144         expect(e.attributes['height'], equals('${i * 10}')); | 
 |  145       }); | 
 |  146     }); | 
 |  147  | 
 |  148  | 
 |  149     test('classed sets class to the specified value on selected', () { | 
 |  150       multiple.classed('new-class'); | 
 |  151       multiple.each((d, i, e) { | 
 |  152         expect(e.attributes['class'], equals('charted-scope-leaf new-class')); | 
 |  153       }); | 
 |  154       multiple.classed('new-class', false); | 
 |  155       multiple.each((d, i, e) { | 
 |  156         expect(e.attributes['class'], equals('charted-scope-leaf')); | 
 |  157       }); | 
 |  158     }); | 
 |  159  | 
 |  160     test('classedWithCallBack sets class to the specified value ' | 
 |  161          'on selected elements with callback', () { | 
 |  162       multiple.classedWithCallback('new-class', (d, i, e) => i % 2 > 0); | 
 |  163       multiple.each((d, i, e) { | 
 |  164         if (i % 2 > 0) { | 
 |  165           expect(e.attributes['class'], equals('charted-scope-leaf new-class')); | 
 |  166         } else { | 
 |  167           expect(e.attributes['class'], equals('charted-scope-leaf')); | 
 |  168         } | 
 |  169       }); | 
 |  170       multiple.classedWithCallback('new-class', (d, i, e) => i % 2 == 0); | 
 |  171       multiple.each((d, i, e) { | 
 |  172         if (i % 2 == 0) { | 
 |  173           expect(e.attributes['class'], equals('charted-scope-leaf new-class')); | 
 |  174         } else { | 
 |  175           expect(e.attributes['class'], equals('charted-scope-leaf')); | 
 |  176         } | 
 |  177       }); | 
 |  178     }); | 
 |  179  | 
 |  180     test('style sets CSS style to specified value on selected', () { | 
 |  181       multiple.each((d, i, e) { | 
 |  182         expect(e.attributes['style'], isNull); | 
 |  183       }); | 
 |  184       multiple.style('height', '10px'); | 
 |  185       multiple.each((d, i, e) { | 
 |  186         expect(e.attributes['style'], equals('height: 10px;')); | 
 |  187       }); | 
 |  188     }); | 
 |  189  | 
 |  190     test('attrWithCallBack sets CSS style to the specified value ' | 
 |  191          'on selected elements with callback', () { | 
 |  192       multiple.each((d, i, e) { | 
 |  193         expect(e.attributes['style'], isNull); | 
 |  194       }); | 
 |  195       multiple.styleWithCallback('height', (d, i, e) => '${i * 10}px'); | 
 |  196       multiple.each((d, i, e) { | 
 |  197         expect(e.attributes['style'], equals('height: ${i * 10}px;')); | 
 |  198       }); | 
 |  199     }); | 
 |  200  | 
 |  201     test('text sets text to specified value on selected', () { | 
 |  202       multiple.each((d, i, e) { | 
 |  203         expect(e.text, ''); | 
 |  204       }); | 
 |  205       multiple.text('some text'); | 
 |  206       multiple.each((d, i, e) { | 
 |  207         expect(e.text, equals('some text')); | 
 |  208       }); | 
 |  209     }); | 
 |  210  | 
 |  211     test('textWithCallBack sets text to the specified value ' | 
 |  212          'on selected elements with callback', () { | 
 |  213       multiple.each((d, i, e) { | 
 |  214         expect(e.text, ''); | 
 |  215       }); | 
 |  216       multiple.textWithCallback((d, i, e) => 'text-${i}'); | 
 |  217       multiple.each((d, i, e) { | 
 |  218         expect(e.text, equals('text-${i}')); | 
 |  219       }); | 
 |  220     }); | 
 |  221  | 
 |  222     test('html sets inner html to specified value on selected', () { | 
 |  223       multiple.each((d, i, e) { | 
 |  224         expect(e.innerHtml, ''); | 
 |  225       }); | 
 |  226       multiple.text('some html'); | 
 |  227       multiple.each((d, i, e) { | 
 |  228         expect(e.innerHtml, equals('some html')); | 
 |  229       }); | 
 |  230     }); | 
 |  231  | 
 |  232     test('htmlWithCallBack sets inner html to the specified value ' | 
 |  233          'on selected elements with callback', () { | 
 |  234       multiple.each((d, i, e) { | 
 |  235         expect(e.innerHtml, ''); | 
 |  236       }); | 
 |  237       multiple.htmlWithCallback((d, i, e) => 'html-${i}'); | 
 |  238       multiple.each((d, i, e) { | 
 |  239         expect(e.innerHtml, equals('html-${i}')); | 
 |  240       }); | 
 |  241     }); | 
 |  242  | 
 |  243     test('append appends new child elements to selection', () { | 
 |  244       expect(multiple.selectAll('.appended').length, equals(0)); | 
 |  245       multiple.append('div')..classed('appended'); | 
 |  246       expect(multiple.selectAll('.appended').length, equals(multiple.length)); | 
 |  247       multiple.append('div')..classed('appended'); | 
 |  248       expect(multiple.selectAll('.appended').length, | 
 |  249           equals(multiple.length * 2)); | 
 |  250     }); | 
 |  251  | 
 |  252     test('appendWithCallback appends new child elements ' | 
 |  253          'to selection with callback', () { | 
 |  254       for (var i = 0; i < multiple.length; i++) { | 
 |  255         expect(multiple.selectAll('.appended-${i}').length, equals(0)); | 
 |  256       } | 
 |  257       multiple.appendWithCallback((d, i, e) { | 
 |  258         Element newItem = new Element.tag('div') | 
 |  259                         ..className = 'appended-${i}'; | 
 |  260         return newItem; | 
 |  261       }); | 
 |  262       for (var i = 0; i < multiple.length; i++) { | 
 |  263         expect(multiple.selectAll('.appended-${i}').length, equals(1)); | 
 |  264       } | 
 |  265     }); | 
 |  266  | 
 |  267     test('insert inserts new child elements to selection', () { | 
 |  268       expect(multiple.selectAll('.appended').length, equals(0)); | 
 |  269       multiple.insert('div', before: '.appended')..classed('appended'); | 
 |  270       expect(multiple.selectAll('.appended').length, equals(multiple.length)); | 
 |  271       multiple.insert('div', before: '.appended')..classed('appended'); | 
 |  272       expect(multiple.selectAll('.appended').length, | 
 |  273           equals(multiple.length * 2)); | 
 |  274     }); | 
 |  275  | 
 |  276     test('insertWithCallback inserts new child elements ' | 
 |  277          'to selection with callback', () { | 
 |  278       for (var i = 0; i < multiple.length; i++) { | 
 |  279         expect(multiple.selectAll('.appended-${i}').length, equals(0)); | 
 |  280       } | 
 |  281       multiple.insertWithCallback((d, i, e) { | 
 |  282         Element newItem = new Element.tag('div') | 
 |  283                         ..className = 'appended-${i}'; | 
 |  284         return newItem; | 
 |  285       }, beforeFn: (d, i, e) => multiple.selectAll('.appended-${i}').first); | 
 |  286       for (var i = 0; i < multiple.length; i++) { | 
 |  287         expect(multiple.selectAll('.appended-${i}').length, equals(1)); | 
 |  288       } | 
 |  289     }); | 
 |  290  | 
 |  291     test('remove removes selected elements', () { | 
 |  292       Selection remove = scope.selectAll('.charted-scope-leaf'); | 
 |  293       expect(remove.length, equals(2)); | 
 |  294       remove.remove(); | 
 |  295       remove = scope.selectAll('.charted-scope-leaf'); | 
 |  296       expect(remove.length, equals(0)); | 
 |  297     }); | 
 |  298  | 
 |  299     test('each visits each selected element in order', () { | 
 |  300       multiple.attrWithCallback('height', (d, i, e) => i * 10); | 
 |  301       int count = 0; | 
 |  302       multiple.each((d, i, e) { | 
 |  303         expect(e.attributes['height'], equals('${i * 10}')); | 
 |  304         count++; | 
 |  305       }); | 
 |  306       expect(count, equals(multiple.length)); | 
 |  307     }); | 
 |  308  | 
 |  309     test('data binds data to selected elements', () { | 
 |  310       List dataList = [1, 2]; | 
 |  311       multiple.data(dataList); | 
 |  312       multiple.each((d, i, e) => expect(d, dataList[i])); | 
 |  313     }); | 
 |  314  | 
 |  315     test('dataWithCallback binds data to elements with callback', () { | 
 |  316       List dataList = [1, 2]; | 
 |  317       multiple.dataWithCallback((d, i, e) => dataList); | 
 |  318       multiple.each((d, i, e) => expect(d, dataList[i])); | 
 |  319     }); | 
 |  320   }); | 
 |  321 } | 
| OLD | NEW |