Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 48b03ef

Browse files
committed
refactor: resolve Typescript discrepancies
1 parent 6fbf3f6 commit 48b03ef

File tree

12 files changed

+63
-34
lines changed

12 files changed

+63
-34
lines changed

addon/components/ui-alert-block/component.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ export default class UiAlertBlock extends Component {
6262
// eslint-disable-next-line ember/classic-decorator-hooks
6363
init() {
6464
super.init();
65-
66-
// @ts-expect-error this helper is weirdly typed, it describes the event and as a key of the source object?
6765
addListener(this.manager, MessageEvents.MESSAGE_ADDED, this, this.onMessageAdded);
6866
}
6967

@@ -72,7 +70,6 @@ export default class UiAlertBlock extends Component {
7270
*/
7371
// eslint-disable-next-line ember/no-component-lifecycle-hooks
7472
willDestroyElement() {
75-
// @ts-expect-error this helper is weirdly typed, it describes the event and as a key of the source object?
7673
removeListener(this.manager, MessageEvents.MESSAGE_ADDED, this, this.onMessageAdded);
7774
super.willDestroyElement();
7875
}

addon/components/ui-bread-crumbs/component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Component from '@ember/component';
55
import { layout, tagName } from '@ember-decorators/component';
66
import { inject as service } from '@ember/service';
77
import { computed } from '@ember/object';
8-
import { getOwner } from '@ember/application';
8+
import { getOwner } from '@nsf-open/ember-ui-foundation/utils';
99
import template from './template';
1010

1111
type FullBreadCrumb = Required<Exclude<BreadCrumb, string>>;
@@ -210,7 +210,7 @@ export default class UiBreadCrumbs extends Component {
210210
}
211211

212212
lookupController(name: string): IBreadCrumbController | null {
213-
const controller = getOwner(this).lookup(`controller:${name}`);
213+
const controller = getOwner(this)?.lookup<IBreadCrumbController>(`controller:${name}`);
214214

215215
if (controller) {
216216
return controller;

addon/utils/get-owner.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getOwner } from '@ember/application';
2+
3+
interface IOwner {
4+
resolveRegistration: <T>(name: string) => T;
5+
lookup: <T>(name: string) => T;
6+
}
7+
8+
/**
9+
* A typed version of @ember/application `getOwner` for internal use.
10+
*/
11+
export default function getTypedOwner(target: unknown): IOwner | null {
12+
return getOwner(target) as IOwner | null;
13+
}

addon/utils/get-root-element.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import getOwner from './get-owner';
12
import { runInDebug, warn } from '@ember/debug';
2-
import { getOwner } from '@ember/application';
33

44
const RootID = 'ui-positioning-root';
55

@@ -11,7 +11,9 @@ export default function getRootElement(context: unknown) {
1111
let root: HTMLElement | null = document.getElementById(RootID) || document.body;
1212

1313
runInDebug(() => {
14-
const config = getOwner(context)?.resolveRegistration('config:environment');
14+
const config = getOwner(context)?.resolveRegistration<{ environment: string } | undefined>(
15+
'config:environment'
16+
);
1517
const isTest = config?.environment === 'test';
1618

1719
if (isTest) {

addon/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export { default as extractErrorMessages } from './extract-error-messages';
55
export { default as keyNavigator } from './key-navigator';
66
export { default as maybeSplitString } from './maybe-split-string';
77
export { default as sortArrayWithRules } from './sort-array-with-rules';
8+
export { default as getOwner } from './get-owner';
89
export { buildFaClassNameString } from './font-awesome';
910
export { waitForTransitionEnd } from './transition-end';
1011
export { addAriaAttribute, removeAriaAttribute, getAriaAttributeValues } from './aria';

addon/utils/optional-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type ComputedProperty from '@ember/object/computed';
22
import type Service from '@ember/service';
3+
import getOwner from './get-owner';
34
import { computed } from '@ember/object';
4-
import { getOwner } from '@ember/application';
55

66
/**
77
* A computed macro that can be used to lazily inject a service and
@@ -10,7 +10,7 @@ import { getOwner } from '@ember/application';
1010
export default function optionalService(name?: string): ComputedProperty<Service | undefined> {
1111
return computed({
1212
get(this: unknown, propertyName: string) {
13-
return getOwner(this).lookup(`service:${name || propertyName}`);
13+
return getOwner(this)?.lookup<Service | undefined>(`service:${name || propertyName}`);
1414
},
1515
});
1616
}

tests/acceptance/ui-bread-crumbs-test.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
import type { IBreadCrumbController } from '@nsf-open/ember-ui-foundation/constants';
2+
import type { TestContext } from '@ember/test-helpers';
3+
14
import { module, test } from 'qunit';
25
import { setupApplicationTest } from 'ember-qunit';
36
import { visit } from '@ember/test-helpers';
47

58
module('Acceptance | Component | ui-bread-crumbs', function (hooks) {
69
setupApplicationTest(hooks);
710

11+
function lookupController(owner: TestContext['owner'], fullName: string): IBreadCrumbController {
12+
return owner.lookup(fullName) as IBreadCrumbController;
13+
}
14+
815
function nthCrumb(idx: number, anchor = false) {
916
return `.breadcrumb li:nth-child(${idx})${anchor ? ' a' : ''}`;
1017
}
@@ -39,13 +46,13 @@ module('Acceptance | Component | ui-bread-crumbs', function (hooks) {
3946
});
4047

4148
test('it does not render an empty ordered list', async function (assert) {
42-
this.owner.lookup('controller:application').breadCrumb = undefined;
49+
lookupController(this.owner, 'controller:application').breadCrumb = undefined;
4350
await visit('/');
4451
assert.dom('.breadcrumb').doesNotExist();
4552
});
4653

4754
test('it filters out breadcrumbs with no label text', async function (assert) {
48-
this.owner.lookup('controller:playground').breadCrumb = { label: '' };
55+
lookupController(this.owner, 'controller:playground').breadCrumb = { label: '' };
4956

5057
await visit('/playground');
5158

@@ -54,14 +61,20 @@ module('Acceptance | Component | ui-bread-crumbs', function (hooks) {
5461
});
5562

5663
test('it supports a breadcrumb being able to "rewind", to remove, prior crumbs', async function (assert) {
57-
this.owner.lookup('controller:playground').breadCrumb = { label: 'Foobar', rewind: 1 };
64+
lookupController(this.owner, 'controller:playground').breadCrumb = {
65+
label: 'Foobar',
66+
rewind: 1,
67+
};
5868

5969
await visit('/playground');
6070

6171
assert.dom(nthCrumb(1)).hasText('Foobar');
6272
assert.dom(nthCrumb(2)).doesNotExist();
6373

64-
this.owner.lookup('controller:artists.artist').breadCrumb = { label: 'Baz', rewind: -1 };
74+
lookupController(this.owner, 'controller:artists.artist').breadCrumb = {
75+
label: 'Baz',
76+
rewind: -1,
77+
};
6578

6679
await visit('/artists/queen');
6780

@@ -70,8 +83,8 @@ module('Acceptance | Component | ui-bread-crumbs', function (hooks) {
7083
});
7184

7285
test('it support a breadcrumb with fully custom href and target', async function (assert) {
73-
this.owner.lookup('controller:playground').breadCrumb = undefined;
74-
this.owner.lookup('controller:playground').breadCrumbs = [
86+
lookupController(this.owner, 'controller:playground').breadCrumb = undefined;
87+
lookupController(this.owner, 'controller:playground').breadCrumbs = [
7588
{ label: 'Search', href: 'https://www.google.com' },
7689
{ label: 'Playground' },
7790
];
@@ -83,7 +96,7 @@ module('Acceptance | Component | ui-bread-crumbs', function (hooks) {
8396
assert.dom(nthCrumb(2, true)).hasAttribute('target', '_self');
8497
assert.dom(nthCrumb(4)).doesNotExist();
8598

86-
this.owner.lookup('controller:artists').breadCrumb = {
99+
lookupController(this.owner, 'controller:artists').breadCrumb = {
87100
label: 'Search More',
88101
href: 'https://www.bing.com',
89102
target: '_blank',

tests/dummy/app/config/environment.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ declare const config: {
88
environment: string;
99
modulePrefix: string;
1010
podModulePrefix: string;
11-
locationType: string;
11+
locationType: 'auto' | 'history' | 'hash' | 'none';
1212
rootURL: string;
1313
APP: Record<string, unknown>;
1414
};

tests/helpers/lookup-component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ export default function lookupComponent<C extends Component = Component>(
99
context: TestContext,
1010
id: string
1111
): C {
12-
return context.owner.lookup('-view-registry:main')[id];
12+
const registry = context.owner.lookup('-view-registry:main') as Record<string, C>;
13+
return registry[id];
1314
}

tests/integration/components/ui-modal-test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ module('Integration | Component | ui-modal', function (hooks) {
106106
});
107107

108108
test('it can be opened and closed via the modal service', async function (assert) {
109-
const modal: ModalService = this.owner.lookup('service:modal');
109+
const modal = this.owner.lookup('service:modal') as ModalService;
110110

111111
// language=handlebars
112112
await render(hbs`
@@ -132,7 +132,7 @@ module('Integration | Component | ui-modal', function (hooks) {
132132
});
133133

134134
test('it can be dynamically passed data and a title via the modal service', async function (assert) {
135-
const modal: ModalService = this.owner.lookup('service:modal');
135+
const modal = this.owner.lookup('service:modal') as ModalService;
136136

137137
// language=handlebars
138138
await render(hbs`
@@ -149,7 +149,7 @@ module('Integration | Component | ui-modal', function (hooks) {
149149
});
150150

151151
test('it will close one modal when another is requested to be opened', async function (assert) {
152-
const modal: ModalService = this.owner.lookup('service:modal');
152+
const modal = this.owner.lookup('service:modal') as ModalService;
153153

154154
// language=handlebars
155155
await render(hbs`
@@ -196,7 +196,7 @@ module('Integration | Component | ui-modal', function (hooks) {
196196
});
197197

198198
test('it allows an open modal to cancel the opening of a new modal', async function (assert) {
199-
const modal: ModalService = this.owner.lookup('service:modal');
199+
const modal = this.owner.lookup('service:modal') as ModalService;
200200

201201
this.set('canHideModal', function () {
202202
assert.step('blocking');

0 commit comments

Comments
 (0)