-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Carlos Rodrigues
committed
Oct 25, 2019
1 parent
fb7904f
commit 51e92dc
Showing
6 changed files
with
207 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { useWebSocket } from "../../src"; | ||
import { WS } from "jest-websocket-mock"; | ||
import { watch } from "@vue/composition-api"; | ||
import { nextTick } from "../utils"; | ||
|
||
describe("WebSocket", () => { | ||
const FAKE_URL = `ws://localhost:42`; | ||
let server: WS = undefined as any; | ||
|
||
let warnSpy: jest.SpyInstance = undefined as any; | ||
beforeAll(() => { | ||
warnSpy = jest.spyOn(console, "warn").mockImplementation(); | ||
}); | ||
|
||
beforeEach(() => { | ||
server = new WS(FAKE_URL); | ||
warnSpy.mockReset(); | ||
}); | ||
|
||
afterEach(() => { | ||
server!.close(); | ||
}); | ||
|
||
afterAll(()=>{ | ||
warnSpy.mockReset(); | ||
}) | ||
|
||
it("should connect to server", async () => { | ||
const { isOpen } = useWebSocket(FAKE_URL); | ||
|
||
await server.connected; | ||
|
||
expect(isOpen.value).toBe(true); | ||
}); | ||
|
||
it("should received message", async () => { | ||
const { messageEvent } = useWebSocket(FAKE_URL); | ||
await server.connected; | ||
|
||
expect(messageEvent.value).toBeNull(); | ||
|
||
server.send("test"); | ||
expect(messageEvent.value).not.toBeNull(); | ||
|
||
expect(messageEvent.value).toMatchObject({ | ||
data: "test" | ||
}); | ||
}); | ||
|
||
it("should send message", async () => { | ||
const { send } = useWebSocket(FAKE_URL); | ||
await server.connected; | ||
|
||
send("test"); | ||
|
||
expect(await server.nextMessage).toBe("test"); | ||
}); | ||
|
||
it("should store error", async () => { | ||
const { errorEvent, errored } = useWebSocket(FAKE_URL); | ||
await server.connected; | ||
|
||
server.error(); | ||
|
||
expect(errorEvent.value).toBeTruthy(); | ||
expect(errored.value).toBe(true); | ||
}); | ||
|
||
it("should warn if the rate of messaging is too high", async () => { | ||
const messages = ["test", "test1", "test2"]; | ||
const received: string[] = []; | ||
|
||
const { data } = useWebSocket(FAKE_URL); | ||
await server.connected; | ||
|
||
watch(data, m => received.push(m), { | ||
lazy: true | ||
}); | ||
|
||
for (let i = 0; i < messages.length; i++) { | ||
server.send(messages[i]); | ||
} | ||
|
||
await nextTick(); | ||
expect(warnSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should get all messages", async () => { | ||
const messages = ["test", "test1", "test2"]; | ||
const received: string[] = []; | ||
|
||
const { data } = useWebSocket(FAKE_URL); | ||
await server.connected; | ||
|
||
watch(data, m => received.push(m), { | ||
lazy: true | ||
}); | ||
|
||
for (let i = 0; i < messages.length; i++) { | ||
server.send(messages[i]); | ||
|
||
await nextTick(); | ||
} | ||
|
||
expect(received).toStrictEqual(messages); | ||
}); | ||
}); |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { ref } from "@vue/composition-api"; | ||
|
||
export function useWebSocket(url: string, protocols?: string | string[]) { | ||
const ws = new WebSocket(url, protocols); | ||
const messageEvent = ref<MessageEvent>(null); | ||
const errorEvent = ref<Event>(); | ||
const data = ref<any>(null); | ||
|
||
const isOpen = ref(false); | ||
const isClosed = ref(false); | ||
const errored = ref(false); | ||
|
||
/* istanbul ignore next */ | ||
let lastMessage = (__DEV__ && Date.now()) || undefined; | ||
|
||
ws.addEventListener("message", x => { | ||
messageEvent.value = x; | ||
data.value = x.data; | ||
|
||
// if the messages are to quick, we need to warn | ||
/* istanbul ignore else */ | ||
if (__DEV__) { | ||
if (Date.now() - lastMessage! < 2) { | ||
console.warn( | ||
'[useWebSocket] message rate is too high, if you are using "data" or "messageEvent"' + | ||
" you might not get updated of all the messages." + | ||
' Use "ws..addEventListener("message", handler)" instead' | ||
); | ||
} | ||
lastMessage = Date.now(); | ||
} | ||
}); | ||
|
||
ws.addEventListener("error", error => { | ||
errorEvent.value = error; | ||
errored.value = true; | ||
}); | ||
|
||
ws.addEventListener("close", () => { | ||
isOpen.value = false; | ||
isClosed.value = true; | ||
}); | ||
|
||
ws.addEventListener("open", () => { | ||
isOpen.value = true; | ||
isClosed.value = false; | ||
}); | ||
|
||
const send = (data: string | ArrayBufferLike | Blob | ArrayBufferView) => | ||
ws.send(data); | ||
|
||
return { | ||
ws, | ||
send, | ||
|
||
messageEvent, | ||
errorEvent, | ||
|
||
data, | ||
|
||
isOpen, | ||
isClosed, | ||
errored | ||
}; | ||
} |
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