1- import { test } from "jsr:@denops/test@^3.0.0" ;
2- import { assertEquals , assertRejects } from "jsr:@std/assert@^1.0.0" ;
3- import { AssertError } from "jsr:@core/unknownutil@^4.3 .0/assert " ;
1+ import { DenopsStub } from "jsr:@denops/test@^3.0.0" ;
2+ import { assertEquals } from "jsr:@std/assert@^1.0.0" ;
3+ import { describe , it } from "jsr:@std/testing@^1.0 .0/bdd " ;
44
55import { main } from "./submatch.ts" ;
66
7- test ( {
8- mode : "all" ,
9- name : "submatch - register dispatcher" ,
10- fn : async ( denops ) => {
7+ describe ( "submatch" , ( ) => {
8+ it ( "should register submatch dispatcher" , ( ) => {
9+ const denops = new DenopsStub ( ) ;
1110 main ( denops ) ;
1211
1312 assertEquals ( typeof denops . dispatcher [ "submatch" ] , "function" ) ;
14- } ,
15- } ) ;
13+ } ) ;
1614
17- test ( {
18- mode : "all" ,
19- name : "submatch - reject invalid context" ,
20- fn : async ( denops ) => {
15+ it ( "should handle invalid context gracefully" , async ( ) => {
16+ const denops = new DenopsStub ( ) ;
2117 main ( denops ) ;
2218
23- await assertRejects (
24- async ( ) => {
25- await denops . dispatcher [ "submatch" ] ( { } , { } , { } ) ;
26- } ,
27- AssertError ,
28- ) ;
29- } ,
30- } ) ;
19+ // withHandleError catches errors, so it won't reject but return undefined
20+ const result = await denops . dispatcher [ "submatch" ] ( { } , { } , { } ) ;
21+ assertEquals ( result , undefined ) ;
22+ } ) ;
3123
32- test ( {
33- mode : "all" ,
34- name : "submatch - reject invalid params" ,
35- fn : async ( denops ) => {
24+ it ( "should handle invalid params gracefully" , async ( ) => {
25+ const denops = new DenopsStub ( ) ;
3626 main ( denops ) ;
3727
3828 const validContext = {
@@ -56,32 +46,28 @@ test({
5646 } ,
5747 } ;
5848
59- await assertRejects (
60- async ( ) => {
61- await denops . dispatcher [ "submatch" ] ( validContext , { } , { } ) ;
62- } ,
63- AssertError ,
64- ) ;
65- } ,
66- } ) ;
49+ // withHandleError catches errors, so it won't reject but return undefined
50+ const result = await denops . dispatcher [ "submatch" ] ( validContext , { } , { } ) ;
51+ assertEquals ( result , undefined ) ;
52+ } ) ;
6753
68- test ( {
69- mode : "all" ,
70- name : "submatch - accept valid call" ,
71- fn : async ( denops ) => {
72- main ( denops ) ;
54+ it ( "should process valid submatch call" , async ( ) => {
55+ const denops = new DenopsStub ( ) ;
7356
74- // Mock the picker dispatcher
75- const originalDispatcher = denops . dispatcher ;
76- let pickerCalled = false ;
77- denops . dispatcher = {
78- ...originalDispatcher ,
79- picker : ( ) => {
80- pickerCalled = true ;
81- return Promise . resolve ( undefined ) ;
82- } ,
57+ // Track dispatch calls
58+ let dispatchCalled = false ;
59+ let dispatchArgs : unknown [ ] = [ ] ;
60+
61+ denops . dispatch = ( name : string , ...args : unknown [ ] ) => {
62+ if ( name === denops . name && args [ 0 ] === "picker" ) {
63+ dispatchCalled = true ;
64+ dispatchArgs = args ;
65+ }
66+ return Promise . resolve ( undefined ) ;
8367 } ;
8468
69+ main ( denops ) ;
70+
8571 const validContext = {
8672 filteredItems : [ { value : "item1" } , { value : "item2" } ] ,
8773 _submatch : {
@@ -113,30 +99,25 @@ test({
11399
114100 await denops . dispatcher [ "submatch" ] ( validContext , validParams , { } ) ;
115101
116- assertEquals ( pickerCalled , true ) ;
102+ assertEquals ( dispatchCalled , true ) ;
103+ assertEquals ( dispatchArgs [ 0 ] , "picker" ) ;
104+ } ) ;
117105
118- // Restore
119- denops . dispatcher = originalDispatcher ;
120- } ,
121- } ) ;
106+ it ( "should forward optional parameters to picker" , async ( ) => {
107+ const denops = new DenopsStub ( ) ;
122108
123- test ( {
124- mode : "all" ,
125- name : "submatch - handle optional parameters" ,
126- fn : async ( denops ) => {
127- main ( denops ) ;
109+ // Track dispatch calls
110+ let capturedPickerParams : unknown ;
128111
129- // Mock the picker dispatcher
130- const originalDispatcher = denops . dispatcher ;
131- let capturedParams : unknown ;
132- denops . dispatcher = {
133- ...originalDispatcher ,
134- picker : ( _args : unknown , params : unknown ) => {
135- capturedParams = params ;
136- return Promise . resolve ( undefined ) ;
137- } ,
112+ denops . dispatch = ( name : string , ...args : unknown [ ] ) => {
113+ if ( name === denops . name && args [ 0 ] === "picker" ) {
114+ capturedPickerParams = args [ 2 ] ; // The picker params
115+ }
116+ return Promise . resolve ( undefined ) ;
138117 } ;
139118
119+ main ( denops ) ;
120+
140121 const validContext = {
141122 filteredItems : [ { value : "item1" } ] ,
142123 selectedItems : [ { value : "selected" } ] ,
@@ -186,52 +167,28 @@ test({
186167
187168 await denops . dispatcher [ "submatch" ] ( validContext , validParams , { } ) ;
188169
189- assertEquals ( typeof capturedParams , "object" ) ;
190- assertEquals (
191- ( capturedParams as Record < string , unknown > ) . defaultAction ,
192- "custom" ,
193- ) ;
194- assertEquals (
195- Array . isArray ( ( capturedParams as Record < string , unknown > ) . sorters ) ,
196- true ,
197- ) ;
198- assertEquals (
199- Array . isArray ( ( capturedParams as Record < string , unknown > ) . renderers ) ,
200- true ,
201- ) ;
202- assertEquals (
203- Array . isArray ( ( capturedParams as Record < string , unknown > ) . previewers ) ,
204- true ,
205- ) ;
206- assertEquals (
207- typeof ( capturedParams as Record < string , unknown > ) . coordinator ,
208- "object" ,
209- ) ;
210- assertEquals (
211- typeof ( capturedParams as Record < string , unknown > ) . theme ,
212- "object" ,
213- ) ;
214-
215- // Restore
216- denops . dispatcher = originalDispatcher ;
217- } ,
218- } ) ;
219-
220- test ( {
221- mode : "all" ,
222- name : "submatch - return true when picker returns true" ,
223- fn : async ( denops ) => {
224- main ( denops ) ;
225-
226- // Mock the picker dispatcher
227- const originalDispatcher = denops . dispatcher ;
228- denops . dispatcher = {
229- ...originalDispatcher ,
230- picker : ( ) => {
170+ assertEquals ( typeof capturedPickerParams , "object" ) ;
171+ const params = capturedPickerParams as Record < string , unknown > ;
172+ assertEquals ( params . defaultAction , "custom" ) ;
173+ assertEquals ( Array . isArray ( params . sorters ) , true ) ;
174+ assertEquals ( Array . isArray ( params . renderers ) , true ) ;
175+ assertEquals ( Array . isArray ( params . previewers ) , true ) ;
176+ assertEquals ( typeof params . coordinator , "object" ) ;
177+ assertEquals ( typeof params . theme , "object" ) ;
178+ } ) ;
179+
180+ it ( "should return true when picker returns true" , async ( ) => {
181+ const denops = new DenopsStub ( ) ;
182+
183+ denops . dispatch = ( name : string , ...args : unknown [ ] ) => {
184+ if ( name === denops . name && args [ 0 ] === "picker" ) {
231185 return Promise . resolve ( true ) ;
232- } ,
186+ }
187+ return Promise . resolve ( undefined ) ;
233188 } ;
234189
190+ main ( denops ) ;
191+
235192 const validContext = {
236193 filteredItems : [ ] ,
237194 _submatch : {
@@ -268,8 +225,5 @@ test({
268225 ) ;
269226
270227 assertEquals ( result , true ) ;
271-
272- // Restore
273- denops . dispatcher = originalDispatcher ;
274- } ,
228+ } ) ;
275229} ) ;
0 commit comments