11import { http } from '../http'
22import { graphql } from '../graphql'
33import { ws } from '../ws'
4+ import { getSiblingHandlers } from '../utils/internal/attachSiblingHandlers'
45import { InMemoryHandlersController } from './handlers-controller'
56
7+ describe ( 'constructor' , ( ) => {
8+ it ( 'places the sibling in its own kind bucket' , ( ) => {
9+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
10+ const [ upgradeHandler ] = getSiblingHandlers ( wsHandler )
11+
12+ const controller = new InMemoryHandlersController ( [ wsHandler ] )
13+
14+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsHandler ] )
15+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [ upgradeHandler ] )
16+ } )
17+
18+ it ( 'interleaves the sibling at the owner position when grouping by kind' , ( ) => {
19+ const httpOne = http . get ( '/' , ( ) => { } )
20+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
21+ const [ upgradeHandler ] = getSiblingHandlers ( wsHandler )
22+ const httpTwo = http . get ( '/' , ( ) => { } )
23+
24+ const controller = new InMemoryHandlersController ( [
25+ httpOne ,
26+ wsHandler ,
27+ httpTwo ,
28+ ] )
29+
30+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [
31+ httpOne ,
32+ upgradeHandler ,
33+ httpTwo ,
34+ ] )
35+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsHandler ] )
36+ } )
37+
38+ it ( 'extracts siblings from every owner in the input list' , ( ) => {
39+ const wsOne = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
40+ const wsTwo = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
41+ const [ upgradeOne ] = getSiblingHandlers ( wsOne )
42+ const [ upgradeTwo ] = getSiblingHandlers ( wsTwo )
43+
44+ const controller = new InMemoryHandlersController ( [ wsOne , wsTwo ] )
45+
46+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsOne , wsTwo ] )
47+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [
48+ upgradeOne ,
49+ upgradeTwo ,
50+ ] )
51+ } )
52+
53+ it ( 'dedupes the shared upgrade sibling across multiple handlers from the same link' , ( ) => {
54+ const chat = ws . link ( '*' )
55+ const wsOne = chat . addEventListener ( 'connection' , ( ) => { } )
56+ const wsTwo = chat . addEventListener ( 'connection' , ( ) => { } )
57+ const [ upgradeHandler ] = getSiblingHandlers ( wsOne )
58+
59+ const controller = new InMemoryHandlersController ( [ wsOne , wsTwo ] )
60+
61+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsOne , wsTwo ] )
62+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [ upgradeHandler ] )
63+ } )
64+ } )
65+
666describe ( InMemoryHandlersController . prototype . use , ( ) => {
767 it ( 'prepends a handler to an empty controller' , ( ) => {
868 const controller = new InMemoryHandlersController ( [ ] )
@@ -51,6 +111,44 @@ describe(InMemoryHandlersController.prototype.use, () => {
51111
52112 expect ( controller . currentHandlers ( ) ) . toEqual ( [ graphqlOne , httpTwo , httpOne ] )
53113 } )
114+
115+ it ( 'propagates siblings to their kind buckets at runtime' , ( ) => {
116+ const controller = new InMemoryHandlersController ( [ ] )
117+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
118+ const [ upgradeHandler ] = getSiblingHandlers ( wsHandler )
119+
120+ controller . use ( [ wsHandler ] )
121+
122+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsHandler ] )
123+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [ upgradeHandler ] )
124+ } )
125+
126+ it ( 'prepends incoming siblings before existing handlers of the same kind' , ( ) => {
127+ const existingHttp = http . get ( '/existing' , ( ) => { } )
128+ const controller = new InMemoryHandlersController ( [ existingHttp ] )
129+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
130+ const [ upgradeHandler ] = getSiblingHandlers ( wsHandler )
131+
132+ controller . use ( [ wsHandler ] )
133+
134+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [
135+ upgradeHandler ,
136+ existingHttp ,
137+ ] )
138+ } )
139+
140+ it ( 'dedupes the shared upgrade sibling when called with multiple handlers from the same link' , ( ) => {
141+ const chat = ws . link ( '*' )
142+ const wsOne = chat . addEventListener ( 'connection' , ( ) => { } )
143+ const wsTwo = chat . addEventListener ( 'connection' , ( ) => { } )
144+ const [ upgradeHandler ] = getSiblingHandlers ( wsOne )
145+
146+ const controller = new InMemoryHandlersController ( [ ] )
147+ controller . use ( [ wsOne , wsTwo ] )
148+
149+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsOne , wsTwo ] )
150+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [ upgradeHandler ] )
151+ } )
54152} )
55153
56154describe ( InMemoryHandlersController . prototype . reset , ( ) => {
@@ -96,6 +194,42 @@ describe(InMemoryHandlersController.prototype.reset, () => {
96194 */
97195 expect ( controller . currentHandlers ( ) ) . toEqual ( [ httpTwo ] )
98196 } )
197+
198+ it ( 'places siblings into their kind buckets when resetting to next handlers' , ( ) => {
199+ const controller = new InMemoryHandlersController ( [ ] )
200+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
201+ const [ upgradeHandler ] = getSiblingHandlers ( wsHandler )
202+
203+ controller . reset ( [ wsHandler ] )
204+
205+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsHandler ] )
206+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [ upgradeHandler ] )
207+ } )
208+
209+ it ( 'restores siblings when resetting to the initial handlers' , ( ) => {
210+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
211+ const [ upgradeHandler ] = getSiblingHandlers ( wsHandler )
212+ const controller = new InMemoryHandlersController ( [ wsHandler ] )
213+
214+ controller . use ( [ http . get ( '/runtime' , ( ) => { } ) ] )
215+ controller . reset ( [ ] )
216+
217+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsHandler ] )
218+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [ upgradeHandler ] )
219+ } )
220+
221+ it ( 'dedupes the shared upgrade sibling when reset with multiple handlers from the same link' , ( ) => {
222+ const chat = ws . link ( '*' )
223+ const wsOne = chat . addEventListener ( 'connection' , ( ) => { } )
224+ const wsTwo = chat . addEventListener ( 'connection' , ( ) => { } )
225+ const [ upgradeHandler ] = getSiblingHandlers ( wsOne )
226+
227+ const controller = new InMemoryHandlersController ( [ ] )
228+ controller . reset ( [ wsOne , wsTwo ] )
229+
230+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsOne , wsTwo ] )
231+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [ upgradeHandler ] )
232+ } )
99233} )
100234
101235describe ( InMemoryHandlersController . prototype . getHandlersByKind , ( ) => {
@@ -112,11 +246,10 @@ describe(InMemoryHandlersController.prototype.getHandlersByKind, () => {
112246 ] ) . getHandlersByKind ( 'websocket' ) ,
113247 ) . toEqual ( [ ] )
114248
249+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
115250 expect (
116- new InMemoryHandlersController ( [
117- ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } ) ,
118- ] ) . getHandlersByKind ( 'request' ) ,
119- ) . toEqual ( [ ] )
251+ new InMemoryHandlersController ( [ wsHandler ] ) . getHandlersByKind ( 'request' ) ,
252+ ) . toEqual ( getSiblingHandlers ( wsHandler ) )
120253 } )
121254
122255 it ( 'returns all handlers if they all match' , ( ) => {
@@ -142,14 +275,15 @@ describe(InMemoryHandlersController.prototype.getHandlersByKind, () => {
142275 const httpHandler = http . get ( '/' , ( ) => { } )
143276 const graphqlHandler = graphql . query ( '' , ( ) => { } )
144277 const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
278+ const wsHandlerSiblings = getSiblingHandlers ( wsHandler )
145279
146280 expect (
147281 new InMemoryHandlersController ( [
148282 httpHandler ,
149283 graphqlHandler ,
150284 wsHandler ,
151285 ] ) . getHandlersByKind ( 'request' ) ,
152- ) . toEqual ( [ httpHandler , graphqlHandler ] )
286+ ) . toEqual ( [ httpHandler , graphqlHandler , ... wsHandlerSiblings ] )
153287
154288 expect (
155289 new InMemoryHandlersController ( [
0 commit comments