replace_child_test.dart 2.04 KB
Newer Older
1
import 'dart:ui';
2 3 4 5 6 7

import 'package:test/test.dart';

import 'dom_utils.dart';

void main() {
8
  Document document = new Document();
9 10

  test("should replace elements", () {
11 12 13 14
    Element parent = document.createElement("div");
    Element oldChild = parent.appendChild(document.createElement("div"));
    Element newChild = document.createElement("div");
    oldChild.replaceWith(<Node>[newChild]);
15 16 17 18 19
    expect(oldChild.parentNode, isNull);
    expect(newChild.parentNode, equals(parent));
  });

  test("should replace text", () {
20 21 22 23
    Element parent = document.createElement("div");
    Node oldChild = parent.appendChild(document.createText(" it's a text "));
    Element newChild = document.createElement("div");
    oldChild.replaceWith(<Node>[newChild]);
24 25 26 27 28
    expect(oldChild.parentNode, isNull);
    expect(newChild.parentNode, equals(parent));
  });

  test("should replace children with a fragment", () {
29 30 31 32 33 34 35 36 37
    DocumentFragment fragment = document.createDocumentFragment();
    Element child1 = fragment.appendChild(document.createElement("div"));
    Node child2 = fragment.appendChild(document.createText(" text "));
    Node child3 = fragment.appendChild(document.createText(" "));
    Element child4 = fragment.appendChild(document.createElement("div"));
    Element parent = document.createElement("div");
    Element oldChild = parent.appendChild(document.createElement("div"));
    Element lastChild = parent.appendChild(document.createElement("div"));
    oldChild.replaceWith(<Node>[fragment]);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    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);
  // });
}