From c137c02ee9c0e0b38a30065e1c7601fd4e337ea7 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Tue, 12 Mar 2024 21:54:05 -0400 Subject: [PATCH] Add getService (#9) * Add getService * eh * type tests --- pnpm-lock.yaml | 8 ++++++++ test-app/package.json | 3 ++- test-app/tests/acceptance/get-service-test.ts | 17 +++++++++++++++++ test-support/src/container/get-service.ts | 19 +++++++++++++++++++ test-support/src/index.ts | 1 + 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test-app/tests/acceptance/get-service-test.ts create mode 100644 test-support/src/container/get-service.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 85014d4..dd325c1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -32,6 +32,9 @@ importers: ember-primitives: specifier: ^0.11.3 version: 0.11.3(@babel/core@7.24.0)(@ember/test-helpers@3.3.0)(@ember/test-waiters@3.1.0)(@glimmer/component@1.1.2)(@glimmer/tracking@1.1.2)(@glint/environment-ember-loose@1.3.0)(@glint/template@1.3.0)(ember-modifier@4.1.0)(ember-resources@7.0.0)(ember-source@5.6.0) + expect-type: + specifier: ^0.18.0 + version: 0.18.0 devDependencies: '@babel/core': specifier: ^7.23.7 @@ -8501,6 +8504,11 @@ packages: homedir-polyfill: 1.0.3 dev: true + /expect-type@0.18.0: + resolution: {integrity: sha512-xjKoyyDLoia2h1WF+vwV8AmEpQ0drGW0InRgyywAHyOC+XSPYMxGoMXSwPjXs46D8FgLmp32sHMd1KrVingDuQ==} + engines: {node: '>=12.0.0'} + dev: false + /express@4.18.3: resolution: {integrity: sha512-6VyCijWQ+9O7WuVMTRBTl+cjNNIzD5cY5mQ1WM8r/LEkI2u8EYpOotESNwzNlyCn3g+dmjKYI6BmNneSr/FSRw==} engines: {node: '>= 0.10.0'} diff --git a/test-app/package.json b/test-app/package.json index 9f5d364..d179ee3 100644 --- a/test-app/package.json +++ b/test-app/package.json @@ -114,6 +114,7 @@ "edition": "octane" }, "dependencies": { - "ember-primitives": "^0.11.3" + "ember-primitives": "^0.11.3", + "expect-type": "^0.18.0" } } diff --git a/test-app/tests/acceptance/get-service-test.ts b/test-app/tests/acceptance/get-service-test.ts new file mode 100644 index 0000000..9062f25 --- /dev/null +++ b/test-app/tests/acceptance/get-service-test.ts @@ -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(); + + assert.ok(router); + }); +}); diff --git a/test-support/src/container/get-service.ts b/test-support/src/container/get-service.ts new file mode 100644 index 0000000..c1177da --- /dev/null +++ b/test-support/src/container/get-service.ts @@ -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( + 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; +} diff --git a/test-support/src/index.ts b/test-support/src/index.ts index b863760..5674828 100644 --- a/test-support/src/index.ts +++ b/test-support/src/index.ts @@ -1 +1,2 @@ export { visitAllLinks } from './routing/visit-all.ts'; +export { getService } from './container/get-service.ts';