Skip to content

Commit fd10660

Browse files
committed
[CLEANUP] remove branching for Ember version < 4
Turns out that accounts for _all_ of the current usage.
1 parent 51ae99b commit fd10660

27 files changed

+235
-602
lines changed

addon/src/setup-application-context.ts

+28-78
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@ import {
44
isTestContext,
55
type TestContext,
66
} from './setup-context.ts';
7-
import hasEmberVersion from './has-ember-version.ts';
87
import settled from './settled.ts';
98
import getTestMetadata from './test-metadata.ts';
109
import { runHooks } from './helper-hooks.ts';
11-
import type Router from '@ember/routing/router';
1210
import type RouterService from '@ember/routing/router-service';
1311
import { assert } from '@ember/debug';
1412

1513
export interface ApplicationTestContext extends TestContext {
1614
element?: Element | null;
1715
}
1816

19-
const CAN_USE_ROUTER_EVENTS = hasEmberVersion(3, 6);
2017
let routerTransitionsPending: boolean | null = null;
21-
const ROUTER = new WeakMap();
2218
const HAS_SETUP_ROUTER = new WeakMap();
2319

2420
// eslint-disable-next-line require-jsdoc
@@ -35,33 +31,7 @@ export function isApplicationTestContext(
3531
@returns {(boolean|null)} if there are pending transitions
3632
*/
3733
export function hasPendingTransitions(): boolean | null {
38-
if (CAN_USE_ROUTER_EVENTS) {
39-
return routerTransitionsPending;
40-
}
41-
42-
const context = getContext();
43-
44-
// there is no current context, we cannot check
45-
if (context === undefined) {
46-
return null;
47-
}
48-
49-
const router = ROUTER.get(context);
50-
51-
if (router === undefined) {
52-
// if there is no router (e.g. no `visit` calls made yet), we cannot
53-
// check for pending transitions but this is explicitly not an error
54-
// condition
55-
return null;
56-
}
57-
58-
const routerMicrolib = router._routerMicrolib || router.router;
59-
60-
if (routerMicrolib === undefined) {
61-
return null;
62-
}
63-
64-
return !!routerMicrolib.activeTransition;
34+
return routerTransitionsPending;
6535
}
6636

6737
/**
@@ -87,29 +57,19 @@ export function setupRouterSettlednessTracking() {
8757
HAS_SETUP_ROUTER.set(context, true);
8858

8959
const { owner } = context;
90-
let router: Router | RouterService;
91-
if (CAN_USE_ROUTER_EVENTS) {
92-
// SAFETY: unfortunately we cannot `assert` here at present because the
93-
// class is not exported, only the type, since it is not designed to be
94-
// sub-classed. The most we can do at present is assert that it at least
95-
// *exists* and assume that if it does exist, it is bound correctly.
96-
const routerService = owner.lookup('service:router');
97-
assert('router service is not set up correctly', !!routerService);
98-
router = routerService as RouterService;
99-
100-
// track pending transitions via the public routeWillChange / routeDidChange APIs
101-
// routeWillChange can fire many times and is only useful to know when we have _started_
102-
// transitioning, we can then use routeDidChange to signal that the transition has settled
103-
router.on('routeWillChange', () => (routerTransitionsPending = true));
104-
router.on('routeDidChange', () => (routerTransitionsPending = false));
105-
} else {
106-
// SAFETY: similarly, this cast cannot be made safer because on the versions
107-
// where we fall into this path, this is *also* not an exported class.
108-
const mainRouter = owner.lookup('router:main');
109-
assert('router:main is not available', !!mainRouter);
110-
router = mainRouter as Router;
111-
ROUTER.set(context, router);
112-
}
60+
61+
// SAFETY: unfortunately we cannot `assert` here at present because the
62+
// class is not exported, only the type, since it is not designed to be
63+
// sub-classed. The most we can do at present is assert that it at least
64+
// *exists* and assume that if it does exist, it is bound correctly.
65+
const router = owner.lookup('service:router') as RouterService;
66+
assert('router service is not set up correctly', !!router);
67+
68+
// track pending transitions via the public routeWillChange / routeDidChange APIs
69+
// routeWillChange can fire many times and is only useful to know when we have _started_
70+
// transitioning, we can then use routeDidChange to signal that the transition has settled
71+
router.on('routeWillChange', () => (routerTransitionsPending = true));
72+
router.on('routeDidChange', () => (routerTransitionsPending = false));
11373

11474
// hook into teardown to reset local settledness state
11575
const ORIGINAL_WILL_DESTROY = router.willDestroy;
@@ -196,8 +156,6 @@ export function currentRouteName(): string {
196156
return currentRouteName;
197157
}
198158

199-
const HAS_CURRENT_URL_ON_ROUTER = hasEmberVersion(2, 13);
200-
201159
/**
202160
@public
203161
@returns {string} the applications current url
@@ -211,30 +169,22 @@ export function currentURL(): string {
211169
}
212170

213171
const router = context.owner.lookup('router:main') as any;
172+
const routerCurrentURL = router.currentURL;
173+
174+
// SAFETY: this path is a lie for the sake of the public-facing types. The
175+
// framework itself sees this path, but users never do. A user who calls the
176+
// API without calling `visit()` will see an `UnrecognizedURLError`, rather
177+
// than getting back `null`.
178+
if (routerCurrentURL === null) {
179+
return routerCurrentURL as never as string;
180+
}
214181

215-
if (HAS_CURRENT_URL_ON_ROUTER) {
216-
const routerCurrentURL = router.currentURL;
217-
218-
// SAFETY: this path is a lie for the sake of the public-facing types. The
219-
// framework itself sees this path, but users never do. A user who calls the
220-
// API without calling `visit()` will see an `UnrecognizedURLError`, rather
221-
// than getting back `null`.
222-
if (routerCurrentURL === null) {
223-
return routerCurrentURL as never as string;
224-
}
225-
226-
assert(
227-
`currentUrl should be a string, but was ${typeof routerCurrentURL}`,
228-
typeof routerCurrentURL === 'string',
229-
);
182+
assert(
183+
`currentUrl should be a string, but was ${typeof routerCurrentURL}`,
184+
typeof routerCurrentURL === 'string',
185+
);
230186

231-
return routerCurrentURL;
232-
} else {
233-
// SAFETY: this is *positively ancient* and should probably be removed at
234-
// some point; old routers which don't have `currentURL` *should* have a
235-
// `location` with `getURL()` per the docs for 2.12.
236-
return (router.location as any).getURL();
237-
}
187+
return routerCurrentURL;
238188
}
239189

240190
/**

addon/src/setup-rendering-context.ts

-15
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type { Owner } from './build-owner.ts';
1212
import getTestMetadata from './test-metadata.ts';
1313
import { assert } from '@ember/debug';
1414
import { runHooks } from './helper-hooks.ts';
15-
import hasEmberVersion from './has-ember-version.ts';
1615
import isComponent from './-internal/is-component.ts';
1716
import { ComponentRenderMap, SetUsage } from './setup-context.ts';
1817

@@ -187,20 +186,6 @@ export function render(
187186
};
188187
toplevelView.setOutletState(outletState);
189188

190-
// Ember's rendering engine is integration with the run loop so that when a run
191-
// loop starts, the rendering is scheduled to be done.
192-
//
193-
// Ember should be ensuring an instance on its own here (the act of
194-
// setting outletState should ensureInstance, since we know we need to
195-
// render), but on Ember < 3.23 that is not guaranteed.
196-
if (!hasEmberVersion(3, 23)) {
197-
// SAFETY: this was correct and type checked on the Ember v3 types, but
198-
// since the `run` namespace does not exist in Ember v4, this no longer
199-
// can be type checked. When (eventually) dropping support for Ember v3,
200-
// and therefore for versions before 3.23, this can be removed entirely.
201-
(run as any).backburner.ensureInstance();
202-
}
203-
204189
// returning settled here because the actual rendering does not happen until
205190
// the renderer detects it is dirty (which happens on backburner's end
206191
// hook), see the following implementation details:

test-app/tests/integration/rerender-test.js

-5
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,8 @@ import GlimmerComponent from '@glimmer/component';
1818
import { tracked } from '@glimmer/tracking';
1919
import { hbs } from 'ember-cli-htmlbars';
2020
import { module, test } from 'qunit';
21-
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
2221

2322
module('rerender real-world', function (hooks) {
24-
// this test requires Ember 3.25 in order to use lexically scoped values in templates
25-
if (!hasEmberVersion(3, 25)) {
26-
return;
27-
}
2823
hooks.beforeEach(async function () {
2924
await setupContext(this);
3025
await setupRenderingContext(this);

test-app/tests/integration/settled-test.js

-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
render,
1313
rerender,
1414
} from '@ember/test-helpers';
15-
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
1615
import { module, test } from 'qunit';
1716
import { hbs } from 'ember-cli-htmlbars';
1817
import Pretender from 'pretender';
@@ -119,10 +118,6 @@ const TestComponent5 = Component.extend({
119118
});
120119

121120
module('settled real-world scenarios', function (hooks) {
122-
if (!hasEmberVersion(2, 4)) {
123-
return;
124-
}
125-
126121
hooks.beforeEach(async function () {
127122
await setupContext(this);
128123
await setupRenderingContext(this);

test-app/tests/integration/setup-rendering-context-test.js

+38-43
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
} from '@ember/test-helpers';
1616
import templateOnly from '@ember/component/template-only';
1717

18-
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
1918
import { setResolverRegistry } from '../helpers/resolver';
2019
import { hbs } from 'ember-cli-htmlbars';
2120
import { precompileTemplate } from '@ember/template-compilation';
@@ -149,60 +148,56 @@ module('setupRenderingContext "real world"', function (hooks) {
149148
});
150149

151150
module('lexical scope access', function () {
152-
if (hasEmberVersion(3, 28)) {
153-
test('can render components passed as locals', async function (assert) {
154-
let add = helper(function ([first, second]) {
155-
return first + second;
156-
});
157-
158-
await render(
159-
precompileTemplate('{{add 1 3}}', {
160-
scope() {
161-
return { add };
162-
},
163-
})
164-
);
165-
166-
assert.equal(this.element.textContent, '4');
151+
test('can render components passed as locals', async function (assert) {
152+
let add = helper(function ([first, second]) {
153+
return first + second;
167154
});
168-
}
155+
156+
await render(
157+
precompileTemplate('{{add 1 3}}', {
158+
scope() {
159+
return { add };
160+
},
161+
})
162+
);
163+
164+
assert.equal(this.element.textContent, '4');
165+
});
169166
});
170167

171168
module('render with a component', function () {
172-
if (hasEmberVersion(3, 25)) {
173-
test('can render locally defined components', async function (assert) {
174-
class MyComponent extends GlimmerComponent {}
169+
test('can render locally defined components', async function (assert) {
170+
class MyComponent extends GlimmerComponent {}
175171

176-
setComponentTemplate(hbs`my name is {{@name}}`, MyComponent);
172+
setComponentTemplate(hbs`my name is {{@name}}`, MyComponent);
177173

178-
const somePerson = new (class {
179-
@tracked name = 'Zoey';
180-
})();
174+
const somePerson = new (class {
175+
@tracked name = 'Zoey';
176+
})();
181177

182-
const template = precompileTemplate(
183-
'<MyComponent @name={{somePerson.name}} />',
184-
{
185-
scope() {
186-
return {
187-
somePerson,
188-
MyComponent,
189-
};
190-
},
191-
}
192-
);
178+
const template = precompileTemplate(
179+
'<MyComponent @name={{somePerson.name}} />',
180+
{
181+
scope() {
182+
return {
183+
somePerson,
184+
MyComponent,
185+
};
186+
},
187+
}
188+
);
193189

194-
const component = setComponentTemplate(template, templateOnly());
190+
const component = setComponentTemplate(template, templateOnly());
195191

196-
await render(component);
192+
await render(component);
197193

198-
assert.equal(this.element.textContent, 'my name is Zoey');
194+
assert.equal(this.element.textContent, 'my name is Zoey');
199195

200-
somePerson.name = 'Tomster';
196+
somePerson.name = 'Tomster';
201197

202-
await rerender();
198+
await rerender();
203199

204-
assert.equal(this.element.textContent, 'my name is Tomster');
205-
});
206-
}
200+
assert.equal(this.element.textContent, 'my name is Tomster');
201+
});
207202
});
208203
});

test-app/tests/unit/dom/blur-test.js

-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
} from '@ember/test-helpers';
99
import { buildInstrumentedElement, insertElement } from '../../helpers/events';
1010
import { isEdge, isChrome } from '../../helpers/browser-detect';
11-
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
1211
import { createDescriptor } from 'dom-element-descriptors';
1312

1413
let focusSteps = ['focus', 'focusin'];
@@ -23,10 +22,6 @@ if (isChrome) {
2322
}
2423

2524
module('DOM Helper: blur', function (hooks) {
26-
if (!hasEmberVersion(2, 4)) {
27-
return;
28-
}
29-
3025
let context, elementWithFocus;
3126

3227
hooks.beforeEach(async function (assert) {

test-app/tests/unit/dom/click-test.js

-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
instrumentElement,
66
insertElement,
77
} from '../../helpers/events';
8-
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
98
import { isChrome } from '../../helpers/browser-detect';
109
import {
1110
registerHooks,
@@ -15,10 +14,6 @@ import {
1514
import { createDescriptor } from 'dom-element-descriptors';
1615

1716
module('DOM Helper: click', function (hooks) {
18-
if (!hasEmberVersion(2, 4)) {
19-
return;
20-
}
21-
2217
let context, element;
2318

2419
hooks.beforeEach(function () {

test-app/tests/unit/dom/double-click-test.js

-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
instrumentElement,
1111
insertElement,
1212
} from '../../helpers/events';
13-
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
1413
import {
1514
registerHooks,
1615
unregisterHooks,
@@ -33,10 +32,6 @@ if (isChrome) {
3332
}
3433

3534
module('DOM Helper: doubleClick', function (hooks) {
36-
if (!hasEmberVersion(2, 4)) {
37-
return;
38-
}
39-
4035
let context, element;
4136

4237
hooks.beforeEach(function () {

test-app/tests/unit/dom/fill-in-test.js

-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { module, test } from 'qunit';
22
import { fillIn, setupContext, teardownContext } from '@ember/test-helpers';
33
import { buildInstrumentedElement, insertElement } from '../../helpers/events';
44
import { isFirefox, isChrome } from '../../helpers/browser-detect';
5-
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
65
import {
76
registerHooks,
87
unregisterHooks,
@@ -17,10 +16,6 @@ if (isFirefox || isChrome) {
1716
}
1817

1918
module('DOM Helper: fillIn', function (hooks) {
20-
if (!hasEmberVersion(2, 4)) {
21-
return;
22-
}
23-
2419
let context, element;
2520

2621
hooks.beforeEach(function () {

0 commit comments

Comments
 (0)