-
-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanup legacy code #1519
Open
chancancode
wants to merge
2
commits into
master
Choose a base branch
from
cleanup-legacy-rebased
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Cleanup legacy code #1519
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -4,22 +4,17 @@ import { | |
isTestContext, | ||
type TestContext, | ||
} from './setup-context.ts'; | ||
import global from './global.ts'; | ||
import hasEmberVersion from './has-ember-version.ts'; | ||
import settled from './settled.ts'; | ||
import getTestMetadata from './test-metadata.ts'; | ||
import { runHooks } from './helper-hooks.ts'; | ||
import type Router from '@ember/routing/router'; | ||
import type RouterService from '@ember/routing/router-service'; | ||
import { assert } from '@ember/debug'; | ||
|
||
export interface ApplicationTestContext extends TestContext { | ||
element?: Element | null; | ||
} | ||
|
||
const CAN_USE_ROUTER_EVENTS = hasEmberVersion(3, 6); | ||
let routerTransitionsPending: boolean | null = null; | ||
const ROUTER = new WeakMap(); | ||
const HAS_SETUP_ROUTER = new WeakMap(); | ||
|
||
// eslint-disable-next-line require-jsdoc | ||
|
@@ -36,33 +31,7 @@ export function isApplicationTestContext( | |
@returns {(boolean|null)} if there are pending transitions | ||
*/ | ||
export function hasPendingTransitions(): boolean | null { | ||
if (CAN_USE_ROUTER_EVENTS) { | ||
return routerTransitionsPending; | ||
} | ||
|
||
const context = getContext(); | ||
|
||
// there is no current context, we cannot check | ||
if (context === undefined) { | ||
return null; | ||
} | ||
|
||
const router = ROUTER.get(context); | ||
|
||
if (router === undefined) { | ||
// if there is no router (e.g. no `visit` calls made yet), we cannot | ||
// check for pending transitions but this is explicitly not an error | ||
// condition | ||
return null; | ||
} | ||
|
||
const routerMicrolib = router._routerMicrolib || router.router; | ||
|
||
if (routerMicrolib === undefined) { | ||
return null; | ||
} | ||
|
||
return !!routerMicrolib.activeTransition; | ||
return routerTransitionsPending; | ||
} | ||
|
||
/** | ||
|
@@ -88,29 +57,19 @@ export function setupRouterSettlednessTracking() { | |
HAS_SETUP_ROUTER.set(context, true); | ||
|
||
const { owner } = context; | ||
let router: Router | RouterService; | ||
if (CAN_USE_ROUTER_EVENTS) { | ||
// SAFETY: unfortunately we cannot `assert` here at present because the | ||
// class is not exported, only the type, since it is not designed to be | ||
// sub-classed. The most we can do at present is assert that it at least | ||
// *exists* and assume that if it does exist, it is bound correctly. | ||
const routerService = owner.lookup('service:router'); | ||
assert('router service is not set up correctly', !!routerService); | ||
router = routerService as RouterService; | ||
|
||
// track pending transitions via the public routeWillChange / routeDidChange APIs | ||
// routeWillChange can fire many times and is only useful to know when we have _started_ | ||
// transitioning, we can then use routeDidChange to signal that the transition has settled | ||
router.on('routeWillChange', () => (routerTransitionsPending = true)); | ||
router.on('routeDidChange', () => (routerTransitionsPending = false)); | ||
} else { | ||
// SAFETY: similarly, this cast cannot be made safer because on the versions | ||
// where we fall into this path, this is *also* not an exported class. | ||
const mainRouter = owner.lookup('router:main'); | ||
assert('router:main is not available', !!mainRouter); | ||
router = mainRouter as Router; | ||
ROUTER.set(context, router); | ||
} | ||
|
||
// SAFETY: unfortunately we cannot `assert` here at present because the | ||
// class is not exported, only the type, since it is not designed to be | ||
// sub-classed. The most we can do at present is assert that it at least | ||
// *exists* and assume that if it does exist, it is bound correctly. | ||
const router = owner.lookup('service:router') as RouterService; | ||
assert('router service is not set up correctly', !!router); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ..and this hunk, should be the only non-trivial modification (even then this is still pretty trivial, just following the lead of the linter and consolidated some variables) |
||
|
||
// track pending transitions via the public routeWillChange / routeDidChange APIs | ||
// routeWillChange can fire many times and is only useful to know when we have _started_ | ||
// transitioning, we can then use routeDidChange to signal that the transition has settled | ||
router.on('routeWillChange', () => (routerTransitionsPending = true)); | ||
router.on('routeDidChange', () => (routerTransitionsPending = false)); | ||
|
||
// hook into teardown to reset local settledness state | ||
const ORIGINAL_WILL_DESTROY = router.willDestroy; | ||
|
@@ -168,13 +127,7 @@ export function visit( | |
return visitResult; | ||
}) | ||
.then(() => { | ||
if (global.EmberENV._APPLICATION_TEMPLATE_WRAPPER !== false) { | ||
context.element = document.querySelector( | ||
'#ember-testing > .ember-view', | ||
); | ||
} else { | ||
context.element = document.querySelector('#ember-testing'); | ||
} | ||
context.element = document.querySelector('#ember-testing'); | ||
}) | ||
.then(settled) | ||
.then(() => { | ||
|
@@ -203,8 +156,6 @@ export function currentRouteName(): string { | |
return currentRouteName; | ||
} | ||
|
||
const HAS_CURRENT_URL_ON_ROUTER = hasEmberVersion(2, 13); | ||
|
||
/** | ||
@public | ||
@returns {string} the applications current url | ||
|
@@ -218,30 +169,22 @@ export function currentURL(): string { | |
} | ||
|
||
const router = context.owner.lookup('router:main') as any; | ||
const routerCurrentURL = router.currentURL; | ||
|
||
// SAFETY: this path is a lie for the sake of the public-facing types. The | ||
// framework itself sees this path, but users never do. A user who calls the | ||
// API without calling `visit()` will see an `UnrecognizedURLError`, rather | ||
// than getting back `null`. | ||
if (routerCurrentURL === null) { | ||
return routerCurrentURL as never as string; | ||
} | ||
|
||
if (HAS_CURRENT_URL_ON_ROUTER) { | ||
const routerCurrentURL = router.currentURL; | ||
|
||
// SAFETY: this path is a lie for the sake of the public-facing types. The | ||
// framework itself sees this path, but users never do. A user who calls the | ||
// API without calling `visit()` will see an `UnrecognizedURLError`, rather | ||
// than getting back `null`. | ||
if (routerCurrentURL === null) { | ||
return routerCurrentURL as never as string; | ||
} | ||
|
||
assert( | ||
`currentUrl should be a string, but was ${typeof routerCurrentURL}`, | ||
typeof routerCurrentURL === 'string', | ||
); | ||
assert( | ||
`currentUrl should be a string, but was ${typeof routerCurrentURL}`, | ||
typeof routerCurrentURL === 'string', | ||
); | ||
|
||
return routerCurrentURL; | ||
} else { | ||
// SAFETY: this is *positively ancient* and should probably be removed at | ||
// some point; old routers which don't have `currentURL` *should* have a | ||
// `location` with `getURL()` per the docs for 2.12. | ||
return (router.location as any).getURL(); | ||
} | ||
return routerCurrentURL; | ||
} | ||
|
||
/** | ||
|
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this..