Skip to content
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

Add getService #9

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';