Skip to content

Commit

Permalink
Add some type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Dec 27, 2023
1 parent e6cd893 commit ee2201b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion addon/src/services/page-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];

Expand Down
46 changes: 44 additions & 2 deletions test-types/index.ts
Original file line number Diff line number Diff line change
@@ -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<Helper<any>>();
let instance = new pageTitle({} as Owner);

expectTypeOf(instance).toMatchTypeOf<Helper<any>>();
expectTypeOf<ReturnType<typeof instance['compute']>>().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<Service>();
expectTypeOf(service.tokens).toBeArray();
expectTypeOf(service.tokens[0]).toMatchTypeOf<object | undefined>();
expectTypeOf<NonNullable<typeof service.tokens[0]>>()
.toMatchTypeOf<{
id: string, title?: string,
separator?: string;
prepend?: boolean;
replace?: boolean;
front?: unknown;
}>();

expectTypeOf<PageTitleService['tokens']>().toBeArray();
expectTypeOf<PageTitleService['sortedTokens']>().toBeArray();
expectTypeOf<ReturnType<PageTitleService['toString']>>().toBeString();
expectTypeOf<PageTitleService['titleDidUpdate']>().toMatchTypeOf<(title: string) => void>();
expectTypeOf<PageTitleService['remove']>().toMatchTypeOf<(id: string) => void>();

expectTypeOf<PageTitleService['push']>()
.toMatchTypeOf<(token: { id: string }) => void>();
expectTypeOf<PageTitleService['push']>()
.toMatchTypeOf<(token: { id: string, title: string }) => void>();

// @ts-expect-error
expectTypeOf<PageTitleService['push']>().toMatchTypeOf<(token: undefined) => void>();

0 comments on commit ee2201b

Please sign in to comment.