replace_child_test.dart 1.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
import 'dart:sky';

import 'package:test/test.dart';

import 'dom_utils.dart';

void main() {
  var document = new Document();

  test("should replace elements", () {
    var parent = document.createElement("div");
    var oldChild = parent.appendChild(document.createElement("div"));
    var newChild = document.createElement("div");
    oldChild.replaceWith([newChild]);
    expect(oldChild.parentNode, isNull);
    expect(newChild.parentNode, equals(parent));
  });

  test("should replace text", () {
    var parent = document.createElement("div");
    var oldChild = parent.appendChild(document.createText(" it's a text "));
    var newChild = document.createElement("div");
    oldChild.replaceWith([newChild]);
    expect(oldChild.parentNode, isNull);
    expect(newChild.parentNode, equals(parent));
  });

  test("should replace children with a fragment", () {
    var fragment = document.createDocumentFragment();
    var child1 = fragment.appendChild(document.createElement("div"));
    var child2 = fragment.appendChild(document.createText(" text "));
    var child3 = fragment.appendChild(document.createText(" "));
    var child4 = fragment.appendChild(document.createElement("div"));
    var parent = document.createElement("div");
    var oldChild = parent.appendChild(document.createElement("div"));
    var lastChild = parent.appendChild(document.createElement("div"));
    oldChild.replaceWith([fragment]);
    expect(child1.parentNode, equals(parent));
    expect(child2.parentNode, equals(parent));
    expect(child3.parentNode, equals(parent));
    expect(child4.parentNode, equals(parent));
    expect(oldChild.parentNode, isNull);
    expect(childNodeCount(parent), equals(5));
    expect(childElementCount(parent), equals(3));
    expect(parent.lastChild, equals(lastChild));
  });

  // test("should throw when appending to a text", () {
  //   var parent = new Text();
  //   expect(() {
  //     parent.replaceChild(document.createElement("div"), null);
  //   }, throws);
  // });
}