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

Commit 0384346

Browse files
committed
feat(expect): toHaveText handles shadow DOM. Deprecates JQuery.textWithShadow
1 parent 0873eac commit 0384346

File tree

4 files changed

+55
-65
lines changed

4 files changed

+55
-65
lines changed

test/_specs.dart

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,22 @@ class Expect {
118118
}
119119
}
120120

121-
_elementText(node) {
122-
if (node is List) {
123-
var acc = '';
124-
node.forEach((n) { acc += _elementText(n); });
125-
return acc;
121+
_elementText(n, [bool notShadow = false]) {
122+
if (n is List) {
123+
return n.map((nn) => _elementText(nn)).join("");
124+
}
125+
126+
if (n is Comment) return '';
127+
128+
if (!notShadow && n is Element && n.shadowRoot != null) {
129+
var shadowText = n.shadowRoot.text;
130+
var domText = _elementText(n, true);
131+
return shadowText.replaceFirst("SHADOW-CONTENT", domText);
126132
}
127-
return node.text;
133+
134+
if (n.nodes == null || n.nodes.length == 0) return n.text;
135+
136+
return n.nodes.map((cn) => _elementText(cn)).join("");
128137
}
129138
}
130139

@@ -194,24 +203,6 @@ class JQuery extends DelegatingList<Node> {
194203
}
195204
}
196205

197-
_renderedText(n, [bool notShadow = false]) {
198-
if (n is List) {
199-
return n.map((nn) => _renderedText(nn)).join("");
200-
}
201-
202-
if (n is Comment) return '';
203-
204-
if (!notShadow && n is Element && n.shadowRoot != null) {
205-
var shadowText = n.shadowRoot.text;
206-
var domText = _renderedText(n, true);
207-
return shadowText.replaceFirst("SHADOW-CONTENT", domText);
208-
}
209-
210-
if (n.nodes == null || n.nodes.length == 0) return n.text;
211-
212-
return n.nodes.map((cn) => _renderedText(cn)).join("");
213-
}
214-
215206
accessor(Function getter, Function setter, [value, single=false]) {
216207
// TODO(dart): ?value does not work, since value was passed. :-(
217208
var setterMode = value != null;
@@ -244,7 +235,6 @@ class JQuery extends DelegatingList<Node> {
244235
(n, v) => getterSetter.setter(name)(n, v),
245236
null,
246237
true);
247-
textWithShadow() => fold('', (t, n) => '${t}${_renderedText(n)}');
248238
find(selector) => fold(new JQuery(), (jq, n) => jq..addAll(
249239
(n is Element ? (n as Element).querySelectorAll(selector) : [])));
250240
hasClass(String name) => fold(false, (hasClass, node) =>

test/_specs_spec.dart

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ main() {
1515
expect(div).toHaveHtml('<span></span>');
1616
});
1717
});
18+
19+
describe('toHaveText', () {
20+
it('should work on regular DOM nodes', () {
21+
expect(es('<span>A<span>C</span></span><span>B</span>')).toHaveText('ACB');
22+
});
23+
24+
it('should work with shadow DOM', () {
25+
var elt = e('<div>DOM content</div>');
26+
var shadow = elt.createShadowRoot();
27+
shadow.setInnerHtml(
28+
'<div>Shadow content</div><content>SHADOW-CONTENT</content>',
29+
treeSanitizer: new NullTreeSanitizer());
30+
expect(elt).toHaveText('Shadow contentDOM content');
31+
});
32+
33+
it('should ignore comments', () {
34+
expect(es('<!--e--><span>A<span>C</span></span><span>B</span>')).toHaveText('ACB');
35+
});
36+
});
1837
});
1938

