Skip to content

Commit

Permalink
Add getService (#9)
Browse files Browse the repository at this point in the history
* Add getService

* eh

* type tests
  • Loading branch information
NullVoxPopuli authored Mar 13, 2024
1 parent f939693 commit c137c02
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"edition": "octane"
},
"dependencies": {
"ember-primitives": "^0.11.3"
"ember-primitives": "^0.11.3",
"expect-type": "^0.18.0"
}
}
17 changes: 17 additions & 0 deletions test-app/tests/acceptance/get-service-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { expectTypeOf } from 'expect-type';

import { getService } from '@universal-ember/test-support';
import RouterService from '@ember/routing/router-service';

module('getService', function (hooks) {
setupApplicationTest(hooks);

test('gets the service', async function (assert) {
const router = getService('router');
expectTypeOf(router).toEqualTypeOf<RouterService>();

assert.ok(router);
});
});
19 changes: 19 additions & 0 deletions test-support/src/container/get-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { assert } from '@ember/debug';
import type { Registry as ServiceRegistry } from '@ember/service';
import { getContext } from '@ember/test-helpers';
import { getOwner } from '@ember/owner';
import type Owner from '@ember/owner';

export function getService<ServiceName extends keyof ServiceRegistry>(
name: ServiceName,
): ServiceRegistry[ServiceName] {
const context = getContext() as { owner?: Owner } | undefined;
assert('Could not determine the context for the test', context);

const owner = context.owner || getOwner(context);
assert('Could not find the owner on the context', owner);

const service = owner.lookup(`service:${name}`);

return service;
}
1 change: 1 addition & 0 deletions test-support/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { visitAllLinks } from './routing/visit-all.ts';
export { getService } from './container/get-service.ts';

0 comments on commit c137c02

Please sign in to comment.