Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 750525e

Browse files
committed
feat(expect): Move JQuery.html to Expect.toHaveHtml()
For #219
1 parent 062249d commit 750525e

10 files changed

+87
-87
lines changed

test/_specs.dart

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class Expect {
5050
toBeNull() => unit.expect(actual, unit.isNull);
5151
toBeNotNull() => unit.expect(actual, unit.isNotNull);
5252

53+
toHaveHtml(expected) => unit.expect(_toHtml(actual), unit.equals(expected));
54+
5355
toHaveBeenCalled() => unit.expect(actual.called, true, reason: 'method not called');
5456
toHaveBeenCalledOnce() => unit.expect(actual.count, 1, reason: 'method invoked ${actual.count} expected once');
5557
toHaveBeenCalledWith([a,b,c,d,e,f]) =>
@@ -87,6 +89,34 @@ class Expect {
8789
toEqualDirty() {
8890
// TODO: implement onece we have forms
8991
}
92+
93+
94+
_toHtml(node, [bool outer = false]) {
95+
if (node is Comment) {
96+
return '<!--${node.text}-->';
97+
} else if (node is DocumentFragment) {
98+
var acc = '';
99+
node.childNodes.forEach((n) { acc += _toHtml(n, true); });
100+
return acc;
101+
} else if (node is List) {
102+
var acc = '';
103+
node.forEach((n) { acc += _toHtml(n); });
104+
return acc;
105+
} else if (node is Element) {
106+
// Remove all the "ng-binding" internal classes
107+
node = node.clone(true) as Element;
108+
node.classes.remove('ng-binding');
109+
node.querySelectorAll(".ng-binding").forEach((Element e) {
110+
e.classes.remove('ng-binding');
111+
});
112+
var htmlString = outer ? node.outerHtml : node.innerHtml;
113+
// Strip out empty class attributes. This seems like a Dart bug...
114+
return htmlString.replaceAll(' class=""', '').trim();
115+
} else {
116+
throw "JQuery._toHtml not implemented for node type [${node.nodeType}]";
117+
}
118+
}
119+
90120
}
91121

92122
class NotExpect {
@@ -155,28 +185,6 @@ class JQuery extends DelegatingList<Node> {
155185
}
156186
}
157187

158-
_toHtml(node, [bool outer = false]) {
159-
if (node is Comment) {
160-
return '<!--${node.text}-->';
161-
} else if (node is DocumentFragment) {
162-
var acc = '';
163-
node.childNodes.forEach((n) { acc += _toHtml(n, true); });
164-
return acc;
165-
} else if (node is Element) {
166-
// Remove all the "ng-binding" internal classes
167-
node = node.clone(true) as Element;
168-
node.classes.remove('ng-binding');
169-
node.querySelectorAll(".ng-binding").forEach((Element e) {
170-
e.classes.remove('ng-binding');
171-
});
172-
var htmlString = outer ? node.outerHtml : node.innerHtml;
173-
// Strip out empty class attributes. This seems like a Dart bug...
174-
return htmlString.replaceAll(' class=""', '');
175-
} else {
176-
throw "JQuery._toHtml not implemented for node type [${node.nodeType}]";
177-
}
178-
}
179-
180188
_renderedText(n, [bool notShadow = false]) {
181189
if (n is List) {
182190
return n.map((nn) => _renderedText(nn)).join("");
@@ -210,13 +218,12 @@ class JQuery extends DelegatingList<Node> {
210218
}
211219

212220
html([String html]) => accessor(
213-
(n) => _toHtml(n),
221+
(n) { throw "Not implemented"; },
214222
(n, v) => n.setInnerHtml(v, treeSanitizer: new NullTreeSanitizer()),
215223
html);
216224
val([String text]) => accessor((n) => n.value, (n, v) => n.value = v);
217225
text([String text]) => accessor((n) => n.text, (n, v) => n.text = v, text);
218226
contents() => fold(new JQuery(), (jq, node) => jq..addAll(node.nodes));
219-
toString() => fold('', (html, node) => '$html${_toHtml(node, true)}');
220227
eq(num childIndex) => $(this[childIndex]);
221228
remove(_) => forEach((n) => n.remove());
222229
attr([String name, String value]) => accessor(

test/_specs_spec.dart

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,21 @@ library _specs_spec;
33
import '_specs.dart';
44

55
main() {
6-
7-
8-
9-
describe('jquery', () {
10-
describe('html', () {
11-
it('get', (){
12-
var div = $('<div>');
13-
expect(div.html()).toEqual('');
6+
describe('expect', () {
7+
describe('toHaveHtml', () {
8+
it('should return html', (){
9+
var div = es('<div>');
10+
expect(es('<div>')).toHaveHtml('');
1411
});
1512

1613
it('should strip ng-binding', () {
17-
var div = $('<div><span class="ng-binding"></span></div>');
18-
expect(div.html()).toEqual('<span></span>');
19-
});
20-
21-
it('set', (){
22-
var div = $('<div>');
23-
expect(div.html('text')).toBe(div);
24-
expect(div.html()).toEqual('text');
14+
var div = es('<div><span class="ng-binding"></span></div>');
15+
expect(div).toHaveHtml('<span></span>');
2516
});
2617
});
18+
});
2719

20+
describe('jquery', () {
2821
describe('shadowRoot', () {
2922
it('should return the shadowRoot if one exists', () {
3023
var elts = $('<div></div>');
@@ -39,7 +32,7 @@ main() {
3932
it('should print the html for the shadowRoot', () {
4033
var elts = $('<div></div>');
4134
elts[0].createShadowRoot().innerHtml = '<div class="ng-binding">Hello shadow</div>';
42-
expect(elts.shadowRoot().html()).toEqual('<div>Hello shadow</div>');
35+
expect(elts.shadowRoot()[0]).toHaveHtml('<div>Hello shadow</div>');
4336
});
4437
});
4538

test/bootstrap_spec.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ void main() {
77
setBody(String html) {
88
var body = window.document.querySelector('body');
99
body.setInnerHtml(html, treeSanitizer: new NullTreeSanitizer());
10-
return $(body);
10+
return body;
1111
}
1212

1313
it('should default to whole page', () {
1414
var body = setBody('<div>{{"works"}}</div>');
1515
ngBootstrap();
16-
expect($(body).html()).toEqual('<div>works</div>');
16+
expect(body).toHaveHtml('<div>works</div>');
1717
});
1818

1919
it('should compile starting at ng-app node', () {
2020
var body = setBody(
2121
'<div>{{ignor me}}<div ng-app ng-bind="\'works\'"></div></div>');
2222
ngBootstrap();
23-
expect(body.text()).toEqual('{{ignor me}}works');
23+
expect(body.text).toEqual('{{ignor me}}works');
2424
});
2525

2626
it('should compile starting at ng-app node', () {
2727
var body = setBody(
2828
'<div>{{ignor me}}<div ng-bind="\'works\'"></div></div>');
29-
ngBootstrap(element:body.find('div[ng-bind]').first);
30-
expect(body.text()).toEqual('{{ignor me}}works');
29+
ngBootstrap(element:body.querySelector('div[ng-bind]'));
30+
expect(body.text).toEqual('{{ignor me}}works');
3131
});
3232
});
3333
}

test/core/templateurl_spec.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void main() {
7070
microLeap();
7171

7272
expect(element.textWithShadow()).toEqual('.hello{}Simple!');
73-
expect(element.children().shadowRoot().html()).toEqual(
73+
expect(element.children().shadowRoot()[0]).toHaveHtml(
7474
'<style>.hello{}</style><div log="SIMPLE">Simple!</div>'
7575
);
7676
})));
@@ -139,7 +139,7 @@ void main() {
139139
microLeap();
140140

141141
expect(element.textWithShadow()).toEqual('.hello{}Simple!');
142-
expect(element.children().shadowRoot().html()).toEqual(
142+
expect(element.children().shadowRoot()[0]).toHaveHtml(
143143
'<style>.hello{}</style><div log="SIMPLE">Simple!</div>'
144144
);
145145
$rootScope.apply();
@@ -225,7 +225,7 @@ void main() {
225225
microLeap();
226226

227227
expect(element.textWithShadow()).toEqual('.hello{}.world{}Simple!');
228-
expect(element.children().shadowRoot().html()).toEqual(
228+
expect(element.children().shadowRoot()[0]).toHaveHtml(
229229
'<style>.hello{}.world{}</style><div log="SIMPLE">Simple!</div>'
230230
);
231231
$rootScope.apply();

test/core_dom/compiler_spec.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ void main() {
5959
});
6060

6161
it('should compile a comment with no directives around', () {
62-
var element = $(_.compile('<div><!-- comment --></div>'));
63-
expect(element.html()).toEqual('<!-- comment -->');
62+
var element = _.compile('<div><!-- comment --></div>');
63+
expect(element).toHaveHtml('<!-- comment -->');
6464
});
6565

6666
it('should compile a comment when the parent has a directive', () {
67-
var element = $(_.compile('<div ng-show="true"><!-- comment --></div>'));
68-
expect(element.html()).toEqual('<!-- comment -->');
67+
var element = _.compile('<div ng-show="true"><!-- comment --></div>');
68+
expect(element).toHaveHtml('<!-- comment -->');
6969
});
7070

7171
it('should compile a directive in a child', () {
@@ -79,17 +79,17 @@ void main() {
7979
});
8080

8181
it('should compile repeater', () {
82-
var element = $(_.compile('<div><div ng-repeat="item in items" ng-bind="item"></div></div>'));
82+
var element = _.compile('<div><div ng-repeat="item in items" ng-bind="item"></div></div>');
8383

8484
_.rootScope.context['items'] = ['A', 'b'];
85-
expect(element.text()).toEqual('');
85+
expect(element.text).toEqual('');
8686

8787
_.rootScope.apply();
88-
expect(element.text()).toEqual('Ab');
88+
expect(element.text).toEqual('Ab');
8989

9090
_.rootScope.context['items'] = [];
9191
_.rootScope.apply();
92-
expect(element.html()).toEqual('<!--ANCHOR: [ng-repeat]=item in items-->');
92+
expect(element).toHaveHtml('<!--ANCHOR: [ng-repeat]=item in items-->');
9393
});
9494

9595
it('should compile a text child of a basic repeater', () {
@@ -120,17 +120,17 @@ void main() {
120120
});
121121

122122
it('should compile repeater with children', (Compiler $compile) {
123-
var element = $(_.compile('<div><div ng-repeat="item in items"><div ng-bind="item"></div></div></div>'));
123+
var element = _.compile('<div><div ng-repeat="item in items"><div ng-bind="item"></div></div></div>');
124124

125125
_.rootScope.context['items'] = ['A', 'b'];
126126

127-
expect(element.text()).toEqual('');
127+
expect(element.text).toEqual('');
128128
_.rootScope.apply();
129-
expect(element.text()).toEqual('Ab');
129+
expect(element.text).toEqual('Ab');
130130

131131
_.rootScope.context['items'] = [];
132132
_.rootScope.apply();
133-
expect(element.html()).toEqual('<!--ANCHOR: [ng-repeat]=item in items-->');
133+
expect(element).toHaveHtml('<!--ANCHOR: [ng-repeat]=item in items-->');
134134
});
135135

136136
it('should compile text', (Compiler $compile) {

test/core_dom/ng_mustache_spec.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ main() {
5656

5757

5858
it('should handle filters', inject((Compiler $compile, RootScope rootScope, Injector injector, DirectiveMap directives) {
59-
var element = $('<div>{{"World" | hello}}</div>');
59+
var element = es('<div>{{"World" | hello}}</div>');
6060
var template = $compile(element, directives);
6161
var view = template(injector);
6262
rootScope.apply();
6363

64-
element = $(view.nodes);
64+
element = view.nodes;
6565

66-
expect(element.html()).toEqual('Hello, World!');
66+
expect(element).toHaveHtml('Hello, World!');
6767
}));
6868
});
6969

test/core_dom/view_spec.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,23 @@ main() {
8787
it('should insert block after anchor view', () {
8888
anchor.insert(a);
8989

90-
expect($rootElement.html()).toEqual('<!-- anchor --><span>A</span>a');
90+
expect($rootElement[0]).toHaveHtml('<!-- anchor --><span>A</span>a');
9191
});
9292

9393

9494
it('should insert multi element view after another multi element view', () {
9595
anchor.insert(a);
9696
anchor.insert(b, insertAfter: a);
9797

98-
expect($rootElement.html()).toEqual('<!-- anchor --><span>A</span>a<span>B</span>b');
98+
expect($rootElement[0]).toHaveHtml('<!-- anchor --><span>A</span>a<span>B</span>b');
9999
});
100100

101101

102102
it('should insert multi element view before another multi element view', () {
103103
anchor.insert(b);
104104
anchor.insert(a);
105105

106-
expect($rootElement.html()).toEqual('<!-- anchor --><span>A</span>a<span>B</span>b');
106+
expect($rootElement[0]).toHaveHtml('<!-- anchor --><span>A</span>a<span>B</span>b');
107107
});
108108
});
109109

@@ -118,12 +118,12 @@ main() {
118118

119119
it('should remove the last view', () {
120120
anchor.remove(b);
121-
expect($rootElement.html()).toEqual('<!-- anchor --><span>A</span>a');
121+
expect($rootElement[0]).toHaveHtml('<!-- anchor --><span>A</span>a');
122122
});
123123

124124
it('should remove child views from parent pseudo black', () {
125125
anchor.remove(a);
126-
expect($rootElement.html()).toEqual('<!-- anchor --><span>B</span>b');
126+
expect($rootElement[0]).toHaveHtml('<!-- anchor --><span>B</span>b');
127127
});
128128

129129
// TODO(deboer): Make this work again.
@@ -183,7 +183,7 @@ main() {
183183

184184
it('should move last to middle', () {
185185
anchor.move(a, moveAfter: b);
186-
expect($rootElement.html()).toEqual('<!-- anchor --><span>B</span>b<span>A</span>a');
186+
expect($rootElement[0]).toHaveHtml('<!-- anchor --><span>B</span>b<span>A</span>a');
187187
});
188188
});
189189
});

test/directive/ng_bind_html_spec.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ main() {
88

99
it('should sanitize and set innerHtml and sanitize and set html',
1010
(Scope scope, Injector injector, Compiler compiler, DirectiveMap directives) {
11-
var element = $('<div ng-bind-html="htmlVar"></div>');
11+
var element = es('<div ng-bind-html="htmlVar"></div>');
1212
compiler(element, directives)(injector, element);
1313
scope.context['htmlVar'] = '<a href="http://www.google.com"><b>Google!</b></a>';
1414
scope.apply();
1515
// Sanitization removes the href attribute on the <a> tag.
16-
expect(element.html()).toEqual('<a><b>Google!</b></a>');
16+
expect(element).toHaveHtml('<a><b>Google!</b></a>');
1717
});
1818

1919
describe('injected NodeValidator', () {
@@ -27,12 +27,12 @@ main() {
2727
});
2828

2929
it('should use injected NodeValidator and override default sanitize behavior', (Scope scope, Injector injector, Compiler compiler, DirectiveMap directives) {
30-
var element = $('<div ng-bind-html="htmlVar"></div>');
30+
var element = es('<div ng-bind-html="htmlVar"></div>');
3131
compiler(element, directives)(injector, element);
3232
scope.context['htmlVar'] = '<a href="http://www.google.com"><b>Google!</b></a>';
3333
scope.apply();
3434
// Sanitation allows href attributes per injected sanitizer.
35-
expect(element.html()).toEqual('<a href="http://www.google.com"><b>Google!</b></a>');
35+
expect(element).toHaveHtml('<a href="http://www.google.com"><b>Google!</b></a>');
3636
});
3737
});
3838
});

0 commit comments

Comments
 (0)