2012-11-01 23:09:47 +00:00
|
|
|
library ElementTest;
|
2014-04-09 19:42:11 +00:00
|
|
|
import 'package:unittest/unittest.dart';
|
|
|
|
import 'package:unittest/html_config.dart';
|
2012-11-01 23:09:47 +00:00
|
|
|
import 'dart:html';
|
2013-10-10 21:42:13 +00:00
|
|
|
import 'utils.dart';
|
2012-04-28 10:34:16 +00:00
|
|
|
|
|
|
|
main() {
|
|
|
|
useHtmlConfiguration();
|
|
|
|
test('InnerHTML', () {
|
|
|
|
Element element = new Element.tag('div');
|
|
|
|
element.id = 'test';
|
2012-11-27 17:38:23 +00:00
|
|
|
element.innerHtml = 'Hello World';
|
2013-05-13 20:57:52 +00:00
|
|
|
document.body.append(element);
|
2012-04-28 10:34:16 +00:00
|
|
|
|
|
|
|
element = document.query('#test');
|
2012-11-27 17:38:23 +00:00
|
|
|
expect(element.innerHtml, 'Hello World');
|
2012-04-28 10:34:16 +00:00
|
|
|
element.remove();
|
|
|
|
});
|
|
|
|
test('HTMLTable', () {
|
|
|
|
Element table = new Element.tag('table');
|
|
|
|
|
|
|
|
TableRowElement row = new Element.tag('tr');
|
2013-05-13 20:57:52 +00:00
|
|
|
table.append(row);
|
2012-04-28 10:34:16 +00:00
|
|
|
|
2013-05-13 20:57:52 +00:00
|
|
|
row.append(new Element.tag('td'));
|
|
|
|
row.append(new Element.tag('td'));
|
2012-04-28 10:34:16 +00:00
|
|
|
|
2012-10-26 22:52:03 +00:00
|
|
|
expect(row.cells.length, 2);
|
2012-04-28 10:34:16 +00:00
|
|
|
|
2012-05-25 12:38:23 +00:00
|
|
|
TableRowElement headerRow = table.rows[0];
|
2012-10-26 22:52:03 +00:00
|
|
|
expect(headerRow.cells.length, 2);
|
2012-04-28 10:34:16 +00:00
|
|
|
});
|
2013-02-14 19:12:38 +00:00
|
|
|
test('dataset', () {
|
2012-04-28 10:34:16 +00:00
|
|
|
Element div = new Element.tag('div');
|
|
|
|
|
2013-02-14 19:12:38 +00:00
|
|
|
expect(div.dataset.isEmpty, isTrue);
|
|
|
|
expect(div.dataset['foo'], isNull);
|
|
|
|
expect(div.dataset.isEmpty, isTrue);
|
2012-04-28 10:34:16 +00:00
|
|
|
|
2013-02-14 19:12:38 +00:00
|
|
|
div.dataset['foo'] = 'foo-value';
|
|
|
|
expect(div.dataset['foo'], 'foo-value');
|
|
|
|
expect(div.dataset.isEmpty, isFalse);
|
2012-04-28 10:34:16 +00:00
|
|
|
|
2013-02-14 19:12:38 +00:00
|
|
|
expect(div.dataset.containsValue('foo-value'), isTrue);
|
|
|
|
expect(div.dataset.containsValue('bar-value'), isFalse);
|
|
|
|
expect(div.dataset.containsKey('foo'), isTrue);
|
|
|
|
expect(div.dataset.containsKey('bar'), isFalse);
|
2012-04-28 10:34:16 +00:00
|
|
|
|
|
|
|
bool hasBeenInvoked;
|
|
|
|
String f() {
|
|
|
|
hasBeenInvoked = true;
|
|
|
|
return 'bar-value';
|
|
|
|
}
|
|
|
|
|
|
|
|
hasBeenInvoked = false;
|
2013-02-14 19:12:38 +00:00
|
|
|
expect(div.dataset.putIfAbsent('bar', f), 'bar-value');
|
2012-10-26 22:52:03 +00:00
|
|
|
expect(hasBeenInvoked, isTrue);
|
2012-04-28 10:34:16 +00:00
|
|
|
|
|
|
|
hasBeenInvoked = false;
|
2013-02-14 19:12:38 +00:00
|
|
|
expect(div.dataset.putIfAbsent('bar', f), 'bar-value');
|
2012-10-26 22:52:03 +00:00
|
|
|
expect(hasBeenInvoked, isFalse);
|
2012-04-28 10:34:16 +00:00
|
|
|
|
|
|
|
final keys = <String> [];
|
|
|
|
final values = <String> [];
|
2013-02-14 19:12:38 +00:00
|
|
|
div.dataset.forEach((String key, String value) {
|
2012-04-28 10:34:16 +00:00
|
|
|
keys.add(key);
|
|
|
|
values.add(value);
|
|
|
|
});
|
2012-10-26 22:52:03 +00:00
|
|
|
expect(keys, unorderedEquals(['foo', 'bar']));
|
|
|
|
expect(values, unorderedEquals(['foo-value', 'bar-value']));
|
2012-04-28 10:34:16 +00:00
|
|
|
|
2013-02-14 19:12:38 +00:00
|
|
|
expect(new List<String>.from(div.dataset.keys),
|
2012-10-26 22:52:03 +00:00
|
|
|
unorderedEquals(['foo', 'bar']));
|
2013-02-14 19:12:38 +00:00
|
|
|
expect(new List<String>.from(div.dataset.values),
|
2012-10-26 22:52:03 +00:00
|
|
|
unorderedEquals(['foo-value', 'bar-value']));
|
2012-04-28 10:34:16 +00:00
|
|
|
|
2013-02-14 19:12:38 +00:00
|
|
|
expect(div.dataset.length, 2);
|
|
|
|
expect(div.dataset.isEmpty, isFalse);
|
2012-04-28 10:34:16 +00:00
|
|
|
|
2013-02-14 19:12:38 +00:00
|
|
|
expect(div.dataset.remove('qux'), isNull);
|
|
|
|
expect(div.dataset.length, 2);
|
|
|
|
expect(div.dataset.isEmpty, isFalse);
|
2012-04-28 10:34:16 +00:00
|
|
|
|
2013-02-14 19:12:38 +00:00
|
|
|
expect(div.dataset.remove('foo'), 'foo-value');
|
|
|
|
expect(div.dataset.length, 1);
|
|
|
|
expect(div.dataset.isEmpty, isFalse);
|
2012-04-28 10:34:16 +00:00
|
|
|
|
2013-02-14 19:12:38 +00:00
|
|
|
div.dataset.clear();
|
|
|
|
expect(div.dataset.length, 0);
|
|
|
|
expect(div.dataset.isEmpty, isTrue);
|
2013-10-10 21:42:13 +00:00
|
|
|
|
|
|
|
Element otherDiv = new Element.html(
|
|
|
|
'<div id="dataDiv" data-my-message="Hello World"></div>',
|
|
|
|
treeSanitizer: new NullTreeSanitizer());
|
|
|
|
expect(otherDiv.dataset.containsKey('myMessage'), isTrue);
|
|
|
|
|
|
|
|
Element anotherDiv = new Element.html(
|
|
|
|
'<div id="dataDiv" data-eggs="bacon"></div>',
|
|
|
|
treeSanitizer: new NullTreeSanitizer());
|
|
|
|
expect(anotherDiv.dataset.containsKey('eggs'), isTrue);
|
2012-04-28 10:34:16 +00:00
|
|
|
});
|
|
|
|
}
|