Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 27784b6

Browse files
Shyam Seshadrishyamseshadri
Shyam Seshadri
authored andcommitted
Change repeater dsl to collect and return an array of string contents based on match
1 parent 49ffab3 commit 27784b6

File tree

2 files changed

+67
-35
lines changed

2 files changed

+67
-35
lines changed

src/scenario/DSL.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ angular.scenario.dsl.input = function(selector) {
4343
};
4444
},
4545

46+
angular.scenario.dsl.NG_BIND_PATTERN =/\{\{[^\}]+\}\}/;
47+
4648
angular.scenario.dsl.repeater = function(selector) {
4749
var namePrefix = "repeater '" + selector + "'";
4850
return {
@@ -51,23 +53,30 @@ angular.scenario.dsl.repeater = function(selector) {
5153
done(this.testDocument.find(selector).size());
5254
});
5355
},
54-
collect: function() {
55-
return $scenario.addFuture(namePrefix + ' collect', function(done) {
56+
collect: function(collectSelector) {
57+
return $scenario.addFuture(
58+
namePrefix + " collect '" + collectSelector + "'",
59+
function(done) {
5660
var self = this;
5761
var doCollect = bind(this, function() {
58-
var repeaterArray = [];
62+
var repeaterArray = [], ngBindPattern;
63+
var startIndex = collectSelector.search(
64+
angular.scenario.dsl.NG_BIND_PATTERN);
65+
if (startIndex >= 0) {
66+
ngBindPattern = collectSelector.substring(
67+
startIndex + 2, collectSelector.length - 2);
68+
collectSelector = '*';
69+
70+
}
5971
this.testDocument.find(selector).each(function() {
60-
var element = angular.extend(self.jQuery(this),
61-
{bindings: [],
62-
boundTo: function(name) { return this.bindings[name]; }}
63-
);
64-
element.find('*').each(function() {
65-
var bindName = self.jQuery(this).attr('ng:bind');
66-
if (bindName) {
67-
element.bindings[bindName] = self.jQuery(this).text();
68-
}
72+
var element = self.jQuery(this);
73+
element.find(collectSelector).
74+
each(function() {
75+
var foundElem = self.jQuery(this);
76+
if (foundElem.attr('ng:bind') == ngBindPattern) {
77+
repeaterArray.push(foundElem.text());
78+
}
6979
});
70-
repeaterArray[index] = element;
7180
});
7281
return repeaterArray;
7382
});
@@ -86,9 +95,9 @@ angular.scenario.dsl.element = function(selector) {
8695
boundTo: function(name) { return this.bindings[name]; }
8796
});
8897
element.find('*').each(function() {
89-
var bindName = self.jQuery(elem).attr('ng:bind');
98+
var bindName = self.jQuery(this).attr('ng:bind');
9099
if (bindName) {
91-
element.bindings[bindName] = self.jQuery(elem).text();
100+
element.bindings[bindName] = self.jQuery(this).text();
92101
}
93102
});
94103
done(element);

test/scenario/DSLSpec.js

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ describe("DSL", function() {
4242
describe('repeater', function() {
4343

4444
var repeater = angular.scenario.dsl.repeater;
45+
var html;
46+
beforeEach(function() {
47+
html = "<table>" +
48+
"<tr class='epic'>" +
49+
"<td class='hero-name'>" +
50+
"<span ng:bind='hero'>John Marston</span>" +
51+
"</td>" +
52+
"<td class='game-name'>" +
53+
"<span ng:bind='game'>Red Dead Redemption</span>" +
54+
"</td>" +
55+
"</tr>" +
56+
"<tr class='epic'>" +
57+
"<td class='hero-name'>" +
58+
"<span ng:bind='hero'>Nathan Drake</span>" +
59+
"</td>" +
60+
"<td class='game-name'>" +
61+
"<span ng:bind='game'>Uncharted</span>" +
62+
"</td>" +
63+
"</tr>" +
64+
"</table>";
65+
});
4566
it('should count', function() {
4667
var future = repeater('.repeater-row').count();
4768
expect(future.name).toEqual("repeater '.repeater-row' count");
@@ -55,29 +76,31 @@ describe("DSL", function() {
5576
expect(future.value).toEqual(2);
5677
});
5778

58-
it('should collect', function() {
59-
var future = repeater('.epic').collect();
60-
expect(future.name).toEqual("repeater '.epic' collect");
61-
executeFuture(future,
62-
"<table>" +
63-
"<tr class='epic'>" +
64-
"<td ng:bind='hero'>John Marston</td>" +
65-
"<td ng:bind='game'>Red Dead Redemption</td>" +
66-
"</tr>" +
67-
"<tr class='epic'>" +
68-
"<td ng:bind='hero'>Nathan Drake</td>" +
69-
"<td ng:bind='game'>Uncharted 2</td>" +
70-
"</tr>" +
71-
"</table>",
72-
function(value) {
73-
future.fulfill(value);
79+
function assertFutureState(future, expectedName, expectedValue) {
80+
expect(future.name).toEqual(expectedName);
81+
executeFuture(future, html, function(value) {
82+
future.fulfill(value);
7483
});
7584
expect(future.fulfilled).toBeTruthy();
76-
expect(future.value[0].boundTo('hero')).toEqual('John Marston');
77-
expect(future.value[0].boundTo('game')).toEqual('Red Dead Redemption');
78-
expect(future.value[1].boundTo('hero')).toEqual('Nathan Drake');
79-
expect(future.value[1].boundTo('game')).toEqual('Uncharted 2');
85+
expect(future.value).toEqual(expectedValue);
86+
}
87+
it('should collect bindings', function() {
88+
assertFutureState(repeater('.epic').collect('{{hero}}'),
89+
"repeater '.epic' collect '{{hero}}'",
90+
['John Marston', 'Nathan Drake']);
91+
assertFutureState(repeater('.epic').collect('{{game}}'),
92+
"repeater '.epic' collect '{{game}}'",
93+
['Red Dead Redemption', 'Uncharted']);
94+
});
95+
it('should collect normal selectors', function() {
96+
assertFutureState(repeater('.epic').collect('.hero-name'),
97+
"repeater '.epic' collect '.hero-name'",
98+
['John Marston', 'Nathan Drake']);
99+
assertFutureState(repeater('.epic').collect('.game-name'),
100+
"repeater '.epic' collect '.game-name'",
101+
['Red Dead Redemption', 'Uncharted']);
80102
});
103+
it('should collect normal attributes', function() {});
81104
});
82105

83106
describe('element', function() {

0 commit comments

Comments
 (0)