@@ -14,194 +14,245 @@ interface TestProtocol extends InPageChannelProtocol {
1414 }
1515}
1616
17- describe ( 'in-page channel function definitions' , ( ) => {
18- it ( 'infers page-script handlers from the protocol name' , ( ) => {
19- createPageScriptChannel < TestProtocol > ( {
20- name : 'devframes:test' ,
21- functions : {
22- echo : {
23- handler : ( value ) => {
24- expectTypeOf ( value ) . toEqualTypeOf < string > ( )
25- return value . toUpperCase ( )
17+ describe ( 'In-page script channel' , ( ) => {
18+ const channel = createPageScriptChannel < TestProtocol > ( {
19+ name : 'devframes:test' ,
20+ functions : {
21+ echo : { handler : value => value } ,
22+ sum : { handler : ( a , b ) => a + b } ,
23+ save : { handler : ( ) => { } } ,
24+ } ,
25+ } )
26+
27+ describe ( 'Function definitions' , ( ) => {
28+ it ( 'infers handlers from the protocol' , ( ) => {
29+ createPageScriptChannel < TestProtocol > ( {
30+ name : 'devframes:test' ,
31+ functions : {
32+ echo : {
33+ handler : ( value ) => {
34+ expectTypeOf ( value ) . toEqualTypeOf < string > ( )
35+ return value . toUpperCase ( )
36+ } ,
2637 } ,
27- } ,
28- sum : {
29- handler : ( a , b ) => {
30- expectTypeOf ( a ) . toEqualTypeOf < number > ( )
31- expectTypeOf ( b ) . toEqualTypeOf < number > ( )
32- return a + b
38+ sum : {
39+ handler : ( a , b ) => {
40+ expectTypeOf ( a ) . toEqualTypeOf < number > ( )
41+ expectTypeOf ( b ) . toEqualTypeOf < number > ( )
42+ return a + b
43+ } ,
3344 } ,
34- } ,
35- save : {
36- handler : ( value ) => {
37- expectTypeOf ( value ) . toEqualTypeOf < string > ( )
45+ save : {
46+ handler : ( value ) => {
47+ expectTypeOf ( value ) . toEqualTypeOf < string > ( )
48+ } ,
3849 } ,
3950 } ,
40- } ,
51+ } )
4152 } )
42- } )
4353
44- it ( 'infers panel handlers from the protocol name' , ( ) => {
45- const { port1 } = new MessageChannel ( )
46- const channel = connectPanelChannel < TestProtocol > ( {
47- name : 'devframes:test' ,
48- window : false ,
49- transport : port1 ,
50- functions : {
51- notify : {
52- handler : ( message ) => {
53- expectTypeOf ( message ) . toEqualTypeOf < string > ( )
54- } ,
55- } ,
56- } ,
57- } )
58- channel . close ( )
59- } )
54+ it ( 'requires every in-page script function' , ( ) => {
55+ // @ts -expect-error `functions` is required.
56+ createPageScriptChannel < TestProtocol > ( { name : 'devframes:test' } )
6057
61- it ( 'requires every function from the local protocol side' , ( ) => {
62- // @ts -expect-error `functions` is required.
63- createPageScriptChannel < TestProtocol > ( { name : 'devframes:test' } )
64- // @ts -expect-error `functions` is required.
65- connectPanelChannel < TestProtocol > ( { name : 'devframes:test' } )
66-
67- createPageScriptChannel < TestProtocol > ( {
68- name : 'devframes:test' ,
69- // @ts -expect-error `sum` and `save` are required.
70- functions : {
71- echo : { handler : value => value } ,
72- } ,
58+ createPageScriptChannel < TestProtocol > ( {
59+ name : 'devframes:test' ,
60+ // @ts -expect-error `sum` and `save` are required.
61+ functions : {
62+ echo : { handler : value => value } ,
63+ } ,
64+ } )
7365 } )
74- } )
7566
76- it ( 'rejects keys from the remote side' , ( ) => {
77- createPageScriptChannel < TestProtocol > ( {
78- name : 'devframes:test' ,
79- functions : {
80- echo : { handler : value => value } ,
81- sum : { handler : ( a , b ) => a + b } ,
82- save : { handler : ( ) => { } } ,
83- // @ts -expect-error `notify` is implemented by panels.
84- notify : { handler : ( message : string ) => void message } ,
85- } ,
67+ it ( 'rejects panel functions' , ( ) => {
68+ createPageScriptChannel < TestProtocol > ( {
69+ name : 'devframes:test' ,
70+ functions : {
71+ echo : { handler : value => value } ,
72+ sum : { handler : ( a , b ) => a + b } ,
73+ save : { handler : ( ) => { } } ,
74+ // @ts -expect-error `notify` is implemented by panels.
75+ notify : { handler : ( message : string ) => void message } ,
76+ } ,
77+ } )
8678 } )
87- } )
8879
89- it ( 'rejects handlers incompatible with the named protocol function' , ( ) => {
90- createPageScriptChannel < TestProtocol > ( {
91- name : 'devframes:test' ,
92- functions : {
93- echo : {
94- // @ts -expect-error `echo` accepts and returns a string.
95- handler : ( value : number ) => value ,
80+ it ( 'rejects incompatible handlers' , ( ) => {
81+ createPageScriptChannel < TestProtocol > ( {
82+ name : 'devframes:test' ,
83+ functions : {
84+ echo : {
85+ // @ts -expect-error `echo` accepts and returns a string.
86+ handler : ( value : number ) => value ,
87+ } ,
88+ sum : { handler : ( a , b ) => a + b } ,
89+ save : { handler : ( ) => { } } ,
9690 } ,
97- sum : { handler : ( a , b ) => a + b } ,
98- save : { handler : ( ) => { } } ,
99- } ,
91+ } )
10092 } )
10193 } )
102- } )
10394
104- describe ( 'page-script channel types' , ( ) => {
105- const channel = createPageScriptChannel < TestProtocol > ( {
106- name : 'devframes:test' ,
107- functions : {
108- echo : { handler : value => value } ,
109- sum : { handler : ( a , b ) => a + b } ,
110- save : { handler : ( ) => { } } ,
111- } ,
112- } )
95+ describe ( 'Function calling' , ( ) => {
96+ it ( 'types fire-and-forget calls to panel functions' , ( ) => {
97+ expectTypeOf ( channel . callEvent ( 'notify' , 'ready' ) ) . toEqualTypeOf < void > ( )
11398
114- it ( 'types calls to panel functions' , ( ) => {
115- expectTypeOf ( channel . callEvent ( 'notify ' , 'ready' ) ) . toEqualTypeOf < void > ( )
116-
117- // @ts -expect-error Page-script functions cannot be called on panels.
118- channel . callEvent ( 'echo' , 'ready' )
119- // @ts -expect-error `notify` requires a string.
120- channel . callEvent ( ' notify' , 42 )
121- // @ts -expect-error ` notify` requires one argument.
122- channel . callEvent ( 'notify' )
123- // @ts -expect-error `notify` accepts one argument.
124- channel . callEvent ( 'notify' , 'ready' , 'extra' )
125- } )
99+ // @ts -expect-error In-page script functions cannot be called on panels.
100+ channel . callEvent ( 'echo ' , 'ready' )
101+ // @ts -expect-error `notify` requires a string.
102+ channel . callEvent ( 'notify' , 42 )
103+ // @ts -expect-error `notify` requires one argument.
104+ channel . callEvent ( 'notify' )
105+ // @ts -expect-error ` notify` accepts one argument.
106+ channel . callEvent ( ' notify' , 'ready' , 'extra' )
107+ } )
108+
109+ it ( 'types calls to connected panels' , ( ) => {
110+ const panel = channel . panels [ 0 ] !
126111
127- it ( 'types connected panel peers and their calls' , ( ) => {
128- const unsubscribe = channel . events . on ( 'panel:connected' , ( panel ) => {
129- expectTypeOf ( panel . id ) . toEqualTypeOf < string > ( )
130112 expectTypeOf ( panel . call ( 'notify' , 'ready' ) ) . toEqualTypeOf < Promise < void > > ( )
131113 expectTypeOf ( panel . close ( ) ) . toEqualTypeOf < void > ( )
132114
133- // @ts -expect-error Page- script functions cannot be called on a panel peer.
115+ // @ts -expect-error In-page script functions cannot be called on a panel peer.
134116 panel . call ( 'sum' , 1 , 2 )
135117 // @ts -expect-error `notify` requires a string.
136118 panel . call ( 'notify' , false )
137119 } )
138-
139- expectTypeOf ( unsubscribe ) . toEqualTypeOf < ( ) => void > ( )
140120 } )
141121
142- it ( 'types disconnected panel peers and one-time listeners' , ( ) => {
143- const unsubscribe = channel . events . once ( 'panel:disconnected' , ( panel ) => {
144- expectTypeOf ( panel . id ) . toEqualTypeOf < string > ( )
145- expectTypeOf ( panel . call ( 'notify' , 'bye' ) ) . toEqualTypeOf < Promise < void > > ( )
146- } )
122+ describe ( 'Event checking' , ( ) => {
123+ it ( 'types panel connection events' , ( ) => {
124+ const unsubscribeConnected = channel . events . on ( 'panel:connected' , ( panel ) => {
125+ expectTypeOf ( panel . id ) . toEqualTypeOf < string > ( )
126+ expectTypeOf ( panel . call ( 'notify' , 'ready' ) ) . toEqualTypeOf < Promise < void > > ( )
127+ } )
128+ const unsubscribeDisconnected = channel . events . on ( 'panel:disconnected' , ( panel ) => {
129+ expectTypeOf ( panel . id ) . toEqualTypeOf < string > ( )
130+ expectTypeOf ( panel . call ( 'notify' , 'bye' ) ) . toEqualTypeOf < Promise < void > > ( )
131+ } )
147132
148- expectTypeOf ( unsubscribe ) . toEqualTypeOf < ( ) => void > ( )
133+ expectTypeOf ( unsubscribeConnected ) . toEqualTypeOf < ( ) => void > ( )
134+ expectTypeOf ( unsubscribeDisconnected ) . toEqualTypeOf < ( ) => void > ( )
135+ } )
149136
150- // @ts -expect-error Unknown page-script channel lifecycle event.
151- channel . events . on ( 'status:updated' , ( ) => { } )
137+ it ( 'rejects panel channel events' , ( ) => {
138+ // @ts -expect-error Unknown in-page script channel lifecycle event.
139+ channel . events . on ( 'status:updated' , ( ) => { } )
140+ } )
152141 } )
153142} )
154143
155- describe ( 'panel channel types ' , ( ) => {
144+ describe ( 'Panel channel' , ( ) => {
156145 const channel = connectPanelChannel < TestProtocol > ( {
157146 name : 'devframes:test' ,
158147 functions : {
159148 notify : { handler : ( ) => { } } ,
160149 } ,
161150 } )
162151
163- it ( 'types calls and their resolved results' , ( ) => {
164- expectTypeOf ( channel . call ( 'echo' , 'hello' ) ) . toEqualTypeOf < Promise < string > > ( )
165- expectTypeOf ( channel . call ( 'sum' , 1 , 2 ) ) . toEqualTypeOf < Promise < number > > ( )
166- expectTypeOf ( channel . call ( 'save' , 'draft' ) ) . toEqualTypeOf < Promise < void > > ( )
167-
168- // @ts -expect-error Panel functions cannot be called on the page script.
169- channel . call ( 'notify' , 'hello' )
170- // @ts -expect-error `echo` requires a string.
171- channel . call ( 'echo' , 42 )
172- // @ts -expect-error `sum` requires two arguments.
173- channel . call ( 'sum' , 1 )
174- // @ts -expect-error `save` accepts one argument.
175- channel . call ( 'save' , 'draft' , 'extra' )
176- } )
152+ describe ( 'Function definitions' , ( ) => {
153+ it ( 'infers handlers from the protocol' , ( ) => {
154+ const { port1 } = new MessageChannel ( )
155+ const inferredChannel = connectPanelChannel < TestProtocol > ( {
156+ name : 'devframes:test' ,
157+ window : false ,
158+ transport : port1 ,
159+ functions : {
160+ notify : {
161+ handler : ( message ) => {
162+ expectTypeOf ( message ) . toEqualTypeOf < string > ( )
163+ } ,
164+ } ,
165+ } ,
166+ } )
167+ inferredChannel . close ( )
168+ } )
177169
178- it ( 'types fire-and-forget calls to page-script functions' , ( ) => {
179- expectTypeOf ( channel . callEvent ( 'echo' , 'hello' ) ) . toEqualTypeOf < void > ( )
180- expectTypeOf ( channel . callEvent ( 'sum' , 1 , 2 ) ) . toEqualTypeOf < void > ( )
181- expectTypeOf ( channel . callEvent ( 'save' , 'draft' ) ) . toEqualTypeOf < void > ( )
182-
183- // @ts -expect-error Panel functions cannot be emitted to the page script.
184- channel . callEvent ( 'notify' , 'hello' )
185- // @ts -expect-error `echo` requires a string.
186- channel . callEvent ( 'echo' , false )
187- // @ts -expect-error `sum` requires two arguments.
188- channel . callEvent ( 'sum' , 1 )
170+ it ( 'requires every panel function' , ( ) => {
171+ // @ts -expect-error `functions` is required.
172+ connectPanelChannel < TestProtocol > ( { name : 'devframes:test' } )
173+
174+ connectPanelChannel < TestProtocol > ( {
175+ name : 'devframes:test' ,
176+ // @ts -expect-error `notify` is required.
177+ functions : { } ,
178+ } )
179+ } )
180+
181+ it ( 'rejects in-page script functions' , ( ) => {
182+ connectPanelChannel < TestProtocol > ( {
183+ name : 'devframes:test' ,
184+ functions : {
185+ notify : { handler : ( ) => { } } ,
186+ // @ts -expect-error `echo` is implemented by the in-page script.
187+ echo : { handler : ( value : string ) => value } ,
188+ } ,
189+ } )
190+ } )
191+
192+ it ( 'rejects incompatible handlers' , ( ) => {
193+ connectPanelChannel < TestProtocol > ( {
194+ name : 'devframes:test' ,
195+ functions : {
196+ notify : {
197+ // @ts -expect-error `notify` accepts a string.
198+ handler : ( message : number ) => void message ,
199+ } ,
200+ } ,
201+ } )
202+ } )
189203 } )
190204
191- it ( 'types status listeners and channel state' , ( ) => {
192- const unsubscribe = channel . events . on ( 'status:updated' , ( status ) => {
193- expectTypeOf ( status ) . toEqualTypeOf < 'connecting' | 'connected' | 'closed' > ( )
205+ describe ( 'Function calling' , ( ) => {
206+ it ( 'types calls and their resolved results' , ( ) => {
207+ expectTypeOf ( channel . call ( 'echo' , 'hello' ) ) . toEqualTypeOf < Promise < string > > ( )
208+ expectTypeOf ( channel . call ( 'sum' , 1 , 2 ) ) . toEqualTypeOf < Promise < number > > ( )
209+ expectTypeOf ( channel . call ( 'save' , 'draft' ) ) . toEqualTypeOf < Promise < void > > ( )
210+
211+ // @ts -expect-error Panel functions cannot be called on the in-page script.
212+ channel . call ( 'notify' , 'hello' )
213+ // @ts -expect-error `echo` requires a string.
214+ channel . call ( 'echo' , 42 )
215+ // @ts -expect-error `sum` requires two arguments.
216+ channel . call ( 'sum' , 1 )
217+ // @ts -expect-error `save` accepts one argument.
218+ channel . call ( 'save' , 'draft' , 'extra' )
194219 } )
195220
196- expectTypeOf ( unsubscribe ) . toEqualTypeOf < ( ) => void > ( )
197- expectTypeOf ( channel . status ) . toEqualTypeOf < 'connecting' | 'connected' | 'closed' > ( )
198- expectTypeOf ( channel . pageScript ) . toEqualTypeOf < { instanceId : string } | undefined > ( )
199- expectTypeOf ( channel . whenConnected ( ) ) . toEqualTypeOf < Promise < void > > ( )
200- expectTypeOf ( channel . whenConnected ( 1_000 ) ) . toEqualTypeOf < Promise < void > > ( )
221+ it ( 'types fire-and-forget calls to in-page script functions' , ( ) => {
222+ expectTypeOf ( channel . callEvent ( 'echo' , 'hello' ) ) . toEqualTypeOf < void > ( )
223+ expectTypeOf ( channel . callEvent ( 'sum' , 1 , 2 ) ) . toEqualTypeOf < void > ( )
224+ expectTypeOf ( channel . callEvent ( 'save' , 'draft' ) ) . toEqualTypeOf < void > ( )
225+
226+ // @ts -expect-error Panel functions cannot be emitted to the in-page script.
227+ channel . callEvent ( 'notify' , 'hello' )
228+ // @ts -expect-error `echo` requires a string.
229+ channel . callEvent ( 'echo' , false )
230+ // @ts -expect-error `sum` requires two arguments.
231+ channel . callEvent ( 'sum' , 1 )
232+ } )
201233
202- // @ts -expect-error Unknown panel channel lifecycle event.
203- channel . events . on ( 'panel:connected' , ( ) => { } )
204- // @ts -expect-error `status:updated` listeners receive the status.
205- channel . events . on ( 'status:updated' , ( status : number ) => void status )
234+ it ( 'types channel state' , ( ) => {
235+ expectTypeOf ( channel . status ) . toEqualTypeOf < 'connecting' | 'connected' | 'closed' > ( )
236+ expectTypeOf ( channel . pageScript ) . toEqualTypeOf < { instanceId : string } | undefined > ( )
237+ expectTypeOf ( channel . whenConnected ( ) ) . toEqualTypeOf < Promise < void > > ( )
238+ expectTypeOf ( channel . whenConnected ( 1_000 ) ) . toEqualTypeOf < Promise < void > > ( )
239+ } )
240+ } )
241+
242+ describe ( 'Event checking' , ( ) => {
243+ it ( 'types status events' , ( ) => {
244+ const unsubscribe = channel . events . on ( 'status:updated' , ( status ) => {
245+ expectTypeOf ( status ) . toEqualTypeOf < 'connecting' | 'connected' | 'closed' > ( )
246+ } )
247+
248+ expectTypeOf ( unsubscribe ) . toEqualTypeOf < ( ) => void > ( )
249+ } )
250+
251+ it ( 'rejects in-page script channel events and incompatible listeners' , ( ) => {
252+ // @ts -expect-error Unknown panel channel lifecycle event.
253+ channel . events . on ( 'panel:connected' , ( ) => { } )
254+ // @ts -expect-error `status:updated` listeners receive the status.
255+ channel . events . on ( 'status:updated' , ( status : number ) => void status )
256+ } )
206257 } )
207258} )
0 commit comments