aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMisko Hevery2010-08-11 11:21:08 -0700
committerMisko Hevery2010-08-11 11:21:08 -0700
commitab2213e80e09c763782bebc5e0ff7509056e828a (patch)
tree59f07f445f71d039400b8e99ebd74bdd87959d14
parent412f05977c41fbda46278b637c4d1a84996d48d1 (diff)
parent04e92a875344fb675f27355a8bae7a22d0a6ae63 (diff)
downloadangular.js-ab2213e80e09c763782bebc5e0ff7509056e828a.tar.bz2
Merge branch 'master' of github.com:angular/angular.js
-rw-r--r--src/scenario/DSL.js58
-rw-r--r--test/scenario/DSLSpec.js86
2 files changed, 89 insertions, 55 deletions
diff --git a/src/scenario/DSL.js b/src/scenario/DSL.js
index 96447aaf..3b049dc6 100644
--- a/src/scenario/DSL.js
+++ b/src/scenario/DSL.js
@@ -43,6 +43,8 @@ angular.scenario.dsl.input = function(selector) {
};
},
+angular.scenario.dsl.NG_BIND_PATTERN =/\{\{[^\}]+\}\}/;
+
angular.scenario.dsl.repeater = function(selector) {
var namePrefix = "repeater '" + selector + "'";
return {
@@ -51,23 +53,30 @@ angular.scenario.dsl.repeater = function(selector) {
done(this.testDocument.find(selector).size());
});
},
- collect: function() {
- return $scenario.addFuture(namePrefix + ' collect', function(done) {
+ collect: function(collectSelector) {
+ return $scenario.addFuture(
+ namePrefix + " collect '" + collectSelector + "'",
+ function(done) {
var self = this;
var doCollect = bind(this, function() {
- var repeaterArray = [];
+ var repeaterArray = [], ngBindPattern;
+ var startIndex = collectSelector.search(
+ angular.scenario.dsl.NG_BIND_PATTERN);
+ if (startIndex >= 0) {
+ ngBindPattern = collectSelector.substring(
+ startIndex + 2, collectSelector.length - 2);
+ collectSelector = '*';
+
+ }
this.testDocument.find(selector).each(function() {
- var element = angular.extend(self.jQuery(this),
- {bindings: [],
- boundTo: function(name) { return this.bindings[name]; }}
- );
- element.find('*').each(function() {
- var bindName = self.jQuery(this).attr('ng:bind');
- if (bindName) {
- element.bindings[bindName] = self.jQuery(this).text();
- }
+ var element = self.jQuery(this);
+ element.find(collectSelector).
+ each(function() {
+ var foundElem = self.jQuery(this);
+ if (foundElem.attr('ng:bind') == ngBindPattern) {
+ repeaterArray.push(foundElem.text());
+ }
});
- repeaterArray[index] = element;
});
return repeaterArray;
});
@@ -80,17 +89,16 @@ angular.scenario.dsl.repeater = function(selector) {
angular.scenario.dsl.element = function(selector) {
var nameSuffix = "element '" + selector + "'";
return $scenario.addFuture('Find ' + nameSuffix, function(done) {
- var self = this;
- var element = angular.extend(this.testDocument.find(selector), {
- bindings: [],
- boundTo: function(name) { return this.bindings[name]; }
- });
- element.find('*').each(function() {
- var bindName = self.jQuery(elem).attr('ng:bind');
- if (bindName) {
- element.bindings[bindName] = self.jQuery(elem).text();
- }
- });
- done(element);
+ var self = this, repeaterArray = [], ngBindPattern;
+ var startIndex = selector.search(angular.scenario.dsl.NG_BIND_PATTERN);
+ if (startIndex >= 0) {
+ ngBindPattern = selector.substring(startIndex + 2, selector.length - 2);
+ var element = this.testDocument.find('*').filter(function() {
+ return self.jQuery(this).attr('ng:bind') == ngBindPattern;
+ });
+ done(element);
+ } else {
+ done(this.testDocument.find(selector));
+ }
});
};
diff --git a/test/scenario/DSLSpec.js b/test/scenario/DSLSpec.js
index 62de9d6c..c8e30917 100644
--- a/test/scenario/DSLSpec.js
+++ b/test/scenario/DSLSpec.js
@@ -42,6 +42,27 @@ describe("DSL", function() {
describe('repeater', function() {
var repeater = angular.scenario.dsl.repeater;
+ var html;
+ beforeEach(function() {
+ html = "<table>" +
+ "<tr class='epic'>" +
+ "<td class='hero-name'>" +
+ "<span ng:bind='hero'>John Marston</span>" +
+ "</td>" +
+ "<td class='game-name'>" +
+ "<span ng:bind='game'>Red Dead Redemption</span>" +
+ "</td>" +
+ "</tr>" +
+ "<tr class='epic'>" +
+ "<td class='hero-name'>" +
+ "<span ng:bind='hero'>Nathan Drake</span>" +
+ "</td>" +
+ "<td class='game-name'>" +
+ "<span ng:bind='game'>Uncharted</span>" +
+ "</td>" +
+ "</tr>" +
+ "</table>";
+ });
it('should count', function() {
var future = repeater('.repeater-row').count();
expect(future.name).toEqual("repeater '.repeater-row' count");
@@ -55,28 +76,32 @@ describe("DSL", function() {
expect(future.value).toEqual(2);
});
- it('should collect', function() {
- var future = repeater('.epic').collect();
- expect(future.name).toEqual("repeater '.epic' collect");
- executeFuture(future,
- "<table>" +
- "<tr class='epic'>" +
- "<td ng:bind='hero'>John Marston</td>" +
- "<td ng:bind='game'>Red Dead Redemption</td>" +
- "</tr>" +
- "<tr class='epic'>" +
- "<td ng:bind='hero'>Nathan Drake</td>" +
- "<td ng:bind='game'>Uncharted 2</td>" +
- "</tr>" +
- "</table>",
- function(value) {
- future.fulfill(value);
+ function assertFutureState(future, expectedName, expectedValue) {
+ expect(future.name).toEqual(expectedName);
+ executeFuture(future, html, function(value) {
+ future.fulfill(value);
});
expect(future.fulfilled).toBeTruthy();
- expect(future.value[0].boundTo('hero')).toEqual('John Marston');
- expect(future.value[0].boundTo('game')).toEqual('Red Dead Redemption');
- expect(future.value[1].boundTo('hero')).toEqual('Nathan Drake');
- expect(future.value[1].boundTo('game')).toEqual('Uncharted 2');
+ expect(future.value).toEqual(expectedValue);
+ }
+ it('should collect bindings', function() {
+ assertFutureState(repeater('.epic').collect('{{hero}}'),
+ "repeater '.epic' collect '{{hero}}'",
+ ['John Marston', 'Nathan Drake']);
+ assertFutureState(repeater('.epic').collect('{{game}}'),
+ "repeater '.epic' collect '{{game}}'",
+ ['Red Dead Redemption', 'Uncharted']);
+ });
+ it('should collect normal selectors', function() {
+ assertFutureState(repeater('.epic').collect('.hero-name'),
+ "repeater '.epic' collect '.hero-name'",
+ ['John Marston', 'Nathan Drake']);
+ assertFutureState(repeater('.epic').collect('.game-name'),
+ "repeater '.epic' collect '.game-name'",
+ ['Red Dead Redemption', 'Uncharted']);
+ });
+ it('should collect normal attributes', function() {
+ //TODO(shyamseshadri) : Left as an exercise to the user
});
});
@@ -95,24 +120,25 @@ describe("DSL", function() {
'</div>' +
'</div>';
});
+ function timeTravel(future) {
+ executeFuture(future, html, function(value) { future.fulfill(value); });
+ expect(future.fulfilled).toBeTruthy();
+ }
it('should find elements on the page and provide jquery api', function() {
var future = element('.reports-detail');
expect(future.name).toEqual("Find element '.reports-detail'");
- executeFuture(future, html, function(value) { future.fulfill(value); });
- expect(future.fulfilled).toBeTruthy();
+ timeTravel(future);
expect(future.value.text()).
toEqual('Description : Details...Date created: 01/01/01');
expect(future.value.find('.desc').text()).
toEqual('Description : Details...');
});
- it('should know how to find ng:bind elements on page', function() {
- var future = element('.reports-detail');
- expect(future.name).toEqual("Find element '.reports-detail'");
- executeFuture(future, html, function(value) { future.fulfill(value); });
- expect(future.fulfilled).toBeTruthy();
- expect(future.value.boundTo('report.description')).toEqual('Details...');
- expect(future.value.boundTo('report.creationDate')).toEqual('01/01/01');
- expect(future.value.boundTo('doesnotexist')).not.toBeDefined();
+ it('should find elements with angular syntax', function() {
+ var future = element('{{report.description}}');
+ expect(future.name).toEqual("Find element '{{report.description}}'");
+ timeTravel(future);
+ expect(future.value.text()).toEqual('Details...');
+ expect(future.value.attr('ng:bind')).toEqual('report.description');
});
});
});