2039
describe('jquery', () {
@@ -35,24 +54,5 @@ main() {
3554
expect(elts.shadowRoot()[0]).toHaveHtml('<div>Hello shadow</div>');
3655
});
3756
});
38-
39-
describe('textWithShadow', () {
40-
it('should work on regular DOM nodes', () {
41-
expect($('<span>A<span>C</span></span><span>B</span>').textWithShadow()).toEqual('ACB');
42-
});
43-
44-
it('should work with shadow DOM', () {
45-
var elt = $('<div>DOM content</div>');
46-
var shadow = elt[0].createShadowRoot();
47-
shadow.setInnerHtml(
48-
'<div>Shadow content</div><content>SHADOW-CONTENT</content>',
49-
treeSanitizer: new NullTreeSanitizer());
50-
expect(elt.textWithShadow()).toEqual('Shadow contentDOM content');
51-
});
52-
53-
it('should ignore comments', () {
54-
expect($('<!--e--><span>A<span>C</span></span><span>B</span>').textWithShadow()).toEqual('ACB');
55-
});
56-
});
5757
});
5858
}

test/core/templateurl_spec.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void main() {
6969
backend.flush();
7070
microLeap();
7171

72-
expect(element.textWithShadow()).toEqual('.hello{}Simple!');
72+
expect(element[0]).toHaveText('.hello{}Simple!');
7373
expect(element.children().shadowRoot()[0]).toHaveHtml(
7474
'<style>.hello{}</style><div log="SIMPLE">Simple!</div>'
7575
);
@@ -98,7 +98,7 @@ void main() {
9898
backend.flush();
9999
microLeap();
100100

101-
expect(element.textWithShadow()).toEqual('Simple!');
101+
expect(element[0]).toHaveText('Simple!');
102102
$rootScope.apply();
103103
// Note: There is no ordering. It is who ever comes off the wire first!
104104
expect(log.result()).toEqual('LOG; SIMPLE');
@@ -119,7 +119,7 @@ void main() {
119119
backend.flush();
120120
microLeap();
121121

122-
expect(element.textWithShadow()).toEqual('Simple!Simple!');
122+
expect(element[0]).toHaveText('Simple!Simple!');
123123
$rootScope.apply();
124124
// Note: There is no ordering. It is who ever comes off the wire first!
125125
expect(log.result()).toEqual('LOG; LOG; SIMPLE; SIMPLE');
@@ -138,7 +138,7 @@ void main() {
138138
backend.flush();
139139
microLeap();
140140

141-
expect(element.textWithShadow()).toEqual('.hello{}Simple!');
141+
expect(element[0]).toHaveText('.hello{}Simple!');
142142
expect(element.children().shadowRoot()[0]).toHaveHtml(
143143
'<style>.hello{}</style><div log="SIMPLE">Simple!</div>'
144144
);
@@ -156,7 +156,7 @@ void main() {
156156

157157
backend.flush();
158158
microLeap();
159-
expect(element.textWithShadow()).toEqual('.hello{}inline!');
159+
expect(element[0]).toHaveText('.hello{}inline!');
160160
})));
161161

162162
it('should ignore CSS load errors ', async(inject(
@@ -168,7 +168,7 @@ void main() {
168168

169169
backend.flush();
170170
microLeap();
171-
expect(element.textWithShadow()).toEqual(
171+
expect(element[0]).toHaveText(
172172
'/*\n'
173173
'HTTP 500: some error\n'
174174
'*/\n'
@@ -184,7 +184,7 @@ void main() {
184184

185185
backend.flush();
186186
microLeap();
187-
expect(element.textWithShadow()).toEqual('.hello{}');
187+
expect(element[0]).toHaveText('.hello{}');
188188
})));
189189

190190
it('should load the CSS before the template is loaded', async(inject(
@@ -199,7 +199,7 @@ void main() {
199199

200200
backend.flush();
201201
microLeap();
202-
expect(element.textWithShadow()).toEqual('.hello{}Simple!');
202+
expect(element[0]).toHaveText('.hello{}Simple!');
203203
})));
204204
});
205205

@@ -224,7 +224,7 @@ void main() {
224224
backend.flush();
225225
microLeap();
226226

227-
expect(element.textWithShadow()).toEqual('.hello{}.world{}Simple!');
227+
expect(element[0]).toHaveText('.hello{}.world{}Simple!');
228228
expect(element.children().shadowRoot()[0]).toHaveHtml(
229229
'<style>.hello{}.world{}</style><div log="SIMPLE">Simple!</div>'
230230
);

test/core_dom/compiler_spec.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ void main() {
222222
});
223223

224224
it('should select on element', async((NgZone zone) {
225-
var element = $(_.compile(r'<div><simple></simple></div>'));
225+
var element = _.compile(r'<div><simple></simple></div>');
226226
microLeap();
227227
_.rootScope.apply();
228-
expect(element.textWithShadow()).toEqual('INNER()');
228+
expect(element).toHaveText('INNER()');
229229
}));
230230

231231
it('should store ElementProbe with Elements', async(() {
@@ -245,37 +245,37 @@ void main() {
245245

246246
it('should create a simple component', async((NgZone zone) {
247247
_.rootScope.context['name'] = 'OUTTER';
248-
var element = $(_.compile(r'<div>{{name}}:<simple>{{name}}</simple></div>'));
248+
var element = _.compile(r'<div>{{name}}:<simple>{{name}}</simple></div>');
249249
microLeap();
250250
_.rootScope.apply();
251-
expect(element.textWithShadow()).toEqual('OUTTER:INNER(OUTTER)');
251+
expect(element).toHaveText('OUTTER:INNER(OUTTER)');
252252
}));
253253

254254
it('should create a component that can access parent scope', async((NgZone zone) {
255255
_.rootScope.context['fromParent'] = "should not be used";
256256
_.rootScope.context['val'] = "poof";
257-
var element = $(_.compile('<parent-expression from-parent=val></parent-expression>'));
257+
var element = _.compile('<parent-expression from-parent=val></parent-expression>');
258258

259259
microLeap();
260260
_.rootScope.apply();
261-
expect(element.textWithShadow()).toEqual('inside poof');
261+
expect(element).toHaveText('inside poof');
262262
}));
263263

264264
it('should behave nicely if a mapped attribute is missing', async((NgZone zone) {
265-
var element = $(_.compile('<parent-expression></parent-expression>'));
265+
var element = _.compile('<parent-expression></parent-expression>');
266266

267267
microLeap();
268268
_.rootScope.apply();
269-
expect(element.textWithShadow()).toEqual('inside ');
269+
expect(element).toHaveText('inside ');
270270
}));
271271

272272
it('should behave nicely if a mapped attribute evals to null', async((NgZone zone) {
273273
_.rootScope.context['val'] = null;
274-
var element = $(_.compile('<parent-expression fromParent=val></parent-expression>'));
274+
var element = _.compile('<parent-expression fromParent=val></parent-expression>');
275275

276276
microLeap();
277277
_.rootScope.apply();
278-
expect(element.textWithShadow()).toEqual('inside ');
278+
expect(element).toHaveText('inside ');
279279
}));
280280

281281
it('should create a component with I/O', async(() {
@@ -441,7 +441,7 @@ void main() {
441441
var element = $(_.compile(r'<div><publish-me></publish-me></div>'));
442442
microLeap();
443443
_.rootScope.apply();
444-
expect(element.textWithShadow()).toEqual('WORKED');
444+
expect(element).toHaveText('WORKED');
445445
}));
446446

447447
it('should publish directive controller into the scope', async((NgZone zone) {
@@ -506,7 +506,7 @@ void main() {
506506

507507
scope.destroy();
508508
expect(logger).toEqual(['detach']);
509-
expect(element.textWithShadow()).toEqual('WORKED');
509+
expect(element).toHaveText('WORKED');
510510
}));
511511

512512
it('should should not call attach after scope is destroyed', async((Compiler $compile, Logger logger, MockHttpBackend backend) {

0 commit comments

Comments
 (0)