document_child_mutations_test.dart 3.08 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
import 'dart:sky';

import 'package:test/test.dart';

import 'dom_utils.dart';

void main() {
  var doc;

  setUp(() {
    doc = new Document();
  });

  test("should allow replacing the document element", () {
    var oldChild = doc.appendChild(doc.createElement("div"));
    expect(childElementCount(doc), equals(1));
    var newChild = doc.createElement("div");
    oldChild.replaceWith([newChild]);
    expect(childElementCount(doc), equals(1));
    expect(newChild.parentNode, equals(doc));
    expect(oldChild.parentNode, isNull);
  });

  test("should allow replacing a text child with an element", () {
    var oldChild = doc.appendChild(doc.createText("text here"));
    expect(childElementCount(doc), equals(0));
    expect(childNodeCount(doc), equals(1));
    var newChild = doc.createElement("div");
    oldChild.replaceWith([newChild]);
    expect(childElementCount(doc), equals(1));
    expect(childNodeCount(doc), equals(1));
    expect(newChild.parentNode, equals(doc));
    expect(oldChild.parentNode, isNull);
  });

  test("should allow replacing the document element with text", () {
    var oldChild = doc.appendChild(doc.createElement("div"));
    expect(childElementCount(doc), equals(1));
    var newChild = doc.createText(" text ");
    oldChild.replaceWith([newChild]);
    expect(childElementCount(doc), equals(0));
    expect(childNodeCount(doc), equals(1));
    expect(newChild.parentNode, equals(doc));
    expect(oldChild.parentNode, isNull);
  });

  test("should allow inserting text with a fragment", () {
    var fragment = doc.createDocumentFragment();
    fragment.appendChild(doc.createText(" text "));
    fragment.appendChild(doc.createText(" text "));
    expect(childNodeCount(doc), equals(0));
    doc.appendChild(fragment);
    expect(childElementCount(doc), equals(0));
    expect(childNodeCount(doc), equals(2));
  });

  test("should allow replacing the document element with a fragment", () {
    var oldChild = doc.appendChild(doc.createElement("div"));
    expect(childElementCount(doc), equals(1));
    var fragment = doc.createDocumentFragment();
    fragment.appendChild(doc.createText(" text "));
    var newChild = fragment.appendChild(doc.createElement("div"));
    fragment.appendChild(doc.createText(" "));
    oldChild.replaceWith([fragment]);
    expect(childElementCount(doc), equals(1));
    expect(childNodeCount(doc), equals(3));
    expect(newChild.parentNode, equals(doc));
    expect(oldChild.parentNode, isNull);
  });

  test("should throw when inserting multiple elements", () {
    doc.appendChild(doc.createElement("div"));
Adam Barth's avatar
Adam Barth committed
73
    doc.appendChild(doc.createText(" text "));
74
    expect(childElementCount(doc), equals(1));
Adam Barth's avatar
Adam Barth committed
75
    doc.createElement("div");
76 77 78
  });

  test("should throw when inserting multiple elements with a fragment", () {
Adam Barth's avatar
Adam Barth committed
79
    doc.appendChild(doc.createElement("div"));
80 81 82 83 84 85 86 87
    expect(childElementCount(doc), equals(1));
    var fragment = doc.createDocumentFragment();
    fragment.appendChild(doc.createText(" text "));
    fragment.appendChild(doc.createElement("div"));
    fragment.appendChild(doc.createElement("div"));
    fragment.appendChild(doc.createText(" "));
  });
}