Skip to content

Commit 5f7dbe2

Browse files
committed
Add some type tests
1 parent 845bef6 commit 5f7dbe2

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

addon/src/services/page-title.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default class PageTitleService extends Service {
4444

4545
// in fastboot context "document" is instance of
4646
// ember-fastboot/simple-dom document
47-
@service('-document') declare document: FastBootDocument;
47+
@service('-document') private declare document: FastBootDocument;
4848

4949
tokens: PageTitleToken[] = [];
5050

test-types/index.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
11
import 'ember-source/types';
22
import '@glint/environment-ember-loose';
33

4-
import Helper from '@ember/component/helper';
54

65
import { expectTypeOf } from 'expect-type';
6+
import type Owner from '@ember/owner';
7+
8+
/************************
9+
* The Helper
10+
************************/
11+
import Helper from '@ember/component/helper';
712
import { pageTitle } from 'ember-page-title';
813

9-
expectTypeOf(new pageTitle()).toMatchTypeOf<Helper<any>>();
14+
let instance = new pageTitle({} as Owner);
15+
16+
expectTypeOf(instance).toMatchTypeOf<Helper<any>>();
17+
expectTypeOf<ReturnType<typeof instance['compute']>>().toBeString(/* an empty string, but still a string */);
18+
19+
/************************
20+
* The Service
21+
************************/
22+
import PageTitleService from 'ember-page-title/services/page-title';
23+
import type Service from '@ember/service';
24+
25+
let service = new PageTitleService({} as Owner);
26+
27+
expectTypeOf(service).toMatchTypeOf<Service>();
28+
expectTypeOf(service.tokens).toBeArray();
29+
expectTypeOf(service.tokens[0]).toMatchTypeOf<object | undefined>();
30+
expectTypeOf<NonNullable<typeof service.tokens[0]>>()
31+
.toMatchTypeOf<{
32+
id: string, title?: string,
33+
separator?: string;
34+
prepend?: boolean;
35+
replace?: boolean;
36+
front?: unknown;
37+
}>();
38+
39+
expectTypeOf<PageTitleService['tokens']>().toBeArray();
40+
expectTypeOf<PageTitleService['sortedTokens']>().toBeArray();
41+
expectTypeOf<ReturnType<PageTitleService['toString']>>().toBeString();
42+
expectTypeOf<PageTitleService['titleDidUpdate']>().toMatchTypeOf<(title: string) => void>();
43+
expectTypeOf<PageTitleService['remove']>().toMatchTypeOf<(id: string) => void>();
44+
45+
expectTypeOf<PageTitleService['push']>()
46+
.toMatchTypeOf<(token: { id: string }) => void>();
47+
expectTypeOf<PageTitleService['push']>()
48+
.toMatchTypeOf<(token: { id: string, title: string }) => void>();
49+
50+
// @ts-expect-error
51+
expectTypeOf<PageTitleService['push']>().toMatchTypeOf<(token: undefined) => void>();

0 commit comments

Comments
 (0)