1
- import { setComponentTemplate } from '@ember/component' ;
2
- import templateOnlyComponent from '@ember/component/template-only' ;
1
+ import AuToaster from ' @appuniversum/ember-appuniversum/components/au-toaster' ;
2
+ import type ToasterService from ' @appuniversum/ember-appuniversum/services/toaster' ;
3
+ import type {
4
+ CustomToastSignature ,
5
+ ToastOptions ,
6
+ } from ' @appuniversum/ember-appuniversum/services/toaster' ;
7
+ import type { TOC } from ' @ember/component/template-only' ;
8
+ import { on } from ' @ember/modifier' ;
3
9
import {
4
10
click ,
5
11
getRootElement ,
6
12
render ,
7
13
settled ,
8
14
waitUntil ,
15
+ type TestContext as BaseTestContext ,
9
16
} from ' @ember/test-helpers' ;
10
17
import { queryByText } from ' @testing-library/dom' ;
11
18
import { setupRenderingTest } from ' dummy/tests/helpers' ;
12
- import { hbs } from 'ember-cli-htmlbars' ;
13
19
import { module , test } from ' qunit' ;
14
20
15
21
const TOASTER = {
@@ -18,15 +24,19 @@ const TOASTER = {
18
24
CLOSE: ' [data-test-alert-close]' ,
19
25
};
20
26
27
+ interface TestContext extends BaseTestContext {
28
+ toaster: ToasterService ;
29
+ }
30
+
21
31
module (' Integration | Component | au-toaster' , function (hooks ) {
22
32
setupRenderingTest (hooks );
23
33
24
- hooks . beforeEach ( async function ( ) {
25
- this . toaster = this . owner . lookup ( 'service:toaster' ) ;
34
+ hooks .beforeEach < TestContext > (async function () {
35
+ this .toaster = this .owner .lookup (' service:toaster' ) as ToasterService ;
26
36
await renderToasterContainer ();
27
37
});
28
38
29
- test ( 'it only renders the container if toasts are present' , async function ( assert ) {
39
+ test < TestContext > (' it only renders the container if toasts are present' , async function (assert ) {
30
40
assert .dom (TOASTER .CONTAINER ).doesNotExist ();
31
41
32
42
this .toaster .notify (' Bar' , ' Foo' );
@@ -35,7 +45,7 @@ module('Integration | Component | au-toaster', function (hooks) {
35
45
assert .dom (TOASTER .CONTAINER ).exists ();
36
46
});
37
47
38
- test ( 'it has different methods to show styled toasts' , async function ( assert ) {
48
+ test < TestContext > (' it has different methods to show styled toasts' , async function (assert ) {
39
49
this .toaster .notify (' toast' , ' notify' );
40
50
this .toaster .success (' toast' , ' success' );
41
51
this .toaster .warning (' toast' , ' warning' );
@@ -48,48 +58,48 @@ module('Integration | Component | au-toaster', function (hooks) {
48
58
.exists ({ count: 5 }, ' it displays a toast for every method call' );
49
59
});
50
60
51
- test ( '`notify` displays the title and message' , async function ( assert ) {
61
+ test < TestContext > (' `notify` displays the title and message' , async function (assert ) {
52
62
this .toaster .notify (' toast' , ' notify' );
53
63
await settled ();
54
64
55
65
assert .dom (TOASTER .TOAST ).containsText (' notify toast' );
56
66
});
57
67
58
- test ( '`success` displays the title and message' , async function ( assert ) {
68
+ test < TestContext > (' `success` displays the title and message' , async function (assert ) {
59
69
this .toaster .success (' toast' , ' success' );
60
70
await settled ();
61
71
62
72
assert .dom (TOASTER .TOAST ).containsText (' success toast' );
63
73
});
64
74
65
- test ( '`warning` displays the title and message' , async function ( assert ) {
75
+ test < TestContext > (' `warning` displays the title and message' , async function (assert ) {
66
76
this .toaster .success (' toast' , ' warning' );
67
77
await settled ();
68
78
69
79
assert .dom (TOASTER .TOAST ).containsText (' warning toast' );
70
80
});
71
81
72
- test ( '`error` displays the title and message' , async function ( assert ) {
82
+ test < TestContext > (' `error` displays the title and message' , async function (assert ) {
73
83
this .toaster .success (' toast' , ' error' );
74
84
await settled ();
75
85
76
86
assert .dom (TOASTER .TOAST ).containsText (' error toast' );
77
87
});
78
88
79
- test ( '`loading` displays the title and message' , async function ( assert ) {
89
+ test < TestContext > (' `loading` displays the title and message' , async function (assert ) {
80
90
this .toaster .success (' toast' , ' loading' );
81
91
await settled ();
82
92
83
93
assert .dom (TOASTER .TOAST ).containsText (' loading toast' );
84
94
});
85
95
86
- test ( 'toasts can be configured to be closable' , async function ( assert ) {
96
+ test < TestContext > (' toasts can be configured to be closable' , async function (assert ) {
87
97
this .toaster .notify (' toast' , ' closable' );
88
98
await settled ();
89
99
90
- let toast = getRootElement ( ) . querySelector ( TOASTER . TOAST ) ;
100
+ let toast = getRootElement ().querySelector (TOASTER .TOAST ) as HTMLElement ;
91
101
assert .dom (toast ).exists ();
92
- let closeButton = queryByText ( toast , 'Sluit' ) ;
102
+ let closeButton = queryByText (toast , ' Sluit' ) as HTMLButtonElement ;
93
103
assert .dom (closeButton ).exists (' Toasts are closable by default' );
94
104
95
105
await click (closeButton );
@@ -100,16 +110,16 @@ module('Integration | Component | au-toaster', function (hooks) {
100
110
this .toaster .notify (' toast' , ' non-closable' , { closable: false });
101
111
await settled ();
102
112
103
- toast = getRootElement ( ) . querySelector ( TOASTER . TOAST ) ;
113
+ toast = getRootElement ().querySelector (TOASTER .TOAST ) as HTMLElement ;
104
114
assert .dom (toast ).exists ();
105
- closeButton = queryByText ( toast , 'Sluit' ) ;
115
+ closeButton = queryByText (toast , ' Sluit' ) as HTMLButtonElement ;
106
116
107
117
assert
108
118
.dom (closeButton )
109
119
.doesNotExist (' The close button is hidden if the alert is not closable' );
110
120
});
111
121
112
- test ( 'toasts can be configured to auto-close after a certain time' , async function ( assert ) {
122
+ test < TestContext > (' toasts can be configured to auto-close after a certain time' , async function (assert ) {
113
123
this .toaster .notify (' toast' , ' auto-closing' , { timeOut: 20 });
114
124
115
125
// We can't use `await settled();` here since it seemingly waits for the timeOut duration as well
@@ -123,8 +133,8 @@ module('Integration | Component | au-toaster', function (hooks) {
123
133
});
124
134
});
125
135
126
- test ( 'toasts can be closed with the `close` method' , async function ( assert ) {
127
- let toast = this . toaster . notify ( 'toast' , 'notify' ) ;
136
+ test < TestContext > (' toasts can be closed with the `close` method' , async function (assert ) {
137
+ const toast = this .toaster .notify (' toast' , ' notify' );
128
138
assert .ok (toast , ' display methods return a toast object' );
129
139
await settled ();
130
140
@@ -135,26 +145,36 @@ module('Integration | Component | au-toaster', function (hooks) {
135
145
assert .dom (TOASTER .TOAST ).doesNotExist ();
136
146
});
137
147
138
- test ( 'custom toast components can be used' , async function ( assert ) {
148
+ test < TestContext > (' custom toast components can be used' , async function (assert ) {
139
149
const CUSTOM_TOAST = ' [data-test-custom-toast]' ;
140
- const CustomToast = setComponentTemplate (
141
- hbs `<div data-test-custom-toast>
142
- <p>
143
- {{@options.foo}}
144
- </p>
145
- <button {{on "click" @close}} type="button">Close me</button>
146
- </div>` ,
147
- templateOnlyComponent ( ) ,
148
- ) ;
150
+ interface ToastOptionsWithFoo extends ToastOptions {
151
+ foo: string ;
152
+ }
153
+
154
+ const CustomToast: TOC <{
155
+ Args: {
156
+ options: ToastOptionsWithFoo ;
157
+ // options: ToastOptions; // Doesn't work
158
+ // options: ToastOptions & { foo: string }; // Doesn't work
159
+ close: CustomToastSignature [' Args' ][' close' ];
160
+ };
161
+ }> = <template >
162
+ <div data-test-custom-toast >
163
+ <p >
164
+ {{@ options.foo }}
165
+ </p >
166
+ <button {{on " click" @ close}} type =" button" >Close me</button >
167
+ </div >
168
+ </template >;
149
169
150
170
this .toaster .show (CustomToast , { foo: ' Foo' });
151
171
await settled ();
152
172
153
- const toastElement = getRootElement ( ) . querySelector ( CUSTOM_TOAST ) ;
173
+ const toastElement = getRootElement ().querySelector (CUSTOM_TOAST ) as HTMLElement ;
154
174
assert .dom (toastElement ).exists ();
155
175
156
176
assert .dom (toastElement .querySelector (' p' )).containsText (' Foo' );
157
- await click ( queryByText ( toastElement , 'Close me' ) ) ;
177
+ await click (queryByText (toastElement , ' Close me' ) as HTMLButtonElement );
158
178
159
179
assert
160
180
.dom (CUSTOM_TOAST )
@@ -177,10 +197,10 @@ module('Integration | Component | au-toaster', function (hooks) {
177
197
});
178
198
179
199
async function renderToasterContainer() {
180
- await render ( hbs `< AuToaster data-test-toaster-container />` ) ;
200
+ await render (< template >< AuToaster data-test-toaster-container /></ template > );
181
201
}
182
202
183
- function timeout ( delay ) {
203
+ function timeout(delay : number ) {
184
204
return new Promise ((resolve ) => {
185
205
setTimeout (resolve , delay );
186
206
});
0 commit comments