mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
Change repeater dsl to collect and return an array of string contents based on match
This commit is contained in:
parent
49ffab3318
commit
27784b6dec
2 changed files with 67 additions and 35 deletions
|
|
@ -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;
|
||||
});
|
||||
|
|
@ -86,9 +95,9 @@ angular.scenario.dsl.element = function(selector) {
|
|||
boundTo: function(name) { return this.bindings[name]; }
|
||||
});
|
||||
element.find('*').each(function() {
|
||||
var bindName = self.jQuery(elem).attr('ng:bind');
|
||||
var bindName = self.jQuery(this).attr('ng:bind');
|
||||
if (bindName) {
|
||||
element.bindings[bindName] = self.jQuery(elem).text();
|
||||
element.bindings[bindName] = self.jQuery(this).text();
|
||||
}
|
||||
});
|
||||
done(element);
|
||||
|
|
|
|||
|
|
@ -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,29 +76,31 @@ 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() {});
|
||||
});
|
||||
|
||||
describe('element', function() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue