Skip to content

Commit

Permalink
Merge pull request #4 from universal-ember/add-visitAll
Browse files Browse the repository at this point in the history
Add visitAllLinks
  • Loading branch information
NullVoxPopuli authored Mar 4, 2024
2 parents a3c3d59 + 440ef26 commit 4ecb083
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 526 deletions.
795 changes: 275 additions & 520 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions test-app/app/router.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import EmberRouter from '@ember/routing/router';
import config from 'test-app/config/environment';

import { properLinks } from 'ember-primitives/proper-links';

@properLinks
export default class Router extends EmberRouter {
location = config.locationType;
rootURL = config.rootURL;
}

Router.map(function () {
// Add route declarations here
this.route('*any');
});
4 changes: 3 additions & 1 deletion test-app/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

<h2 id="title">Welcome to Ember</h2>

{{outlet}}
{{outlet}}

<a href="/foo">here</a>
9 changes: 6 additions & 3 deletions test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"lint:js:fix": "eslint . --fix",
"lint:types": "tsc --noEmit",
"start": "ember serve",
"test": "concurrently \"npm:lint\" \"npm:test:*\" --names \"lint,test:\"",
"test": "ember test",
"test:ember": "ember test"
},
"devDependencies": {
Expand Down Expand Up @@ -66,6 +66,7 @@
"@types/rsvp": "^4.0.9",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"@universal-ember/test-support": "workspace:*",
"broccoli-asset-rev": "^3.0.0",
"concurrently": "^8.2.2",
"ember-auto-import": "^2.7.2",
Expand Down Expand Up @@ -102,7 +103,6 @@
"stylelint": "^15.11.0",
"stylelint-config-standard": "^34.0.0",
"stylelint-prettier": "^4.1.0",
"test-support": "workspace:*",
"tracked-built-ins": "^3.3.0",
"typescript": "^5.3.3",
"webpack": "^5.89.0"
Expand All @@ -112,5 +112,8 @@
},
"ember": {
"edition": "octane"
},
"dependencies": {
"ember-primitives": "^0.11.3"
}
}
}
12 changes: 12 additions & 0 deletions test-app/tests/acceptance/visit-all-links-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';

import { visitAllLinks } from '@universal-ember/test-support';

module('All Links', function (hooks) {
setupApplicationTest(hooks);

test('are visitable without error', async function () {
await visitAllLinks();
});
});
11 changes: 9 additions & 2 deletions test-support/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "test-support",
"name": "@universal-ember/test-support",
"version": "0.0.0",
"description": "The default blueprint for Embroider v2 addons.",
"keywords": [
Expand Down Expand Up @@ -49,6 +49,7 @@
"test": "echo 'A v2 addon does not have tests, run tests in test-app'"
},
"dependencies": {
"@ember/test-helpers": "^3.2.1",
"@embroider/addon-shim": "^1.8.7",
"decorator-transforms": "^1.0.1"
},
Expand Down Expand Up @@ -84,6 +85,7 @@
"@types/ember__template": "^4.0.5",
"@types/ember__test": "^4.0.5",
"@types/ember__utils": "^4.0.6",
"@types/qunit": "^2.19.10",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"babel-plugin-ember-template-compilation": "^2.2.1",
Expand All @@ -96,6 +98,7 @@
"eslint-plugin-prettier": "^5.0.1",
"prettier": "^3.1.1",
"prettier-plugin-ember-template-tag": "^1.1.0",
"qunit": "^2.20.1",
"rollup": "^4.9.1",
"rollup-plugin-copy": "^3.5.0",
"typescript": "^5.3.3"
Expand All @@ -109,6 +112,10 @@
"ember-addon": {
"version": 2,
"type": "addon",
"main": "addon-main.cjs"
"main": "addon-main.cjs",
"app-js": {}
},
"peerDependencies": {
"qunit": "^2.20.1"
}
}
1 change: 1 addition & 0 deletions test-support/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { visitAllLinks } from './routing/visit-all.ts';
60 changes: 60 additions & 0 deletions test-support/src/routing/visit-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { assert as debugAssert } from '@ember/debug';
import { click, currentURL, findAll } from '@ember/test-helpers';
import { visit } from '@ember/test-helpers';
import { find } from '@ember/test-helpers';
import QUnit from 'qunit';

function findInAppLinks(): string[] {
return (findAll('a')
?.map((link) => link.getAttribute('href'))
?.filter((href) => !href?.startsWith('http'))
.filter(Boolean) || []) as string[];
}

const assert = QUnit.assert;

export async function visitAllLinks() {
/**
* string of "on::target"
*/
const visited = new Set();
let returnTo = '/';

await visit(returnTo);

const inAppLinks = findInAppLinks();
const queue: (string | { changeReturnTo: string })[] = [...inAppLinks];

while (queue.length > 0) {
const toVisit = queue.shift();

debugAssert(`Queue entries cannot be falsey`, toVisit);

if (typeof toVisit === 'object' && toVisit !== null) {
returnTo = toVisit.changeReturnTo;
continue;
}

const key = `${currentURL()}::${toVisit}`;

if (visited.has(key)) continue;

await visit(returnTo);

const link = find(`a[href="${toVisit}"]`);

debugAssert(`link exists with ${toVisit}`, link);

await click(link);
assert.ok(
currentURL().startsWith(toVisit),
`Navigation was successful: to:${toVisit}, from:${returnTo}`,
);
visited.add(key);

const links = findInAppLinks();

queue.push({ changeReturnTo: currentURL() });
queue.push(...links);
}
}

0 comments on commit 4ecb083

Please sign in to comment.