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+
654describe ( InMemoryHandlersController . prototype . use , ( ) => {
755 it ( 'prepends a handler to an empty controller' , ( ) => {
856 const controller = new InMemoryHandlersController ( [ ] )
@@ -51,6 +99,31 @@ describe(InMemoryHandlersController.prototype.use, () => {
5199
52100 expect ( controller . currentHandlers ( ) ) . toEqual ( [ graphqlOne , httpTwo , httpOne ] )
53101 } )
102+
103+ it ( 'propagates siblings to their kind buckets at runtime' , ( ) => {
104+ const controller = new InMemoryHandlersController ( [ ] )
105+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
106+ const [ upgradeHandler ] = getSiblingHandlers ( wsHandler )
107+
108+ controller . use ( [ wsHandler ] )
109+
110+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsHandler ] )
111+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [ upgradeHandler ] )
112+ } )
113+
114+ it ( 'prepends incoming siblings before existing handlers of the same kind' , ( ) => {
115+ const existingHttp = http . get ( '/existing' , ( ) => { } )
116+ const controller = new InMemoryHandlersController ( [ existingHttp ] )
117+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
118+ const [ upgradeHandler ] = getSiblingHandlers ( wsHandler )
119+
120+ controller . use ( [ wsHandler ] )
121+
122+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [
123+ upgradeHandler ,
124+ existingHttp ,
125+ ] )
126+ } )
54127} )
55128
56129describe ( InMemoryHandlersController . prototype . reset , ( ) => {
@@ -96,6 +169,29 @@ describe(InMemoryHandlersController.prototype.reset, () => {
96169 */
97170 expect ( controller . currentHandlers ( ) ) . toEqual ( [ httpTwo ] )
98171 } )
172+
173+ it ( 'places siblings into their kind buckets when resetting to next handlers' , ( ) => {
174+ const controller = new InMemoryHandlersController ( [ ] )
175+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
176+ const [ upgradeHandler ] = getSiblingHandlers ( wsHandler )
177+
178+ controller . reset ( [ wsHandler ] )
179+
180+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsHandler ] )
181+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [ upgradeHandler ] )
182+ } )
183+
184+ it ( 'restores siblings when resetting to the initial handlers' , ( ) => {
185+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
186+ const [ upgradeHandler ] = getSiblingHandlers ( wsHandler )
187+ const controller = new InMemoryHandlersController ( [ wsHandler ] )
188+
189+ controller . use ( [ http . get ( '/runtime' , ( ) => { } ) ] )
190+ controller . reset ( [ ] )
191+
192+ expect ( controller . getHandlersByKind ( 'websocket' ) ) . toEqual ( [ wsHandler ] )
193+ expect ( controller . getHandlersByKind ( 'request' ) ) . toEqual ( [ upgradeHandler ] )
194+ } )
99195} )
100196
101197describe ( InMemoryHandlersController . prototype . getHandlersByKind , ( ) => {
@@ -112,11 +208,10 @@ describe(InMemoryHandlersController.prototype.getHandlersByKind, () => {
112208 ] ) . getHandlersByKind ( 'websocket' ) ,
113209 ) . toEqual ( [ ] )
114210
211+ const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
115212 expect (
116- new InMemoryHandlersController ( [
117- ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } ) ,
118- ] ) . getHandlersByKind ( 'request' ) ,
119- ) . toEqual ( [ ] )
213+ new InMemoryHandlersController ( [ wsHandler ] ) . getHandlersByKind ( 'request' ) ,
214+ ) . toEqual ( getSiblingHandlers ( wsHandler ) )
120215 } )
121216
122217 it ( 'returns all handlers if they all match' , ( ) => {
@@ -142,14 +237,15 @@ describe(InMemoryHandlersController.prototype.getHandlersByKind, () => {
142237 const httpHandler = http . get ( '/' , ( ) => { } )
143238 const graphqlHandler = graphql . query ( '' , ( ) => { } )
144239 const wsHandler = ws . link ( '*' ) . addEventListener ( 'connection' , ( ) => { } )
240+ const wsHandlerSiblings = getSiblingHandlers ( wsHandler )
145241
146242 expect (
147243 new InMemoryHandlersController ( [
148244 httpHandler ,
149245 graphqlHandler ,
150246 wsHandler ,
151247 ] ) . getHandlersByKind ( 'request' ) ,
152- ) . toEqual ( [ httpHandler , graphqlHandler ] )
248+ ) . toEqual ( [ httpHandler , graphqlHandler , ... wsHandlerSiblings ] )
153249
154250 expect (
155251 new InMemoryHandlersController ( [
0 commit comments