@@ -60,6 +60,59 @@ jest.mock('@aws-amplify/core', () => {
6060} ) ;
6161
6262describe ( 'AppSyncEventProvider' , ( ) => {
63+ describe ( 'connect' , ( ) => {
64+ let fakeWebSocketInterface : FakeWebSocketInterface ;
65+ const loggerSpy : jest . SpyInstance = jest . spyOn (
66+ ConsoleLogger . prototype ,
67+ '_log' ,
68+ ) ;
69+ let provider : AWSAppSyncEventProvider ;
70+ beforeEach ( async ( ) => {
71+ fakeWebSocketInterface = new FakeWebSocketInterface ( ) ;
72+ provider = new AWSAppSyncEventProvider ( ) ;
73+
74+ Object . defineProperty ( provider , 'socketStatus' , {
75+ value : constants . SOCKET_STATUS . CLOSED ,
76+ } ) ;
77+
78+ jest . spyOn ( provider as any , '_getNewWebSocket' ) . mockImplementation ( ( ) => {
79+ fakeWebSocketInterface . newWebSocket ( ) ;
80+ return fakeWebSocketInterface . webSocket as WebSocket ;
81+ } ) ;
82+ } )
83+
84+ afterEach ( async ( ) => {
85+ provider ?. close ( ) ;
86+ await fakeWebSocketInterface ?. closeInterface ( ) ;
87+ fakeWebSocketInterface ?. teardown ( ) ;
88+ loggerSpy . mockClear ( ) ;
89+ } ) ;
90+
91+ test ( 'socket status should be READY' , async ( ) => {
92+
93+ // Connect to the provider
94+ const connectPromise = provider . connect ( {
95+ appSyncGraphqlEndpoint : 'ws://localhost:8080' ,
96+ authenticationType : 'apiKey' ,
97+ apiKey : 'test-api-key' ,
98+ region : 'us-east-1'
99+ } ) ;
100+
101+ // Trigger the websocket open event
102+ await fakeWebSocketInterface . readyForUse ;
103+ await fakeWebSocketInterface . triggerOpen ( ) ;
104+
105+ await fakeWebSocketInterface . sendDataMessage ( {
106+ type : MESSAGE_TYPES . GQL_CONNECTION_ACK
107+ } ) ;
108+
109+ // Wait for connection to complete
110+ await connectPromise ;
111+
112+ // Verify the socket status
113+ expect ( ( provider as any ) . socketStatus ) . toBe ( constants . SOCKET_STATUS . READY ) ;
114+ } ) ;
115+ } ) ;
63116 describe ( 'subscribe()' , ( ) => {
64117 describe ( 'returned observer' , ( ) => {
65118 describe ( 'connection logic with mocked websocket' , ( ) => {
0 commit comments