Skip to content
This repository was archived by the owner on Jan 3, 2019. It is now read-only.

Commit cd77b49

Browse files
authored
feat(all): move effector from component to web component (#215)
BREAKING CHANGE: Moved the `getNodeCount` method from the `Component` class to the `WebComponent` class.
1 parent 3f74612 commit cd77b49

File tree

17 files changed

+145
-150
lines changed

17 files changed

+145
-150
lines changed

@pageobject/base/src/Component.test.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class DIV extends Component<HTMLElement, TestAdapter> {
2121
public getID(): Effect<string> {
2222
return async () => (await this.findUniqueNode()).id;
2323
}
24+
25+
public getNodeCount(): Effect<number> {
26+
return async () => (await this.findNodes()).length;
27+
}
2428
}
2529

2630
class Unselectable extends Component<HTMLElement, TestAdapter> {
@@ -100,27 +104,27 @@ const filterNotUnique = divs.where(div => div.divs.getID(), matches(/./));
100104

101105
describe('Component', () => {
102106
describe('at()', () => {
103-
it('should throw an illegal-argument error', () => {
107+
it('should throw a position-base error', () => {
104108
expect(() => divs.at(0)).toThrow(
105-
'The specified position (0) must be one-based'
109+
'Position (0) of <DIV> component must be one-based'
106110
);
107111
});
108112

109-
it('should throw an illegal-state error', () => {
113+
it('should throw a position-already-set error', () => {
110114
expect(() => divs.at(1).at(2)).toThrow(
111-
'The existing position (1) of this <DIV> component cannot be overwritten with 2'
115+
'Position (1) of <DIV> component cannot be overwritten with 2'
112116
);
113117
});
114118
});
115119

116120
describe('findUniqueNode()', () => {
117-
it('should throw an illegal-argument error', async () => {
121+
it('should throw a no-selector error', async () => {
118122
await expect(unselectable.findUniqueNode()).rejects.toThrow(
119-
'The specified <Unselectable> component has no selector'
123+
'<Unselectable> component has no selector'
120124
);
121125
});
122126

123-
it('should return an unique node', async () => {
127+
it('should return a unique node', async () => {
124128
for (const a1 of a1List) {
125129
expect((await a1.findUniqueNode()).id).toBe('a1');
126130
}
@@ -139,7 +143,7 @@ describe('Component', () => {
139143
});
140144

141145
it('should throw a component-not-found error', async () => {
142-
const message = 'The searched <DIV> component cannot be found';
146+
const message = '<DIV> component cannot be found';
143147

144148
for (const notFound of notFoundList) {
145149
await expect(notFound.findUniqueNode()).rejects.toThrow(message);
@@ -150,8 +154,7 @@ describe('Component', () => {
150154
});
151155

152156
it('should throw a component-not-unique error', async () => {
153-
const message =
154-
'The searched <DIV> component cannot be uniquely determined';
157+
const message = '<DIV> component cannot be uniquely determined';
155158

156159
for (const notUnique of notUniqueList) {
157160
await expect(notUnique.findUniqueNode()).rejects.toThrow(message);
@@ -162,56 +165,55 @@ describe('Component', () => {
162165
});
163166
});
164167

165-
describe('getNodeCount() => Effect()', () => {
166-
it('should throw an illegal-argument error', async () => {
167-
await expect(unselectable.getNodeCount()()).rejects.toThrow(
168-
'The specified <Unselectable> component has no selector'
168+
describe('findNodes()', () => {
169+
it('should throw a no-selector error', async () => {
170+
await expect(unselectable.findNodes()).rejects.toThrow(
171+
'<Unselectable> component has no selector'
169172
);
170173
});
171174

172-
it('should return 1', async () => {
175+
it('should return an array with one node', async () => {
173176
for (const a1 of a1List) {
174-
await expect(a1.getNodeCount()()).resolves.toBe(1);
177+
expect((await a1.findNodes()).length).toBe(1);
175178
}
176179

177180
for (const a2 of a2List) {
178-
await expect(a2.getNodeCount()()).resolves.toBe(1);
181+
expect((await a2.findNodes()).length).toBe(1);
179182
}
180183

181184
for (const b1 of b1List) {
182-
await expect(b1.getNodeCount()()).resolves.toBe(1);
185+
expect((await b1.findNodes()).length).toBe(1);
183186
}
184187

185188
for (const b2 of b2List) {
186-
await expect(b2.getNodeCount()()).resolves.toBe(1);
189+
expect((await b2.findNodes()).length).toBe(1);
187190
}
188191
});
189192

190-
it('should return 0', async () => {
193+
it('should return an empty array', async () => {
191194
for (const notFound of notFoundList) {
192-
await expect(notFound.getNodeCount()()).resolves.toBe(0);
195+
expect((await notFound.findNodes()).length).toBe(0);
193196
}
194197
});
195198

196-
it('should return a number greater than 1', async () => {
199+
it('should return an array with more than one node', async () => {
197200
for (const notUnique of notUniqueList) {
198-
await expect(notUnique.getNodeCount()()).resolves.toBeGreaterThan(1);
201+
expect((await notUnique.findNodes()).length).toBeGreaterThan(1);
199202
}
200203
});
201204

202205
it('should throw a component-not-found error', async () => {
203-
const message = 'The searched <DIV> component cannot be found';
206+
const message = '<DIV> component cannot be found';
204207

205-
await expect(ancestorNotFound.getNodeCount()()).rejects.toThrow(message);
206-
await expect(filterNotFound.getNodeCount()()).rejects.toThrow(message);
208+
await expect(ancestorNotFound.findNodes()).rejects.toThrow(message);
209+
await expect(filterNotFound.findNodes()).rejects.toThrow(message);
207210
});
208211

209212
it('should throw a component-not-unique error', async () => {
210-
const message =
211-
'The searched <DIV> component cannot be uniquely determined';
213+
const message = '<DIV> component cannot be uniquely determined';
212214

213-
await expect(ancestorNotUnique.getNodeCount()()).rejects.toThrow(message);
214-
await expect(filterNotUnique.getNodeCount()()).rejects.toThrow(message);
215+
await expect(ancestorNotUnique.findNodes()).rejects.toThrow(message);
216+
await expect(filterNotUnique.findNodes()).rejects.toThrow(message);
215217
});
216218
});
217219
});

@pageobject/base/src/Component.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ export abstract class Component<TNode, TAdapter extends Adapter<TNode>> {
2222
}
2323

2424
public at(position: number): this {
25+
const name = this.toString();
26+
2527
if (position < 1) {
26-
throw new Error(`The specified position (${position}) must be one-based`);
28+
throw new Error(
29+
`Position (${position}) of ${name} component must be one-based`
30+
);
2731
}
2832

2933
if (this._position) {
3034
throw new Error(
31-
`The existing position (${
35+
`Position (${
3236
this._position
33-
}) of this ${this.toString()} component cannot be overwritten with ${position}`
37+
}) of ${name} component cannot be overwritten with ${position}`
3438
);
3539
}
3640

@@ -63,9 +67,7 @@ export abstract class Component<TNode, TAdapter extends Adapter<TNode>> {
6367
}
6468

6569
if (!this.selector) {
66-
throw new Error(
67-
`The specified ${this.toString()} component has no selector`
68-
);
70+
throw new Error(`${this.toString()} component has no selector`);
6971
}
7072

7173
let nodes = await this.adapter.findNodes(
@@ -100,26 +102,19 @@ export abstract class Component<TNode, TAdapter extends Adapter<TNode>> {
100102

101103
public async findUniqueNode(): Promise<TNode> {
102104
const nodes = await this.findNodes();
105+
const name = this.toString();
103106

104107
if (nodes.length === 0) {
105-
throw new Error(
106-
`The searched ${this.toString()} component cannot be found`
107-
);
108+
throw new Error(`${name} component cannot be found`);
108109
}
109110

110111
if (nodes.length > 1) {
111-
throw new Error(
112-
`The searched ${this.toString()} component cannot be uniquely determined`
113-
);
112+
throw new Error(`${name} component cannot be uniquely determined`);
114113
}
115114

116115
return nodes[0];
117116
}
118117

119-
public getNodeCount(): Effect<number> {
120-
return async () => (await this.findNodes()).length;
121-
}
122-
123118
public toString(): string {
124119
return `<${this.constructor.name}>`;
125120
}

@pageobject/web/src/WebAdapter.ts renamed to @pageobject/web/src/WebAdapterTest.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,7 @@
1-
import {Adapter} from '@pageobject/base';
21
import {ok, strictEqual} from 'assert';
32
import {join} from 'path';
43
import {WebAdapter} from '.';
54

6-
export type Argument = any; // tslint:disable-line no-any
7-
8-
export interface WebNode {
9-
click(): Promise<void>;
10-
doubleClick(): Promise<void>;
11-
12-
execute<THTMLElement extends HTMLElement, TResult>(
13-
script: (element: THTMLElement, ...args: Argument[]) => TResult,
14-
...args: Argument[]
15-
): Promise<TResult>;
16-
}
17-
18-
export type Character = string;
19-
export type Key = 'Enter' | 'Escape' | 'Tab';
20-
21-
export interface WebAdapter extends Adapter<WebNode> {
22-
execute<TResult>(
23-
script: (...args: Argument[]) => TResult,
24-
...args: Argument[]
25-
): Promise<TResult>;
26-
27-
goto(url: string): Promise<void>;
28-
press(key: Key | Character): Promise<void>;
29-
quit(): Promise<void>;
30-
}
31-
325
const fileURL = `file://${join(__dirname, '../fixtures/index.html')}`;
336

347
export class WebAdapterTest {

@pageobject/web/src/WebComponent.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,24 @@ describe('WebComponent', () => {
186186
});
187187
});
188188

189+
describe('getNodeCount() => Effect()', () => {
190+
it('should return 0', async () => {
191+
adapter.findNodes.mockImplementation(async () => []);
192+
193+
await expect(component.getNodeCount()()).resolves.toBe(0);
194+
});
195+
196+
it('should return 1', async () => {
197+
await expect(component.getNodeCount()()).resolves.toBe(1);
198+
});
199+
200+
it('should return 2', async () => {
201+
adapter.findNodes.mockImplementation(async () => [node, node]);
202+
203+
await expect(component.getNodeCount()()).resolves.toBe(2);
204+
});
205+
});
206+
189207
describe('getText() => Effect()', () => {
190208
it('should return the text of this component', async () => {
191209
await expect(component.getText()()).resolves.toBe('text');

@pageobject/web/src/WebComponent.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
1-
import {Component, Effect} from '@pageobject/base';
2-
import {Key, WebAdapter, WebNode} from '.';
1+
import {Adapter, Component, Effect} from '@pageobject/base';
2+
3+
export type Argument = any; // tslint:disable-line no-any
4+
5+
export interface WebNode {
6+
click(): Promise<void>;
7+
doubleClick(): Promise<void>;
8+
9+
execute<THTMLElement extends HTMLElement, TResult>(
10+
script: (element: THTMLElement, ...args: Argument[]) => TResult,
11+
...args: Argument[]
12+
): Promise<TResult>;
13+
}
14+
15+
export type Character = string;
16+
export type Key = 'Enter' | 'Escape' | 'Tab';
17+
18+
export interface WebAdapter extends Adapter<WebNode> {
19+
execute<TResult>(
20+
script: (...args: Argument[]) => TResult,
21+
...args: Argument[]
22+
): Promise<TResult>;
23+
24+
goto(url: string): Promise<void>;
25+
press(key: Key | Character): Promise<void>;
26+
quit(): Promise<void>;
27+
}
328

429
export class Keyboard {
530
public readonly adapter: WebAdapter;
@@ -60,6 +85,10 @@ export abstract class WebComponent extends Component<WebNode, WebAdapter> {
6085
return async () => (await this.findUniqueNode()).doubleClick();
6186
}
6287

88+
public getNodeCount(): Effect<number> {
89+
return async () => (await this.findNodes()).length;
90+
}
91+
6392
/**
6493
* @returns The "rendered" text content of this component and its descendants.
6594
*/

@pageobject/web/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './JSDOMAdapter';
2-
export * from './WebAdapter';
2+
export * from './WebAdapterTest';
33
export * from './WebComponent';

docs/api/base/assets/js/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api/base/classes/component.html

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,6 @@
934934
<li class=" tsd-kind-method tsd-parent-kind-class">
935935
<a href="component.html#finduniquenode" class="tsd-kind-icon">find<wbr>Unique<wbr>Node</a>
936936
</li>
937-
<li class=" tsd-kind-method tsd-parent-kind-class">
938-
<a href="component.html#getnodecount" class="tsd-kind-icon">get<wbr>Node<wbr>Count</a>
939-
</li>
940937
<li class=" tsd-kind-method tsd-parent-kind-class tsd-is-protected">
941938
<a href="component.html#reconstruct" class="tsd-kind-icon">reconstruct</a>
942939
</li>
@@ -1058,7 +1055,6 @@ <h3>Methods</h3>
10581055
<li class="tsd-kind-method tsd-parent-kind-class"><a href="component.html#at" class="tsd-kind-icon">at</a></li>
10591056
<li class="tsd-kind-method tsd-parent-kind-class"><a href="component.html#findnodes" class="tsd-kind-icon">find<wbr>Nodes</a></li>
10601057
<li class="tsd-kind-method tsd-parent-kind-class"><a href="component.html#finduniquenode" class="tsd-kind-icon">find<wbr>Unique<wbr>Node</a></li>
1061-
<li class="tsd-kind-method tsd-parent-kind-class"><a href="component.html#getnodecount" class="tsd-kind-icon">get<wbr>Node<wbr>Count</a></li>
10621058
<li class="tsd-kind-method tsd-parent-kind-class tsd-is-protected"><a href="component.html#reconstruct" class="tsd-kind-icon">reconstruct</a></li>
10631059
<li class="tsd-kind-method tsd-parent-kind-class"><a href="component.html#tostring" class="tsd-kind-icon">to<wbr>String</a></li>
10641060
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter"><a href="component.html#where" class="tsd-kind-icon">where</a></li>
@@ -1164,7 +1160,7 @@ <h3>find<wbr>Nodes</h3>
11641160
<li class="tsd-description">
11651161
<aside class="tsd-sources">
11661162
<ul>
1167-
<li>Defined in <a href="https://github.com/clebert/pageobject/blob/master/@pageobject/base/src/Component.ts#L60">Component.ts:60</a></li>
1163+
<li>Defined in <a href="https://github.com/clebert/pageobject/blob/master/@pageobject/base/src/Component.ts#L64">Component.ts:64</a></li>
11681164
</ul>
11691165
</aside>
11701166
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">TNode</span><span class="tsd-signature-symbol">[]</span><span class="tsd-signature-symbol">&gt;</span></h4>
@@ -1181,30 +1177,13 @@ <h3>find<wbr>Unique<wbr>Node</h3>
11811177
<li class="tsd-description">
11821178
<aside class="tsd-sources">
11831179
<ul>
1184-
<li>Defined in <a href="https://github.com/clebert/pageobject/blob/master/@pageobject/base/src/Component.ts#L101">Component.ts:101</a></li>
1180+
<li>Defined in <a href="https://github.com/clebert/pageobject/blob/master/@pageobject/base/src/Component.ts#L103">Component.ts:103</a></li>
11851181
</ul>
11861182
</aside>
11871183
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">TNode</span><span class="tsd-signature-symbol">&gt;</span></h4>
11881184
</li>
11891185
</ul>
11901186
</section>
1191-
<section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class">
1192-
<a name="getnodecount" class="tsd-anchor"></a>
1193-
<h3>get<wbr>Node<wbr>Count</h3>
1194-
<ul class="tsd-signatures tsd-kind-method tsd-parent-kind-class">
1195-
<li class="tsd-signature tsd-kind-icon">get<wbr>Node<wbr>Count<span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="../globals.html#effect" class="tsd-signature-type">Effect</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">&gt;</span></li>
1196-
</ul>
1197-
<ul class="tsd-descriptions">
1198-
<li class="tsd-description">
1199-
<aside class="tsd-sources">
1200-
<ul>
1201-
<li>Defined in <a href="https://github.com/clebert/pageobject/blob/master/@pageobject/base/src/Component.ts#L119">Component.ts:119</a></li>
1202-
</ul>
1203-
</aside>
1204-
<h4 class="tsd-returns-title">Returns <a href="../globals.html#effect" class="tsd-signature-type">Effect</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">&gt;</span></h4>
1205-
</li>
1206-
</ul>
1207-
</section>
12081187
<section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class tsd-is-protected">
12091188
<a name="reconstruct" class="tsd-anchor"></a>
12101189
<h3><span class="tsd-flag ts-flagProtected">Protected</span> reconstruct</h3>
@@ -1215,7 +1194,7 @@ <h3><span class="tsd-flag ts-flagProtected">Protected</span> reconstruct</h3>
12151194
<li class="tsd-description">
12161195
<aside class="tsd-sources">
12171196
<ul>
1218-
<li>Defined in <a href="https://github.com/clebert/pageobject/blob/master/@pageobject/base/src/Component.ts#L127">Component.ts:127</a></li>
1197+
<li>Defined in <a href="https://github.com/clebert/pageobject/blob/master/@pageobject/base/src/Component.ts#L122">Component.ts:122</a></li>
12191198
</ul>
12201199
</aside>
12211200
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">this</span></h4>
@@ -1232,7 +1211,7 @@ <h3>to<wbr>String</h3>
12321211
<li class="tsd-description">
12331212
<aside class="tsd-sources">
12341213
<ul>
1235-
<li>Defined in <a href="https://github.com/clebert/pageobject/blob/master/@pageobject/base/src/Component.ts#L123">Component.ts:123</a></li>
1214+
<li>Defined in <a href="https://github.com/clebert/pageobject/blob/master/@pageobject/base/src/Component.ts#L118">Component.ts:118</a></li>
12361215
</ul>
12371216
</aside>
12381217
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">string</span></h4>
@@ -1249,7 +1228,7 @@ <h3>where</h3>
12491228
<li class="tsd-description">
12501229
<aside class="tsd-sources">
12511230
<ul>
1252-
<li>Defined in <a href="https://github.com/clebert/pageobject/blob/master/@pageobject/base/src/Component.ts#L45">Component.ts:45</a></li>
1231+
<li>Defined in <a href="https://github.com/clebert/pageobject/blob/master/@pageobject/base/src/Component.ts#L49">Component.ts:49</a></li>
12531232
</ul>
12541233
</aside>
12551234
<h4 class="tsd-type-parameters-title">Type parameters</h4>

0 commit comments

Comments
 (0)