From ee2201bbeaff7ed00cc01e411d458497295e7911 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Wed, 27 Dec 2023 12:52:13 -0500 Subject: [PATCH] Add some type tests --- addon/src/services/page-title.ts | 2 +- test-types/index.ts | 46 ++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/addon/src/services/page-title.ts b/addon/src/services/page-title.ts index 26a1252..f137f57 100644 --- a/addon/src/services/page-title.ts +++ b/addon/src/services/page-title.ts @@ -44,7 +44,7 @@ export default class PageTitleService extends Service { // in fastboot context "document" is instance of // ember-fastboot/simple-dom document - @service('-document') declare document: FastBootDocument; + @service('-document') private declare document: FastBootDocument; tokens: PageTitleToken[] = []; diff --git a/test-types/index.ts b/test-types/index.ts index 95a2287..7527f80 100644 --- a/test-types/index.ts +++ b/test-types/index.ts @@ -1,9 +1,51 @@ import 'ember-source/types'; import '@glint/environment-ember-loose'; -import Helper from '@ember/component/helper'; import { expectTypeOf } from 'expect-type'; +import type Owner from '@ember/owner'; + +/************************ + * The Helper + ************************/ +import Helper from '@ember/component/helper'; import { pageTitle } from 'ember-page-title'; -expectTypeOf(new pageTitle()).toMatchTypeOf>(); +let instance = new pageTitle({} as Owner); + +expectTypeOf(instance).toMatchTypeOf>(); +expectTypeOf>().toBeString(/* an empty string, but still a string */); + +/************************ + * The Service + ************************/ +import PageTitleService from 'ember-page-title/services/page-title'; +import type Service from '@ember/service'; + +let service = new PageTitleService({} as Owner); + +expectTypeOf(service).toMatchTypeOf(); +expectTypeOf(service.tokens).toBeArray(); +expectTypeOf(service.tokens[0]).toMatchTypeOf(); +expectTypeOf>() + .toMatchTypeOf<{ + id: string, title?: string, + separator?: string; + prepend?: boolean; + replace?: boolean; + front?: unknown; + }>(); + +expectTypeOf().toBeArray(); +expectTypeOf().toBeArray(); +expectTypeOf>().toBeString(); +expectTypeOf().toMatchTypeOf<(title: string) => void>(); +expectTypeOf().toMatchTypeOf<(id: string) => void>(); + +expectTypeOf() + .toMatchTypeOf<(token: { id: string }) => void>(); +expectTypeOf() + .toMatchTypeOf<(token: { id: string, title: string }) => void>(); + +// @ts-expect-error +expectTypeOf().toMatchTypeOf<(token: undefined) => void>();