Created
March 17, 2018 12:07
-
-
Save lgandecki/691c886f002473cd83e2fe9f1c0796b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyWsServer { | |
initialHooks = {}; | |
hooks = {}; | |
on(eventType, cb) { | |
this.initialHooks[eventType] = cb; | |
} | |
receiveConnection(client) { | |
this.initialHooks['connection']({ | |
on: (name, cb) => { | |
this.hooks[name] = cb; | |
}, | |
send: (message) => { | |
client.receive(message); | |
} | |
}); | |
setTimeout(() => { | |
client.connected(); | |
}, 0); | |
} | |
receive(message) { | |
console.log('receiving from client', message) | |
setTimeout(() => { | |
this.hooks.message(message) | |
}, 50); | |
} | |
} | |
let wsServer | |
const returnMyWsClient = (wsServer) => { | |
return class MyWsClient { | |
static OPEN = "OPEN"; | |
static CONNECTING = "CONNECTING" | |
constructor() { | |
wsServer.receiveConnection(this); | |
this.readyState = MyWsClient.CONNECTING | |
} | |
connected() { | |
// setTimeout(() => { | |
this.readyState = MyWsClient.OPEN | |
this.onopen(); | |
// }, 0); | |
} | |
send(message) { | |
console.log("sending") | |
wsServer.receive(message) | |
} | |
receive(message) { | |
console.log("receiving from server ", message) | |
setTimeout(() => { | |
this.onmessage({data: message}) | |
}, 50); | |
} | |
}; | |
}; | |
describe('Client', function () { | |
beforeEach(() => { | |
wsServer = new MyWsServer(); | |
}); | |
it('should send GQL_CONNECTION_INIT message when creating the connection', (done) => { | |
wsServer.on('connection', (connection: any) => { | |
return connection.on('message', (message: any) => { | |
const parsedMessage = JSON.parse(message); | |
expect(parsedMessage.type).to.equals(MessageTypes.GQL_CONNECTION_INIT); | |
done(); | |
}); | |
}); | |
new SubscriptionClient(`ws://localhost:${RAW_TEST_PORT}/`, {}, returnMyWsClient(wsServer)); | |
}); | |
it('should send GQL_CONNECTION_INIT message first, then the GQL_START message', (done) => { | |
let initReceived = false; | |
let sub: any; | |
wsServer.on('connection', (connection: any) => { | |
connection.on('message', (message: any) => { | |
const parsedMessage = JSON.parse(message); | |
console.log("parsed message from client", parsedMessage) | |
// mock server | |
if (parsedMessage.type === MessageTypes.GQL_CONNECTION_INIT) { | |
connection.send(JSON.stringify({ type: MessageTypes.GQL_CONNECTION_ACK, payload: {} })); | |
initReceived = true; | |
} | |
if (parsedMessage.type === MessageTypes.GQL_START) { | |
expect(initReceived).to.be.true; | |
if ( sub ) { | |
sub.unsubscribe(); | |
done(); | |
} else { | |
done(new Error('did not get subscription')); | |
} | |
} | |
}); | |
}); | |
const client = new SubscriptionClient(`ws://localhost:${RAW_TEST_PORT}/`, {}, returnMyWsClient(wsServer)); | |
sub = client.request({ | |
query: `subscription useInfo { | |
user(id: 3) { | |
id | |
name | |
} | |
}`, | |
}).subscribe({}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment