@interactors/core 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +9 -0
- package/dist/cjs/constructor.js +260 -0
- package/dist/cjs/constructor.js.map +1 -0
- package/dist/cjs/converge.js +27 -0
- package/dist/cjs/converge.js.map +1 -0
- package/dist/cjs/create-interactor.js +24 -0
- package/dist/cjs/create-interactor.js.map +1 -0
- package/dist/cjs/dispatch.js +34 -0
- package/dist/cjs/dispatch.js.map +1 -0
- package/dist/cjs/element/click.js +12 -0
- package/dist/cjs/element/click.js.map +1 -0
- package/dist/cjs/element/fill-in.js +71 -0
- package/dist/cjs/element/fill-in.js.map +1 -0
- package/dist/cjs/errors.js +32 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/escape-html.js +13 -0
- package/dist/cjs/escape-html.js.map +1 -0
- package/dist/cjs/filter-set.js +50 -0
- package/dist/cjs/filter-set.js.map +1 -0
- package/dist/cjs/focused.js +47 -0
- package/dist/cjs/focused.js.map +1 -0
- package/dist/cjs/format-table.js +30 -0
- package/dist/cjs/format-table.js.map +1 -0
- package/dist/cjs/index.js +47 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/inspector.js +22 -0
- package/dist/cjs/inspector.js.map +1 -0
- package/dist/cjs/interaction.js +53 -0
- package/dist/cjs/interaction.js.map +1 -0
- package/dist/cjs/locator.js +19 -0
- package/dist/cjs/locator.js.map +1 -0
- package/dist/cjs/match.js +135 -0
- package/dist/cjs/match.js.map +1 -0
- package/dist/cjs/matcher.js +30 -0
- package/dist/cjs/matcher.js.map +1 -0
- package/dist/cjs/matchers/and.js +16 -0
- package/dist/cjs/matchers/and.js.map +1 -0
- package/dist/cjs/matchers/every.js +16 -0
- package/dist/cjs/matchers/every.js.map +1 -0
- package/dist/cjs/matchers/including.js +15 -0
- package/dist/cjs/matchers/including.js.map +1 -0
- package/dist/cjs/matchers/matching.js +15 -0
- package/dist/cjs/matchers/matching.js.map +1 -0
- package/dist/cjs/matchers/not.js +16 -0
- package/dist/cjs/matchers/not.js.map +1 -0
- package/dist/cjs/matchers/or.js +16 -0
- package/dist/cjs/matchers/or.js.map +1 -0
- package/dist/cjs/matchers/some.js +16 -0
- package/dist/cjs/matchers/some.js.map +1 -0
- package/dist/cjs/merge-objects.js +3 -0
- package/dist/cjs/merge-objects.js.map +1 -0
- package/dist/cjs/perform.js +13 -0
- package/dist/cjs/perform.js.map +1 -0
- package/dist/cjs/specification.js +4 -0
- package/dist/cjs/specification.js.map +1 -0
- package/dist/constructor.d.ts +11 -0
- package/dist/constructor.d.ts.map +1 -0
- package/dist/converge.d.ts +2 -0
- package/dist/converge.d.ts.map +1 -0
- package/dist/create-interactor.d.ts +18 -0
- package/dist/create-interactor.d.ts.map +1 -0
- package/dist/dispatch.d.ts +6 -0
- package/dist/dispatch.d.ts.map +1 -0
- package/dist/element/click.d.ts +2 -0
- package/dist/element/click.d.ts.map +1 -0
- package/dist/element/fill-in.d.ts +12 -0
- package/dist/element/fill-in.d.ts.map +1 -0
- package/dist/errors.d.ts +13 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/escape-html.d.ts +2 -0
- package/dist/escape-html.d.ts.map +1 -0
- package/dist/esm/constructor.js +248 -0
- package/dist/esm/constructor.js.map +1 -0
- package/dist/esm/converge.js +23 -0
- package/dist/esm/converge.js.map +1 -0
- package/dist/esm/create-interactor.js +20 -0
- package/dist/esm/create-interactor.js.map +1 -0
- package/dist/esm/dispatch.js +26 -0
- package/dist/esm/dispatch.js.map +1 -0
- package/dist/esm/element/click.js +5 -0
- package/dist/esm/element/click.js.map +1 -0
- package/dist/esm/element/fill-in.js +67 -0
- package/dist/esm/element/fill-in.js.map +1 -0
- package/dist/esm/errors.js +25 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/escape-html.js +9 -0
- package/dist/esm/escape-html.js.map +1 -0
- package/dist/esm/filter-set.js +46 -0
- package/dist/esm/filter-set.js.map +1 -0
- package/dist/esm/focused.js +41 -0
- package/dist/esm/focused.js.map +1 -0
- package/dist/esm/format-table.js +26 -0
- package/dist/esm/format-table.js.map +1 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/inspector.js +18 -0
- package/dist/esm/inspector.js.map +1 -0
- package/dist/esm/interaction.js +45 -0
- package/dist/esm/interaction.js.map +1 -0
- package/dist/esm/locator.js +15 -0
- package/dist/esm/locator.js.map +1 -0
- package/dist/esm/match.js +127 -0
- package/dist/esm/match.js.map +1 -0
- package/dist/esm/matcher.js +21 -0
- package/dist/esm/matcher.js.map +1 -0
- package/dist/esm/matchers/and.js +12 -0
- package/dist/esm/matchers/and.js.map +1 -0
- package/dist/esm/matchers/every.js +12 -0
- package/dist/esm/matchers/every.js.map +1 -0
- package/dist/esm/matchers/including.js +11 -0
- package/dist/esm/matchers/including.js.map +1 -0
- package/dist/esm/matchers/matching.js +11 -0
- package/dist/esm/matchers/matching.js.map +1 -0
- package/dist/esm/matchers/not.js +12 -0
- package/dist/esm/matchers/not.js.map +1 -0
- package/dist/esm/matchers/or.js +12 -0
- package/dist/esm/matchers/or.js.map +1 -0
- package/dist/esm/matchers/some.js +12 -0
- package/dist/esm/matchers/some.js.map +1 -0
- package/dist/esm/merge-objects.js +2 -0
- package/dist/esm/merge-objects.js.map +1 -0
- package/dist/esm/perform.js +9 -0
- package/dist/esm/perform.js.map +1 -0
- package/dist/esm/specification.js +3 -0
- package/dist/esm/specification.js.map +1 -0
- package/dist/filter-set.d.ts +10 -0
- package/dist/filter-set.d.ts.map +1 -0
- package/dist/focused.d.ts +30 -0
- package/dist/focused.d.ts.map +1 -0
- package/dist/format-table.d.ts +6 -0
- package/dist/format-table.d.ts.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/inspector.d.ts +17 -0
- package/dist/inspector.d.ts.map +1 -0
- package/dist/interaction.d.ts +42 -0
- package/dist/interaction.d.ts.map +1 -0
- package/dist/locator.d.ts +10 -0
- package/dist/locator.d.ts.map +1 -0
- package/dist/match.d.ts +54 -0
- package/dist/match.d.ts.map +1 -0
- package/dist/matcher.d.ts +9 -0
- package/dist/matcher.d.ts.map +1 -0
- package/dist/matchers/and.d.ts +3 -0
- package/dist/matchers/and.d.ts.map +1 -0
- package/dist/matchers/every.d.ts +3 -0
- package/dist/matchers/every.d.ts.map +1 -0
- package/dist/matchers/including.d.ts +3 -0
- package/dist/matchers/including.d.ts.map +1 -0
- package/dist/matchers/matching.d.ts +3 -0
- package/dist/matchers/matching.d.ts.map +1 -0
- package/dist/matchers/not.d.ts +3 -0
- package/dist/matchers/not.d.ts.map +1 -0
- package/dist/matchers/or.d.ts +3 -0
- package/dist/matchers/or.d.ts.map +1 -0
- package/dist/matchers/some.d.ts +3 -0
- package/dist/matchers/some.d.ts.map +1 -0
- package/dist/merge-objects.d.ts +2 -0
- package/dist/merge-objects.d.ts.map +1 -0
- package/dist/perform.d.ts +3 -0
- package/dist/perform.d.ts.map +1 -0
- package/dist/specification.d.ts +217 -0
- package/dist/specification.d.ts.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +65 -0
- package/src/constructor.ts +298 -0
- package/src/converge.ts +22 -0
- package/src/create-interactor.ts +21 -0
- package/src/dispatch.ts +24 -0
- package/src/element/click.ts +5 -0
- package/src/element/fill-in.ts +76 -0
- package/src/errors.ts +15 -0
- package/src/escape-html.ts +8 -0
- package/src/filter-set.ts +45 -0
- package/src/focused.ts +43 -0
- package/src/format-table.ts +34 -0
- package/src/index.ts +20 -0
- package/src/inspector.ts +42 -0
- package/src/interaction.ts +82 -0
- package/src/locator.ts +15 -0
- package/src/match.ts +162 -0
- package/src/matcher.ts +28 -0
- package/src/matchers/and.ts +12 -0
- package/src/matchers/every.ts +12 -0
- package/src/matchers/including.ts +12 -0
- package/src/matchers/matching.ts +12 -0
- package/src/matchers/not.ts +12 -0
- package/src/matchers/or.ts +12 -0
- package/src/matchers/some.ts +12 -0
- package/src/merge-objects.ts +2 -0
- package/src/perform.ts +11 -0
- package/src/specification.ts +253 -0
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@interactors/core",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Composable page objects for components",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"browser": "dist/esm/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"repository": "https://github.com/thefrontside/interactors.git",
|
|
9
|
+
"homepage": "https://frontside.com/interactors",
|
|
10
|
+
"author": "Frontside Engineering <engineering@frontside.com>",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist/**/*",
|
|
14
|
+
"src/**/*",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"require": "./dist/cjs/index.js",
|
|
20
|
+
"default": "./dist/esm/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"clean": "rm -rf dist *.tsbuildinfo",
|
|
25
|
+
"lint": "eslint \"{src,test}/**/*.ts\"",
|
|
26
|
+
"check:types": "tsc --noEmit",
|
|
27
|
+
"test:unit": "mocha \"test/**/*.test.ts\"",
|
|
28
|
+
"test:types": "dtslint types --localTs ../../node_modules/typescript/lib --expectOnly",
|
|
29
|
+
"test": "yarn test:unit && yarn test:types",
|
|
30
|
+
"docs": "rm -rf docs && yarn typedoc --options typedoc.json",
|
|
31
|
+
"prepack": "tsc --build ./tsconfig.build.json && yarn prepack:es2015 && yarn prepack:commonjs",
|
|
32
|
+
"prepack:es2015": "tsc --project ./tsconfig.build.json --outdir dist/esm --module es2015",
|
|
33
|
+
"prepack:commonjs": "tsc --project ./tsconfig.build.json --outdir dist/cjs --module commonjs"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@bigtest/globals": "^0.7.6",
|
|
37
|
+
"@interactors/globals": "^0.1.0",
|
|
38
|
+
"@testing-library/dom": "^8.5.0",
|
|
39
|
+
"@testing-library/user-event": "^13.2.1",
|
|
40
|
+
"change-case": "^4.1.1",
|
|
41
|
+
"element-is-visible": "^1.0.0",
|
|
42
|
+
"lodash.isequal": "^4.5.0",
|
|
43
|
+
"performance-api": "^1.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@frontside/tsconfig": "^1.2.0",
|
|
47
|
+
"@types/express": "^4.17.6",
|
|
48
|
+
"@types/jsdom": "^16.2.3",
|
|
49
|
+
"@types/lodash.isequal": "^4.5.5",
|
|
50
|
+
"@types/mocha": "^7.0.1",
|
|
51
|
+
"@types/node": "^14.17.5",
|
|
52
|
+
"dtslint": "^4.0.5",
|
|
53
|
+
"expect": "^24.9.0",
|
|
54
|
+
"express": "^4.17.1",
|
|
55
|
+
"jsdom": "^16.2.2",
|
|
56
|
+
"mocha": "^6.2.2",
|
|
57
|
+
"ts-node": "^10.4.0",
|
|
58
|
+
"typedoc": "^0.22.7",
|
|
59
|
+
"typescript": "~4.4.4"
|
|
60
|
+
},
|
|
61
|
+
"volta": {
|
|
62
|
+
"node": "14.17.5",
|
|
63
|
+
"yarn": "1.22.11"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
import { globals } from '@interactors/globals';
|
|
4
|
+
import { converge } from './converge';
|
|
5
|
+
import { MergeObjects } from './merge-objects';
|
|
6
|
+
import {
|
|
7
|
+
InteractorOptions,
|
|
8
|
+
ActionMethods,
|
|
9
|
+
InteractorConstructor,
|
|
10
|
+
Interactor,
|
|
11
|
+
Filters,
|
|
12
|
+
Actions,
|
|
13
|
+
FilterDefinition,
|
|
14
|
+
FilterParams,
|
|
15
|
+
FilterMethods,
|
|
16
|
+
InteractorSpecification,
|
|
17
|
+
BaseInteractor,
|
|
18
|
+
FilterObject,
|
|
19
|
+
} from './specification';
|
|
20
|
+
import { FilterSet } from './filter-set';
|
|
21
|
+
import { Locator } from './locator';
|
|
22
|
+
import { MatchFilter, applyFilter } from './match';
|
|
23
|
+
import { formatTable } from './format-table';
|
|
24
|
+
import { FilterNotMatchingError } from './errors';
|
|
25
|
+
import { interaction, interactionFilter, check, checkFilter, Interaction, ReadonlyInteraction } from './interaction';
|
|
26
|
+
import { Match } from './match';
|
|
27
|
+
import { NoSuchElementError, NotAbsentError, AmbiguousElementError } from './errors';
|
|
28
|
+
import { isMatcher } from './matcher';
|
|
29
|
+
import { matching } from './matchers/matching';
|
|
30
|
+
|
|
31
|
+
const defaultLocator: FilterDefinition<string, Element> = (element) => element.textContent || "";
|
|
32
|
+
const defaultSelector = 'div';
|
|
33
|
+
|
|
34
|
+
export function findElements<E extends Element>(parentElement: Element, interactor: InteractorOptions<any, any, any>): E[] {
|
|
35
|
+
if(typeof(interactor.specification.selector) === 'function') {
|
|
36
|
+
return interactor.specification.selector(parentElement);
|
|
37
|
+
} else if(interactor.specification.selector === ':root') {
|
|
38
|
+
// this is a bit of a hack, because otherwise there isn't a good way of selecting the root element
|
|
39
|
+
return [parentElement.ownerDocument.querySelector(':root') as E];
|
|
40
|
+
} else {
|
|
41
|
+
return Array.from(parentElement.querySelectorAll(interactor.specification.selector || defaultSelector));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function findMatches(parentElement: Element, interactor: InteractorOptions<any, any, any>): Match<Element, any>[] {
|
|
46
|
+
if (!interactor.name) {
|
|
47
|
+
throw new Error('One of your interactors was created without a name. Please provide a label for your interactor:\n\tHTML.extend(\'my interactor\') || createInteractor(\'my interactor\')');
|
|
48
|
+
}
|
|
49
|
+
return findElements(parentElement, interactor).map((e) => new Match(e, interactor.filter, interactor.locator));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function findMatchesMatching(parentElement: Element, interactor: InteractorOptions<any, any, any>): Match<Element, any>[] {
|
|
53
|
+
return findMatches(parentElement, interactor).filter((m) => m.matches);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function findMatchesNonEmpty(parentElement: Element, interactor: InteractorOptions<any, any, any>): Match<Element, any>[] {
|
|
57
|
+
let matches = findMatches(parentElement, interactor);
|
|
58
|
+
let matching = matches.filter((m) => m.matches);
|
|
59
|
+
if(matching.length > 0) {
|
|
60
|
+
return matching;
|
|
61
|
+
} else if(matches.length === 0) {
|
|
62
|
+
throw new NoSuchElementError(`did not find ${description(interactor)}`);
|
|
63
|
+
} else {
|
|
64
|
+
let table = formatTable({
|
|
65
|
+
headers: interactor.locator ? [interactor.name, ...interactor.filter.asTableHeader()] : interactor.filter.asTableHeader(),
|
|
66
|
+
rows: matches.slice().sort((a, b) => b.sortWeight - a.sortWeight).map((m) => m.asTableRow()),
|
|
67
|
+
});
|
|
68
|
+
throw new NoSuchElementError(`did not find ${description(interactor)}, did you mean one of:\n\n${table}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Given a parent element, and an interactor, find exactly one matching element
|
|
73
|
+
// and return it. If no elements match, raise an error. If more than one
|
|
74
|
+
// element matches, raise an error.
|
|
75
|
+
export function resolveUnique(parentElement: Element, interactor: InteractorOptions<any, any, any>): Element {
|
|
76
|
+
let matching = findMatchesNonEmpty(parentElement, interactor);
|
|
77
|
+
|
|
78
|
+
if(matching.length === 1) {
|
|
79
|
+
return matching[0].element;
|
|
80
|
+
} else {
|
|
81
|
+
let alternatives = matching.map((m) => '- ' + m.elementDescription());
|
|
82
|
+
throw new AmbiguousElementError(`${description(interactor)} matches multiple elements:\n\n${alternatives.join('\n')}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Given a parent element, and an interactor, find all matching elements and
|
|
87
|
+
// return them. If no elements match, raise an error.
|
|
88
|
+
export function resolveNonEmpty(parentElement: Element, interactor: InteractorOptions<any, any, any>): Element[] {
|
|
89
|
+
return findMatchesNonEmpty(parentElement, interactor).map(m => m.element);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Given a parent element, and an interactor, check if there are any matching
|
|
93
|
+
// elements, and throw an error if there are. Otherwise return undefined.
|
|
94
|
+
export function resolveEmpty(parentElement: Element, interactor: InteractorOptions<any, any, any>): void {
|
|
95
|
+
let matching = findMatchesMatching(parentElement, interactor);
|
|
96
|
+
|
|
97
|
+
if(matching.length !== 0) {
|
|
98
|
+
let alternatives = matching.map((m) => '- ' + m.elementDescription());
|
|
99
|
+
throw new NotAbsentError(`${description(interactor)} exists but should not:\n\n${alternatives.join('\n')}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function ownDescription(options: InteractorOptions<any, any, any>): string {
|
|
104
|
+
if(options.locator) {
|
|
105
|
+
return `${options.name} ${options.locator.description} ${options.filter.description}`.trim();
|
|
106
|
+
} else {
|
|
107
|
+
return `${options.name} ${options.filter.description}`.trim();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function description(options: InteractorOptions<any, any, any>): string {
|
|
112
|
+
let ancestorsAndSelf: InteractorOptions<any, any, any>[] = options.ancestors.concat(options);
|
|
113
|
+
return ancestorsAndSelf.reverse().map(ownDescription).join(' within ');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Removes any default values for a filter from the lookup if that filter is present in the
|
|
118
|
+
* assertion. Otherwise, it is not possible to make an assertion on a filter that might conflict
|
|
119
|
+
* see https://github.com/thefrontside/bigtest/issues/966
|
|
120
|
+
*/
|
|
121
|
+
function getLookupFilterForAssertion<E extends Element, F extends Filters<E>>(filter: FilterSet<E, F>, filters: FilterParams<E, F>): FilterSet<E, F> {
|
|
122
|
+
let lookupFilter = new FilterSet(filter.specification, Object.assign({}, filter.filters));
|
|
123
|
+
let specFilters = lookupFilter.specification.filters;
|
|
124
|
+
for (let key in specFilters) {
|
|
125
|
+
if (typeof specFilters[key] !== 'function'
|
|
126
|
+
&& 'default' in specFilters[key]
|
|
127
|
+
&& key in filters
|
|
128
|
+
&& !(key in lookupFilter.filters)) {
|
|
129
|
+
lookupFilter.filters[key] = filters[key];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return lookupFilter;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function unsafeSyncResolveParent(options: InteractorOptions<any, any, any>): Element {
|
|
136
|
+
return options.ancestors.reduce(resolveUnique, globals.document.documentElement);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function unsafeSyncResolveUnique<E extends Element>(options: InteractorOptions<E, any, any>): E {
|
|
140
|
+
return resolveUnique(unsafeSyncResolveParent(options), options) as E;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function instantiateBaseInteractor<E extends Element, F extends Filters<E>, A extends Actions<E>>(
|
|
144
|
+
options: InteractorOptions<E, F, A>,
|
|
145
|
+
resolver: (options: InteractorOptions<E, F, A>) => E
|
|
146
|
+
): BaseInteractor<E, FilterParams<E, F>> & ActionMethods<E, A> {
|
|
147
|
+
let interactor = {
|
|
148
|
+
options,
|
|
149
|
+
|
|
150
|
+
get description(): string {
|
|
151
|
+
return description(options);
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
perform<T>(fn: (element: E) => T): Interaction<T> {
|
|
155
|
+
return interaction(`run perform on ${description(options)}`, () => converge(() => fn(resolver(options))))
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
assert(fn: (element: E) => void): ReadonlyInteraction<void> {
|
|
159
|
+
return check(`${description(options)} asserts`, () => {
|
|
160
|
+
return converge(() => {
|
|
161
|
+
fn(resolver(options));
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
has(filters: FilterParams<E, F>): ReadonlyInteraction<void> {
|
|
167
|
+
return interactor.is(filters);
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
is(filters: FilterParams<E, F>): ReadonlyInteraction<void> {
|
|
171
|
+
let filter = new FilterSet(options.specification, filters);
|
|
172
|
+
return check(`${description(options)} matches filters: ${filter.description}`, () => {
|
|
173
|
+
return converge(() => {
|
|
174
|
+
let element = resolver({...options, filter: getLookupFilterForAssertion(options.filter, filters) });
|
|
175
|
+
let match = new MatchFilter(element, filter);
|
|
176
|
+
if (!match.matches) {
|
|
177
|
+
throw new FilterNotMatchingError(`${description(options)} does not match filters:\n\n${match.formatAsExpectations()}`);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
apply(parentElement: Element): string {
|
|
184
|
+
let element = [...options.ancestors, options].reduce(resolveUnique, parentElement);
|
|
185
|
+
return applyFilter(options.specification.locator || defaultLocator, element);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
for (let [actionName, action] of Object.entries(options.specification.actions || {})) {
|
|
190
|
+
if (!interactor.hasOwnProperty(actionName)) {
|
|
191
|
+
Object.defineProperty(interactor, actionName, {
|
|
192
|
+
value: function(...args: unknown[]) {
|
|
193
|
+
let actionDescription = actionName;
|
|
194
|
+
if (args.length) {
|
|
195
|
+
actionDescription += ` with ` + args.map((a) => JSON.stringify(a)).join(', ');
|
|
196
|
+
}
|
|
197
|
+
return interaction(
|
|
198
|
+
`${actionDescription} on ${description(options)}`,
|
|
199
|
+
() => action(interactor as Interactor<E, FilterParams<E, F>> & ActionMethods<E, A>, ...args)
|
|
200
|
+
);
|
|
201
|
+
},
|
|
202
|
+
configurable: true,
|
|
203
|
+
writable: true,
|
|
204
|
+
enumerable: false,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
for (let [filterName, filter] of Object.entries(options.specification.filters || {})) {
|
|
210
|
+
if (!interactor.hasOwnProperty(filterName)) {
|
|
211
|
+
Object.defineProperty(interactor, filterName, {
|
|
212
|
+
value: function() {
|
|
213
|
+
return interactionFilter(`${filterName} of ${description(options)}`, async () => {
|
|
214
|
+
return applyFilter(filter, resolver(options));
|
|
215
|
+
}, (parentElement) => {
|
|
216
|
+
let element = [...options.ancestors, options].reduce(resolveUnique, parentElement);
|
|
217
|
+
return applyFilter(filter, element);
|
|
218
|
+
});
|
|
219
|
+
},
|
|
220
|
+
configurable: true,
|
|
221
|
+
writable: true,
|
|
222
|
+
enumerable: false,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return interactor as BaseInteractor<E, FilterParams<E, F>> & ActionMethods<E, A>;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function instantiateInteractor<E extends Element, F extends Filters<E>, A extends Actions<E>>(
|
|
231
|
+
options: InteractorOptions<E, F, A>,
|
|
232
|
+
): Interactor<E, FilterParams<E, F>> & ActionMethods<E, A> {
|
|
233
|
+
let interactor = instantiateBaseInteractor(options, unsafeSyncResolveUnique)
|
|
234
|
+
|
|
235
|
+
return Object.assign(interactor, {
|
|
236
|
+
find<T extends Interactor<any, any>>(child: T): T {
|
|
237
|
+
return instantiateInteractor({
|
|
238
|
+
...child.options,
|
|
239
|
+
ancestors: [...options.ancestors, options, ...child.options.ancestors]
|
|
240
|
+
}) as unknown as T;
|
|
241
|
+
},
|
|
242
|
+
|
|
243
|
+
exists(): ReadonlyInteraction<void> & FilterObject<boolean, Element> {
|
|
244
|
+
return checkFilter(`${description(options)} exists`, () => {
|
|
245
|
+
return converge(() => {
|
|
246
|
+
resolveNonEmpty(unsafeSyncResolveParent(options), options);
|
|
247
|
+
});
|
|
248
|
+
}, (element) => {
|
|
249
|
+
return findMatchesMatching(element, options).length > 0;
|
|
250
|
+
});
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
absent(): ReadonlyInteraction<void> & FilterObject<boolean, Element> {
|
|
254
|
+
return checkFilter(`${description(options)} does not exist`, () => {
|
|
255
|
+
return converge(() => {
|
|
256
|
+
resolveEmpty(unsafeSyncResolveParent(options), options);
|
|
257
|
+
});
|
|
258
|
+
}, (element) => {
|
|
259
|
+
return findMatchesMatching(element, options).length === 0;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function createConstructor<E extends Element, FP extends FilterParams<any, any>, FM extends FilterMethods<any, any>, AM extends ActionMethods<any, any>>(
|
|
266
|
+
name: string,
|
|
267
|
+
specification: InteractorSpecification<E, any, any>,
|
|
268
|
+
): InteractorConstructor<E, FP, FM, AM> {
|
|
269
|
+
function initInteractor(...args: any[]) {
|
|
270
|
+
let locator, filter;
|
|
271
|
+
let locatorValue = args[0] instanceof RegExp ? matching(args[0]) : args[0]
|
|
272
|
+
if (typeof(locatorValue) === 'string' || isMatcher(locatorValue)) {
|
|
273
|
+
locator = new Locator(specification.locator || defaultLocator, locatorValue);
|
|
274
|
+
filter = new FilterSet(specification, args[1] || {});
|
|
275
|
+
} else {
|
|
276
|
+
filter = new FilterSet(specification, args[0] || {});
|
|
277
|
+
}
|
|
278
|
+
return instantiateInteractor({ name, specification, filter, locator, ancestors: [] });
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return Object.assign(initInteractor, {
|
|
282
|
+
selector: (value: string): InteractorConstructor<E, FP, FM, AM> => {
|
|
283
|
+
return createConstructor(name, { ...specification, selector: value });
|
|
284
|
+
},
|
|
285
|
+
locator: (value: FilterDefinition<string, E>): InteractorConstructor<E, FP, FM, AM> => {
|
|
286
|
+
return createConstructor(name, { ...specification, locator: value });
|
|
287
|
+
},
|
|
288
|
+
filters: <FR extends Filters<E>>(filters: FR): InteractorConstructor<E, MergeObjects<FP, FilterParams<E, FR>>, MergeObjects<FM, FilterMethods<E, FR>>, AM> => {
|
|
289
|
+
return createConstructor(name, { ...specification, filters: { ...specification.filters, ...filters } });
|
|
290
|
+
},
|
|
291
|
+
actions: <AR extends Actions<E>>(actions: AR): InteractorConstructor<E, FP, FM, MergeObjects<AM, ActionMethods<E, AR>>> => {
|
|
292
|
+
return createConstructor(name, { ...specification, actions: Object.assign({}, specification.actions, actions) });
|
|
293
|
+
},
|
|
294
|
+
extend: <ER extends Element = E>(newName: string): InteractorConstructor<ER, FP, FM, AM> => {
|
|
295
|
+
return createConstructor(newName, specification) as unknown as InteractorConstructor<ER, FP, FM, AM>;
|
|
296
|
+
},
|
|
297
|
+
}) as unknown as InteractorConstructor<E, FP, FM, AM>;
|
|
298
|
+
}
|
package/src/converge.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { performance } from 'performance-api';
|
|
2
|
+
import { globals } from '@interactors/globals'
|
|
3
|
+
|
|
4
|
+
function wait(ms: number) {
|
|
5
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function converge<T>(fn: () => T): Promise<T> {
|
|
9
|
+
let startTime = performance.now();
|
|
10
|
+
while(true) {
|
|
11
|
+
try {
|
|
12
|
+
return fn();
|
|
13
|
+
} catch(e) {
|
|
14
|
+
let diff = performance.now() - startTime;
|
|
15
|
+
if(diff > globals.interactorTimeout) {
|
|
16
|
+
throw e;
|
|
17
|
+
} else {
|
|
18
|
+
await wait(1);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EmptyObject, InteractorConstructor } from './specification';
|
|
2
|
+
import { createConstructor } from './constructor';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Create a custom interactor with the given name.
|
|
6
|
+
*
|
|
7
|
+
* ### Creating a simple interactor
|
|
8
|
+
*
|
|
9
|
+
* ``` typescript
|
|
10
|
+
* let Paragraph = createInteractor('paragraph').selector('p');
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Note the double function call!
|
|
14
|
+
*
|
|
15
|
+
* @param interactorName The human readable name of the interactor, used mainly for debugging purposes and error messages
|
|
16
|
+
* @typeParam E The type of DOM Element that this interactor operates on. By specifying the element type, actions and filters defined for the interactor can be type checked against the actual element type.
|
|
17
|
+
* @returns You will need to call the returned builder to create an interactor.
|
|
18
|
+
*/
|
|
19
|
+
export function createInteractor<E extends Element>(name: string): InteractorConstructor<E, EmptyObject, EmptyObject, EmptyObject> {
|
|
20
|
+
return createConstructor(name, {});
|
|
21
|
+
}
|
package/src/dispatch.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function dispatchChange(element: HTMLElement): boolean {
|
|
2
|
+
let Event = element.ownerDocument.defaultView?.Event || window.Event;
|
|
3
|
+
return element.dispatchEvent(new Event('change', { bubbles: true, cancelable: false }));
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function dispatchInput(element: HTMLElement, options: InputEventInit = {}): boolean {
|
|
7
|
+
let InputEvent = element.ownerDocument.defaultView?.InputEvent || window.InputEvent;
|
|
8
|
+
return element.dispatchEvent(new InputEvent('input', Object.assign({ bubbles: true, cancelable: false }, options)));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function dispatchKeyDown(element: HTMLElement, options: KeyboardEventInit = {}): boolean {
|
|
12
|
+
let KeyboardEvent = element.ownerDocument.defaultView?.KeyboardEvent || window.KeyboardEvent;
|
|
13
|
+
return element.dispatchEvent(new KeyboardEvent('keydown', Object.assign({ bubbles: true, cancelable: true }, options)));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function dispatchKeyUp(element: HTMLElement, options: KeyboardEventInit = {}): boolean {
|
|
17
|
+
let KeyboardEvent = element.ownerDocument.defaultView?.KeyboardEvent || window.KeyboardEvent;
|
|
18
|
+
return element.dispatchEvent(new KeyboardEvent('keyup', Object.assign({ bubbles: true, cancelable: true }, options)));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function dispatchClick(element: HTMLElement, options: MouseEventInit = {}): boolean {
|
|
22
|
+
let MouseEvent = element.ownerDocument.defaultView?.MouseEvent || window.MouseEvent;
|
|
23
|
+
return element.dispatchEvent(new MouseEvent('click', Object.assign({ bubbles: true, cancelable: true }, options)));
|
|
24
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { dispatchChange, dispatchInput, dispatchKeyDown, dispatchKeyUp } from '../dispatch';
|
|
2
|
+
|
|
3
|
+
type TextFieldElement = HTMLInputElement | HTMLTextAreaElement;
|
|
4
|
+
|
|
5
|
+
// do a best effort at determining the key code
|
|
6
|
+
function guessCode(letter: string): string | undefined {
|
|
7
|
+
if(letter.match(/^[a-zA-Z]$/)) {
|
|
8
|
+
return `Key${letter.toUpperCase()}`;
|
|
9
|
+
} else if(letter.match(/^[0-9]$/)) {
|
|
10
|
+
return `Digit${letter}`;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function clearText(element: TextFieldElement) {
|
|
15
|
+
if(element.value.length) {
|
|
16
|
+
setValue(element, "");
|
|
17
|
+
dispatchInput(element, { inputType: 'deleteContentBackward', data: null });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function enterText(element: TextFieldElement, value: string) {
|
|
22
|
+
for(let letter of value) {
|
|
23
|
+
if(dispatchKeyDown(element, { key: letter, code: guessCode(letter) })) {
|
|
24
|
+
// don't change the value if the keydown event was stopped
|
|
25
|
+
setValue(element, element.value + letter);
|
|
26
|
+
// input is not dispatched if the keydown event was stopped
|
|
27
|
+
dispatchInput(element, { inputType: 'insertText', data: letter });
|
|
28
|
+
}
|
|
29
|
+
// keyup is always dispatched
|
|
30
|
+
dispatchKeyUp(element, { key: letter, code: guessCode(letter) });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Use the prototype setter of the element in order to set the value, that way any behavior that monkey patches
|
|
37
|
+
* the element itself is circumvented. This has the effect of the value just "magically" appearing on the input
|
|
38
|
+
* element just like it does when the browser sets the value because of the default action of an event.
|
|
39
|
+
* We have to do this because React actually does do this monkey-patching as an optimization. Basically, they
|
|
40
|
+
* short-circuit syncing the react state whenever someone sets input.value and then ignore the next `change` event.
|
|
41
|
+
*
|
|
42
|
+
* See https://github.com/cypress-io/cypress/issues/536
|
|
43
|
+
*/
|
|
44
|
+
function setValue(element: TextFieldElement, value: string): void {
|
|
45
|
+
let property = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(element), 'value');
|
|
46
|
+
if (property && property.set) {
|
|
47
|
+
property.set.call(element, value);
|
|
48
|
+
} else {
|
|
49
|
+
// if the value property on the element protoype is not present
|
|
50
|
+
// then there are worse problems. But this is very typesafe!
|
|
51
|
+
element.value = value;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Fill in text into an element by emulating how a user would do it, first
|
|
57
|
+
* focusing the element, then filling in the text letter by letter, generating
|
|
58
|
+
* the appropriate keyboard events.
|
|
59
|
+
*
|
|
60
|
+
* @param element The element to fill in text in
|
|
61
|
+
* @param value The text value to fill in
|
|
62
|
+
*/
|
|
63
|
+
export function fillIn(element: TextFieldElement, value: string): void {
|
|
64
|
+
let originalValue = element.value;
|
|
65
|
+
|
|
66
|
+
element.focus();
|
|
67
|
+
|
|
68
|
+
clearText(element);
|
|
69
|
+
enterText(element, value);
|
|
70
|
+
|
|
71
|
+
if(originalValue !== value) {
|
|
72
|
+
dispatchChange(element);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
element.blur();
|
|
76
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export class NoSuchElementError extends Error {
|
|
2
|
+
name = "NoSuchElementError";
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export class AmbiguousElementError extends Error {
|
|
6
|
+
name = "AmbiguousElementError";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class NotAbsentError extends Error {
|
|
10
|
+
name = "NotAbsentError";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class FilterNotMatchingError extends Error {
|
|
14
|
+
name = "FilterNotMatchingError";
|
|
15
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Filters, FilterFn, FilterObject, FilterParams, InteractorSpecification } from './specification';
|
|
2
|
+
import { noCase } from 'change-case';
|
|
3
|
+
import { matcherDescription } from './matcher';
|
|
4
|
+
|
|
5
|
+
export class FilterSet<E extends Element, F extends Filters<E>> {
|
|
6
|
+
constructor(
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
+
public specification: InteractorSpecification<E, F, any>,
|
|
9
|
+
public filters: FilterParams<E, F>,
|
|
10
|
+
) {};
|
|
11
|
+
|
|
12
|
+
get description(): string {
|
|
13
|
+
let entries = Object.entries(this.filters);
|
|
14
|
+
if(entries.length === 0) {
|
|
15
|
+
return '';
|
|
16
|
+
} else {
|
|
17
|
+
return entries.map(([key, value]) => {
|
|
18
|
+
if(typeof(value) === 'boolean') {
|
|
19
|
+
if(value) {
|
|
20
|
+
return `which is ${noCase(key)}`;
|
|
21
|
+
} else {
|
|
22
|
+
return `which is not ${noCase(key)}`;
|
|
23
|
+
}
|
|
24
|
+
} else {
|
|
25
|
+
return `with ${noCase(key)} ${matcherDescription(value)}`
|
|
26
|
+
}
|
|
27
|
+
}).join(' and ');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get all(): FilterParams<E, F> {
|
|
32
|
+
let filter: Record<string, unknown> = Object.assign({}, this.filters);
|
|
33
|
+
for(let key in this.specification.filters) {
|
|
34
|
+
let definition = this.specification.filters[key] as FilterFn<unknown, E> | FilterObject<unknown, E>;
|
|
35
|
+
if(!(key in this.filters) && typeof(definition) !== 'function' && 'default' in definition) {
|
|
36
|
+
filter[key] = definition.default;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return filter as FilterParams<E, F>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
asTableHeader(): string[] {
|
|
43
|
+
return Object.entries(this.all).map(([key, value]) => `${key}: ${matcherDescription(value)}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/focused.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
import { Interactor } from './specification';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Helper function for focused filters, returns whether the given element is focused.
|
|
7
|
+
*/
|
|
8
|
+
export function focused(element: Element): boolean {
|
|
9
|
+
console.warn("usage of the focused() helper is deprecated, and will be removed from the public API. Instead, switch your interactor to extend the `HTML` interactor to inherit the `focused` filter");
|
|
10
|
+
return element.ownerDocument.activeElement === element;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Helper function for focus action, performs `element.focus()` on the selected element.
|
|
15
|
+
*
|
|
16
|
+
* Can be used with object property value shorthand in your interactor actions.
|
|
17
|
+
*
|
|
18
|
+
* ```js
|
|
19
|
+
* actions: {
|
|
20
|
+
* focus,
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export async function focus<E extends HTMLElement>(interactor: Interactor<E, any>): Promise<void> {
|
|
25
|
+
console.warn("usage of the focus() helper is deprecated, and will be removed from the public API. Instead, switch your interactor to extend the `HTML` interactor to inherit the `focus()` action");
|
|
26
|
+
await interactor.perform((element) => element.focus())
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Helper function for blur action, performs `element.blur()` on the selected element.
|
|
31
|
+
*
|
|
32
|
+
* Can be used with object property value shorthand in your interactor actions.
|
|
33
|
+
*
|
|
34
|
+
* ```js
|
|
35
|
+
* actions: {
|
|
36
|
+
* focus,
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export async function blur<E extends HTMLElement>(interactor: Interactor<E, any>): Promise<void> {
|
|
41
|
+
console.warn("usage of the blur() helper is deprecated, and will be removed from the public API. Instead, switch your interactor to extend the `HTML` interactor to inherit the `blur()` action");
|
|
42
|
+
await interactor.perform((element) => element.blur())
|
|
43
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface TableOptions {
|
|
2
|
+
headers: string[];
|
|
3
|
+
rows: string[][];
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const MAX_COLUMN_WIDTH = 40
|
|
7
|
+
|
|
8
|
+
function formatValue(value: string, width: number) {
|
|
9
|
+
if(value.length > width) {
|
|
10
|
+
return value.slice(0, width - 1) + '…';
|
|
11
|
+
} else {
|
|
12
|
+
return value.padEnd(width);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function formatTable(options: TableOptions): string {
|
|
17
|
+
let columnWidths = options.headers.map((h, index) => {
|
|
18
|
+
return Math.min(MAX_COLUMN_WIDTH, Math.max(h.length, ...options.rows.map((r) => r[index].length)));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
let formatRow = (cells: string[]) => {
|
|
22
|
+
return '┃ ' + cells.map((c, index) => formatValue(c, columnWidths[index])).join(' ┃ ') + ' ┃';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let spacerRow = () => {
|
|
26
|
+
return '┣━' + columnWidths.map((w) => "━".repeat(w)).join('━╋━') + '━┫';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return [
|
|
30
|
+
formatRow(options.headers),
|
|
31
|
+
spacerRow(),
|
|
32
|
+
...options.rows.map((row) => formatRow(row))
|
|
33
|
+
].join('\n');
|
|
34
|
+
}
|