/
collection.js
153 lines (142 loc) · 3.51 KB
/
collection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import Ember from 'ember';
import { buildSelector } from './helpers';
import { create } from './create';
import { count } from '../page-object';
function merge(target, ...objects) {
objects.forEach(o => Ember.merge(target, o));
return target;
}
function generateEnumerable(definition) {
var enumerable = merge({}, definition);
delete enumerable.itemScope;
if (typeof enumerable.count === 'undefined') {
enumerable.count = count(definition.itemScope)
}
return create(enumerable, { parent: this });
}
function generateItem(index, definition) {
var filters = merge({}, { scope: definition.scope, at: index });
var scope = buildSelector({}, definition.itemScope, filters);
return create(merge({}, definition.item, { scope, resetScope: definition.resetScope }), { parent: this });
}
/**
* Creates a component that represents a collection of items. The collection is zero-indexed.
*
* Collections have a `count` property that returns the number of elements in the collection.
*
* The collection returned by the collection method behaves as a regular PageObject when called without an index.
*
* When called with an index, the method returns the matching item.
*
* @example
*
* // <table>
* // <tbody>
* // <tr>
* // <td>Mary<td>
* // <td>Watson</td>
* // </tr>
* // <tr>
* // <td>John<td>
* // <td>Doe</td>
* // </tr>
* // </tbody>
* // </table>
*
* const page = PageObject.create({
* users: collection({
* itemScope: 'table tr',
*
* item: {
* firstName: text('td', { at: 0 }),
* lastName: text('td', { at: 1 })
* }
* })
* });
*
* assert.equal(page.users().count, 2);
* assert.equal(page.users(1).firstName, 'John');
* assert.equal(page.users(1).lastName, 'Doe');
*
* @example
*
* // <div class="admins">
* // <table>
* // <tbody>
* // <tr>
* // <td>Mary<td>
* // <td>Watson</td>
* // </tr>
* // <tr>
* // <td>John<td>
* // <td>Doe</td>
* // </tr>
* // </tbody>
* // </table>
* // </div>
*
* // <div class="normal">
* // <table>
* // </table>
* // </div>
*
* const page = PageObject.create({
* users: collection({
* scope: '.admins',
*
* itemScope: 'table tr',
*
* item: {
* firstName: text('td', { at: 0 }),
* lastName: text('td', { at: 1 })
* }
* })
* });
*
* assert.equal(page.users().count, 2);
*
* @example
*
* // <table>
* // <caption>User Index</caption>
* // <tbody>
* // <tr>
* // <td>Doe</td>
* // </tr>
* // </tbody>
* // </table>
*
* const page = PageObject.create({
* users: PageObject.collection({
* scope: 'table',
* itemScope: 'tr',
*
* item: {
* firstName: text('td', { at: 0 })
* },
*
* caption: PageObject.text('caption')
* })
* });
*
* assert.equal(page.users().caption, 'User Index');
*
* @param {Object} definition - Collection definition
* @param {string} definition.scope - Nests provided scope within parent's scope
* @param {boolean} definition.resetScope - Override parent's scope
* @param {String} definition.itemScope - CSS selector
* @param {Object} definition.item - Item definition
* @return {Descriptor}
*/
export function collection(definition) {
return {
isDescriptor: true,
value(index) {
if (typeof index === 'number') {
return generateItem.call(this, index, definition);
} else {
return generateEnumerable.call(this, definition);
}
}
};
}