@universal-ember/test-support 0.6.1 → 0.7.1
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.
|
@@ -1,14 +1,33 @@
|
|
|
1
1
|
import { assert as assert$1 } from '@ember/debug';
|
|
2
2
|
import { visit, getContext, currentURL, find, click, findAll } from '@ember/test-helpers';
|
|
3
3
|
import QUnit from 'qunit';
|
|
4
|
+
import { shouldHandle } from 'should-handle-link';
|
|
4
5
|
|
|
5
6
|
function findInAppLinks() {
|
|
6
7
|
const results = [];
|
|
7
8
|
const allAnchorsOnThePage = findAll('a');
|
|
8
9
|
for (const a of allAnchorsOnThePage) {
|
|
10
|
+
// `findAll('a')` can also match SVG `<a>`, whose `href` is an
|
|
11
|
+
// SVGAnimatedString the router (and shouldHandle) can't work with.
|
|
12
|
+
if (!(a instanceof HTMLAnchorElement)) continue;
|
|
9
13
|
const href = a.getAttribute('href');
|
|
10
14
|
if (!href) continue;
|
|
11
|
-
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Links the SPA's router never handles are handled natively by the
|
|
18
|
+
* browser instead (new tab/window via `target`, download dialog,
|
|
19
|
+
* `mailto:`/`tel:`, cross-origin, `rel="external"`). Clicking them in a
|
|
20
|
+
* test can't change `currentURL()`, so they'd always be reported as
|
|
21
|
+
* failed navigations.
|
|
22
|
+
*
|
|
23
|
+
* `shouldHandle` is the predicate ember-primitives' @properLinks uses to
|
|
24
|
+
* decide whether the router handles a click, so the crawler visits
|
|
25
|
+
* exactly the set of links the router would. The fabricated click event
|
|
26
|
+
* carries the "plain left click" defaults (button 0, no modifier keys).
|
|
27
|
+
*/
|
|
28
|
+
if (!shouldHandle(window.location.href, a, new MouseEvent('click'))) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
12
31
|
const current = new URL(currentURL(), window.location.origin);
|
|
13
32
|
const url = new URL(href, current);
|
|
14
33
|
const withoutDomain = `${url.pathname}${url.search}${url.hash}`;
|
|
@@ -33,6 +52,7 @@ async function visitAllLinks(callback, knownRedirects) {
|
|
|
33
52
|
const ctx = getContext();
|
|
34
53
|
const router = ctx?.owner?.lookup('service:router');
|
|
35
54
|
assert$1(`Could not find the router service`, router);
|
|
55
|
+
const rootURL = router.rootURL;
|
|
36
56
|
while (queue.length > 0) {
|
|
37
57
|
const toVisit = queue.shift();
|
|
38
58
|
assert$1(`Queue entries cannot be falsey`, toVisit);
|
|
@@ -64,7 +84,7 @@ async function visitAllLinks(callback, knownRedirects) {
|
|
|
64
84
|
const link = find(toVisit.selector);
|
|
65
85
|
assert$1(`link exists via selector \`${toVisit.selector}\``, link);
|
|
66
86
|
await click(link);
|
|
67
|
-
const current = currentURL();
|
|
87
|
+
const current = rootURL.replace(/\/$/, '') + '/' + currentURL().replace(/^\//, '');
|
|
68
88
|
const expected = knownRedirects?.[toVisit.href] ?? toVisit.href;
|
|
69
89
|
assert.pushResult({
|
|
70
90
|
result: current.startsWith(expected),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"visit-all.js","sources":["../../src/routing/visit-all.ts"],"sourcesContent":["import { assert as debugAssert } from '@ember/debug';\nimport {\n visit,\n find,\n getContext,\n click,\n currentURL,\n findAll,\n} from '@ember/test-helpers';\nimport QUnit from 'qunit';\nimport type Owner from '@ember/owner';\nimport type RouterService from '@ember/routing/router-service';\n\ninterface InAppLink {\n href: string;\n original: string;\n selector: string;\n}\n\nfunction findInAppLinks(): InAppLink[] {\n const results: InAppLink[] = [];\n\n const allAnchorsOnThePage = findAll('a');\n\n for (const a of allAnchorsOnThePage) {\n const href = a.getAttribute('href');\n\n if (!href) continue;\n if (href
|
|
1
|
+
{"version":3,"file":"visit-all.js","sources":["../../src/routing/visit-all.ts"],"sourcesContent":["import { assert as debugAssert } from '@ember/debug';\nimport {\n visit,\n find,\n getContext,\n click,\n currentURL,\n findAll,\n} from '@ember/test-helpers';\nimport QUnit from 'qunit';\nimport { shouldHandle } from 'should-handle-link';\nimport type Owner from '@ember/owner';\nimport type RouterService from '@ember/routing/router-service';\n\ninterface InAppLink {\n href: string;\n original: string;\n selector: string;\n}\n\nfunction findInAppLinks(): InAppLink[] {\n const results: InAppLink[] = [];\n\n const allAnchorsOnThePage = findAll('a');\n\n for (const a of allAnchorsOnThePage) {\n // `findAll('a')` can also match SVG `<a>`, whose `href` is an\n // SVGAnimatedString the router (and shouldHandle) can't work with.\n if (!(a instanceof HTMLAnchorElement)) continue;\n\n const href = a.getAttribute('href');\n\n if (!href) continue;\n\n /**\n * Links the SPA's router never handles are handled natively by the\n * browser instead (new tab/window via `target`, download dialog,\n * `mailto:`/`tel:`, cross-origin, `rel=\"external\"`). Clicking them in a\n * test can't change `currentURL()`, so they'd always be reported as\n * failed navigations.\n *\n * `shouldHandle` is the predicate ember-primitives' @properLinks uses to\n * decide whether the router handles a click, so the crawler visits\n * exactly the set of links the router would. The fabricated click event\n * carries the \"plain left click\" defaults (button 0, no modifier keys).\n */\n if (!shouldHandle(window.location.href, a, new MouseEvent('click'))) {\n continue;\n }\n\n const current = new URL(currentURL(), window.location.origin);\n const url = new URL(href, current);\n const withoutDomain = `${url.pathname}${url.search}${url.hash}`;\n\n results.push({\n href: withoutDomain,\n original: href,\n selector: `a[href=\"${href}\"]`,\n });\n }\n\n return results;\n}\n\nconst assert = QUnit.assert;\n\nexport async function visitAllLinks(\n callback?: (url: string) => void | Promise<void>,\n knownRedirects?: Record<string, string>,\n) {\n /**\n * string of \"on::target\"\n */\n const visited = new Set();\n let returnTo = '/';\n\n await visit(returnTo);\n\n const inAppLinks = findInAppLinks();\n const queue: (InAppLink | { changeReturnTo: string })[] = [...inAppLinks];\n\n const ctx = getContext() as unknown as { owner: Owner };\n const router = ctx?.owner?.lookup('service:router') as RouterService;\n\n debugAssert(`Could not find the router service`, router);\n\n const rootURL = router.rootURL;\n\n while (queue.length > 0) {\n const toVisit = queue.shift();\n\n debugAssert(`Queue entries cannot be falsey`, toVisit);\n\n if ('changeReturnTo' in toVisit) {\n returnTo = toVisit.changeReturnTo;\n continue;\n }\n\n // In-page links are on the page we're already on.\n // As long as we haven't already encountered an error,\n // this is silly to check.\n if (toVisit.original.startsWith('#')) {\n continue;\n }\n\n const [nonHashPart] = toVisit.href.split('#');\n\n // This was our first page, we've already been here\n if (nonHashPart === '/') {\n continue;\n }\n\n const key = `${currentURL()}::${nonHashPart}`;\n\n if (visited.has(key)) continue;\n\n const result = router.recognize(toVisit.href);\n\n if (!result) {\n assert.ok(\n true,\n `${toVisit.href} on page ${returnTo} is not recognized by this app and will be skipped`,\n );\n continue;\n }\n\n await visit(returnTo);\n\n const link = find(toVisit.selector);\n\n debugAssert(`link exists via selector \\`${toVisit.selector}\\``, link);\n\n await click(link);\n\n const current =\n rootURL.replace(/\\/$/, '') + '/' + currentURL().replace(/^\\//, '');\n const expected = knownRedirects?.[toVisit.href] ?? toVisit.href;\n\n assert.pushResult({\n result: current.startsWith(expected),\n actual: current,\n expected: expected,\n message: `Navigation was successful: to:${toVisit.original}, from:${returnTo}`,\n });\n visited.add(key);\n\n if (callback) {\n await callback(toVisit.href);\n }\n\n const links = findInAppLinks();\n\n queue.push({ changeReturnTo: currentURL() });\n queue.push(...links);\n }\n\n return visited.size;\n}\n"],"names":["findInAppLinks","results","allAnchorsOnThePage","findAll","a","HTMLAnchorElement","href","getAttribute","shouldHandle","window","location","MouseEvent","current","URL","currentURL","origin","url","withoutDomain","pathname","search","hash","push","original","selector","assert","QUnit","visitAllLinks","callback","knownRedirects","visited","Set","returnTo","visit","inAppLinks","queue","ctx","getContext","router","owner","lookup","debugAssert","rootURL","length","toVisit","shift","changeReturnTo","startsWith","nonHashPart","split","key","has","result","recognize","ok","link","find","click","replace","expected","pushResult","actual","message","add","links","size"],"mappings":";;;;;AAoBA,SAASA,cAAcA,GAAgB;EACrC,MAAMC,OAAoB,GAAG,EAAE;AAE/B,EAAA,MAAMC,mBAAmB,GAAGC,OAAO,CAAC,GAAG,CAAC;AAExC,EAAA,KAAK,MAAMC,CAAC,I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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@universal-ember/test-support",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "The default blueprint for Embroider v2 addons.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ember-addon"
|
|
@@ -36,7 +36,8 @@
|
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@ember/test-helpers": "^4.0.4 || ^5.2.2",
|
|
39
|
-
"@embroider/addon-shim": "^1.8.7"
|
|
39
|
+
"@embroider/addon-shim": "^1.8.7",
|
|
40
|
+
"should-handle-link": "^1.2.2"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
43
|
"@babel/core": "^7.23.6",
|