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

Commit

Permalink
feat(expect): Move JQuery.text to Expect.toHaveText() and element.text
Browse files Browse the repository at this point in the history
For #219
  • Loading branch information
jbdeboer committed Mar 20, 2014
1 parent c71b8cf commit dfe84d8
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 120 deletions.
10 changes: 9 additions & 1 deletion test/_specs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Expect {
toBeNotNull() => unit.expect(actual, unit.isNotNull);

toHaveHtml(expected) => unit.expect(_toHtml(actual), unit.equals(expected));
toHaveText(expected) => unit.expect(_elementText(actual), unit.equals(expected));

toHaveBeenCalled() => unit.expect(actual.called, true, reason: 'method not called');
toHaveBeenCalledOnce() => unit.expect(actual.count, 1, reason: 'method invoked ${actual.count} expected once');
Expand Down Expand Up @@ -117,6 +118,14 @@ class Expect {
}
}

_elementText(node) {
if (node is List) {
var acc = '';
node.forEach((n) { acc += _elementText(n); });
return acc;
}
return node.text;
}
}

class NotExpect {
Expand Down Expand Up @@ -222,7 +231,6 @@ class JQuery extends DelegatingList<Node> {
(n, v) => n.setInnerHtml(v, treeSanitizer: new NullTreeSanitizer()),
html);
val([String text]) => accessor((n) => n.value, (n, v) => n.value = v);
text([String text]) => accessor((n) => n.text, (n, v) => n.text = v, text);
contents() => fold(new JQuery(), (jq, node) => jq..addAll(node.nodes));
eq(num childIndex) => $(this[childIndex]);
remove(_) => forEach((n) => n.remove());
Expand Down
2 changes: 1 addition & 1 deletion test/_specs_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ main() {
it('should return the shadowRoot if one exists', () {
var elts = $('<div></div>');
elts[0].createShadowRoot().innerHtml = "Hello shadow";
expect(elts.shadowRoot().text()).toEqual("Hello shadow");
expect(elts.shadowRoot()[0].text).toEqual("Hello shadow");
});

it('should return empty list if there is no shadowRoot', () {
Expand Down
42 changes: 21 additions & 21 deletions test/core_dom/compiler_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ void main() {
beforeEach(inject((TestBed tb) => _ = tb));

it('should compile basic hello world', () {
var element = $(_.compile('<div ng-bind="name"></div>'));
var element = _.compile('<div ng-bind="name"></div>');

_.rootScope.context['name'] = 'angular';

expect(element.text()).toEqual('');
expect(element.text).toEqual('');
_.rootScope.apply();
expect(element.text()).toEqual('angular');
expect(element.text).toEqual('angular');
});

it('should not throw on an empty list', () {
Expand All @@ -69,13 +69,13 @@ void main() {
});

it('should compile a directive in a child', () {
var element = $(_.compile('<div><div ng-bind="name"></div></div>'));
var element = _.compile('<div><div ng-bind="name"></div></div>');

_.rootScope.context['name'] = 'angular';

expect(element.text()).toEqual('');
expect(element.text).toEqual('');
_.rootScope.apply();
expect(element.text()).toEqual('angular');
expect(element.text).toEqual('angular');
});

it('should compile repeater', () {
Expand All @@ -93,12 +93,12 @@ void main() {
});

it('should compile a text child of a basic repeater', () {
var element = $(_.compile(
var element = _.compile(
'<div ng-show="true">' +
'<span ng-repeat="r in [1, 2]">{{r}}</span>' +
'</div>'));
'</div>');
_.rootScope.apply();
expect(element.text()).toEqual('12');
expect(element.text).toEqual('12');
});

it('should compile a text child of a repeat with a directive', () {
Expand All @@ -109,14 +109,14 @@ void main() {
});

it('should compile a sibling template directive', () {
var element = $(_.compile(
var element = _.compile(
'<div ng-model="selected">'
'<option value="">blank</option>'
'<div ng-repeat="value in [1,2]" ng-value="value">{{value}}</div>'
'</div>'));
'</div>');

_.rootScope.apply();
expect(element.text()).toEqual('blank12');
expect(element.text).toEqual('blank12');
});

it('should compile repeater with children', (Compiler $compile) {
Expand All @@ -134,26 +134,26 @@ void main() {
});

it('should compile text', (Compiler $compile) {
var element = $(_.compile('<div>{{name}}<span>!</span></div>'));
var element = _.compile('<div>{{name}}<span>!</span></div>');
_.rootScope.context['name'] = 'OK';

microLeap();
_.rootScope.apply();
expect(element.text()).toEqual('OK!');
expect(element.text).toEqual('OK!');
});

it('should compile nested repeater', (Compiler $compile) {
var element = $(_.compile(
var element = _.compile(
'<div>' +
'<ul ng-repeat="lis in uls">' +
'<li ng-repeat="li in lis">{{li}}</li>' +
'</ul>' +
'</div>'));
'</div>');

_.rootScope.context['uls'] = [['A'], ['b']];

_.rootScope.apply();
expect(element.text()).toEqual('Ab');
expect(element.text).toEqual('Ab');
});

it('should compile two directives with the same selector', (Logger log) {
Expand Down Expand Up @@ -189,12 +189,12 @@ void main() {
});

it('should interpolate text nodes', () {
var element = $(_.compile('<div>{{name}}</div>'));
var element = _.compile('<div>{{name}}</div>');

_.rootScope.context['name'] = 'angular';

_.rootScope.apply();
expect(element.text()).toEqual('angular');
expect(element.text).toEqual('angular');
});
});

Expand Down Expand Up @@ -445,11 +445,11 @@ void main() {
}));

it('should publish directive controller into the scope', async((NgZone zone) {
var element = $(_.compile(r'<div><div publish-me>{{ctrlName.value}}</div></div>'));
var element = _.compile(r'<div><div publish-me>{{ctrlName.value}}</div></div>');

microLeap();
_.rootScope.apply();
expect(element.text()).toEqual('WORKED');
expect(element.text).toEqual('WORKED');
}));

it('should "publish" controller to injector under provided publishTypes', () {
Expand Down
6 changes: 3 additions & 3 deletions test/core_dom/ng_mustache_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ main() {
beforeEach(inject((TestBed tb) => _ = tb));

it('should replace {{}} in text', inject((Compiler $compile, Scope rootScope, Injector injector, DirectiveMap directives) {
var element = $('<div>{{name}}<span>!</span></div>');
var element = es('<div>{{name}}<span>!</span></div>');
var template = $compile(element, directives);

rootScope.context['name'] = 'OK';
var view = template(injector);

element = $(view.nodes);
element = view.nodes;

rootScope.apply();
expect(element.text()).toEqual('OK!');
expect(element).toHaveText('OK!');
}));

describe('observe/flush phase', () {
Expand Down
8 changes: 4 additions & 4 deletions test/core_dom/view_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ main() {
anchor.insert(a);
anchor.insert(b, insertAfter: a);

expect($rootElement.text()).toEqual('AaBb');
expect($rootElement[0].text).toEqual('AaBb');
});

it('should remove the last view', () {
Expand Down Expand Up @@ -163,11 +163,11 @@ main() {
// view after the <!--start--> element.
outerAnchor.insert(outterBoundViewFactory(null));

expect($rootElement.text()).toEqual('text');
expect($rootElement[0].text).toEqual('text');

anchor.remove(outerView);

expect($rootElement.text()).toEqual('');
expect($rootElement[0].text).toEqual('');
});
});

Expand All @@ -177,7 +177,7 @@ main() {
anchor.insert(a);
anchor.insert(b, insertAfter: a);

expect($rootElement.text()).toEqual('AaBb');
expect($rootElement[0].text).toEqual('AaBb');
});


Expand Down
6 changes: 3 additions & 3 deletions test/directive/ng_bind_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ main() {
beforeEach((TestBed tb) => _ = tb);

it('should set.text', (Scope scope, Injector injector, Compiler compiler, DirectiveMap directives) {
var element = $('<div ng-bind="a"></div>');
compiler(element, directives)(injector, element);
var element = e('<div ng-bind="a"></div>');
compiler([element], directives)(injector, [element]);
scope.context['a'] = "abc123";
scope.apply();
expect(element.text()).toEqual('abc123');
expect(element.text).toEqual('abc123');
});


Expand Down
20 changes: 10 additions & 10 deletions test/directive/ng_non_bindable_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ main() {

it('should set ignore all other markup/directives on the descendent nodes',
(Scope scope, Injector injector, Compiler compiler, DirectiveMap directives) {
var element = $('<div>' +
Element element = e('<div>' +
' <span id="s1">{{a}}</span>' +
' <span id="s2" ng-bind="b"></span>' +
' <div foo="{{a}}" ng-non-bindable>' +
Expand All @@ -19,21 +19,21 @@ main() {
' <span id="s3">{{a}}</span>' +
' <span id="s4" ng-bind="b"></span>' +
'</div>');
compiler(element, directives)(injector, element);
compiler([element], directives)(injector, [element]);
scope.context['a'] = "one";
scope.context['b'] = "two";
scope.apply();
// Bindings not contained by ng-non-bindable should resolve.
expect(element.find("#s1").text().trim()).toEqual('one');
expect(element.find("#s2").text().trim()).toEqual('two');
expect(element.find("#s3").text().trim()).toEqual('one');
expect(element.find("#s4").text().trim()).toEqual('two');
expect(element.querySelector("#s1").text.trim()).toEqual('one');
expect(element.querySelector("#s2").text.trim()).toEqual('two');
expect(element.querySelector("#s3").text.trim()).toEqual('one');
expect(element.querySelector("#s4").text.trim()).toEqual('two');
// Bindings contained by ng-non-bindable should be left alone.
var nonBindableDiv = element.find("div");
expect(nonBindableDiv[0]).toHaveHtml('<span ng-bind="a"></span>{{b}}');
expect(nonBindableDiv.text().trim()).toEqual('{{b}}');
var nonBindableDiv = element.querySelector("div");
expect(nonBindableDiv).toHaveHtml('<span ng-bind="a"></span>{{b}}');
expect(nonBindableDiv.text.trim()).toEqual('{{b}}');
// Bindings on the same node are processed.
expect(nonBindableDiv.attr('foo')).toEqual('one');
expect(nonBindableDiv.attributes['foo']).toEqual('one');
});
});
}
Loading

0 comments on commit dfe84d8

Please sign in to comment.