-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from universal-ember/add-visitAll
Add visitAllLinks
- Loading branch information
Showing
8 changed files
with
370 additions
and
526 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
|
||
<h2 id="title">Welcome to Ember</h2> | ||
|
||
{{outlet}} | ||
{{outlet}} | ||
|
||
<a href="/foo">here</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { visitAllLinks } from './routing/visit-all.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |