Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 61 additions & 7 deletions packages/unuse/src/useWebSocket/index.vue.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
// @vitest-environment happy-dom

import { beforeEach, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
import type { Ref } from 'vue';
import { isRef } from 'vue';
import type { WebSocketStatus } from '.';
import { isRef, nextTick, shallowRef } from 'vue';
import type { UseWebSocketReturn, WebSocketStatus } from '.';
import { useWebSocket } from '.';
import { describeVue, useSetup } from '../_testUtils/vue';

describeVue('useWebSocket', () => {
const mockWebSocket = vi.fn<(host: string) => WebSocket>();
let vm: ReturnType<
typeof useSetup<{ ref: UseWebSocketReturn<unknown> }>
> | null = null;

mockWebSocket.prototype.send = vi.fn();
mockWebSocket.prototype.close = vi.fn();

beforeEach(() => {
vi.stubGlobal('WebSocket', mockWebSocket);
});

return () => {
vi.unstubAllGlobals();
};
afterEach(() => {
vm?.unmount();
vi.unstubAllGlobals();
});

it('should initialize web socket', () => {
const vm = useSetup(() => {
vm = useSetup(() => {
const ref = useWebSocket('ws://localhost');
return { ref };
});
Expand All @@ -42,4 +46,54 @@
(vm.ref.ws as unknown as Ref<WebSocket | undefined>).value
).toBeDefined();
});

it('should reconnect if URL changes', async () => {
const url = shallowRef('ws://localhost');
vm = useSetup(() => {
const ref = useWebSocket(url);
return { ref };
});

url.value = 'ws://127.0.0.1';
await nextTick();

expect(mockWebSocket.prototype.close).toHaveBeenCalledWith(1000, undefined);
expect(mockWebSocket).toHaveBeenCalledWith('ws://127.0.0.1', []);
expect((vm.ref.status as unknown as Ref<WebSocketStatus>).value).toBe(
'CONNECTING'
);
});

it('should not reconnect on URL change if immediate and autoConnect are false', async () => {
const url = shallowRef('ws://localhost');
vm = useSetup(() => {
const ref = useWebSocket(url, {
immediate: false,
autoConnect: false,
});
return { ref };
});

url.value = 'ws://127.0.0.1';
await nextTick();

expect(mockWebSocket.prototype.close).not.toHaveBeenCalled();

Check failure on line 80 in packages/unuse/src/useWebSocket/index.vue.spec.ts

View workflow job for this annotation

GitHub Actions / Build & Unit Test: node-24, ubuntu-latest

packages/unuse/src/useWebSocket/index.vue.spec.ts > useWebSocket > Vue > should not reconnect on URL change if immediate and autoConnect are false

AssertionError: expected "spy" to not be called at all, but actually been called 986 times Received: 1st spy call: Array [ 1000, undefined, ] 2nd spy call: Array [ 1000, undefined, ] 3rd spy call: Array [ 1000, undefined, ] 4th spy call: Array [ 1000, undefined, ] 5th spy call: Array [ 1000, undefined, ] 6th spy call: Array [ 1000, undefined, ] 7th spy call: Array [ 1000, undefined, ] 8th spy call: Array [ 1000, undefined, ] 9th spy call: Array [ 1000, undefined, ] 10th spy call: Array [ 1000, undefined, ] 11th spy call: Array [ 1000, undefined, ] 12th spy call: Array [ 1000, undefined, ] 13th spy call: Array [ 1000, undefined, ] 14th spy call: Array [ 1000, undefined, ] 15th spy call: Array [ 1000, undefined, ] 16th spy call: Array [ 1000, undefined, ] 17th spy call: Array [ 1000, undefined, ] 18th spy call: Array [ 1000, undefined, ] 19th spy call: Array [ 1000, undefined, ] 20th spy call: Array [ 1000, undefined, ] 21st spy call: Array [ 1000, undefined, ] 22nd spy call: Array [ 1000, undefined, ] 23rd spy call: Array [ 1000, undefined, ] 24th spy call: Array [ 1000, undefined, ] 25th spy call: Array [ 1000, undefined, ] 26th spy call: Array [ 1000, undefined, ] 27th spy call: Array [ 1000, undefined, ] 28th spy call: Array [ 1000, undefined, ] 29th spy call: Array [ 1000, undefined, ] 30th spy call: Array [ 1000, undefined, ] 31st spy call: Array [ 1000, undefined, ] 32nd spy call: Array [ 1000, undefined, ] 33rd spy call: Array [ 1000, undefined, ] 34th spy call: Array [ 1000, undefined, ] 35th spy call: Array [ 1000, undefined, ] 36th spy call: Array [ 1000, undefined, ] 37th spy call: Array [ 1000, undefined, ] 38th spy call: Array [ 1000, undefined, ] 39th spy call: Array [ 1000, undefined, ] 40th spy call: Array [ 1000, undefined, ] 41st spy call: Array [ 1000, undefined, ] 42nd spy call: Array [ 1000, undefined, ] 43rd spy call: Array [ 1000, undefined, ] 44th spy call: Array [ 1000, undefined, ] 45th spy call: Array [ 1000, undefined, ] 46th spy call: Array [ 1000, undefined, ] 47th spy call: Array [ 1000, undefined, ] 48th spy call: Array [ 1000, undefined, ] 49th spy call: Array [ 1000, undefined, ] 50th spy call: Array [ 1000, undefined, ] 51st spy call: Array [ 1000, undefined, ] 52nd spy call: Array [ 1000, undefined, ] 53rd spy call: Array [ 1000, undefined, ] 54th spy call: Array [ 1000, undefined, ] 55th spy call: Array [ 1000, undefined, ] 56th spy call: Array [ 1000, undefined, ] 57th spy call: Array [ 1000, undefined, ] 58th spy call: Array [ 1000, undefined, ] 59th spy call: Array [ 1000, undefined, ] 60th spy call: Array [ 1000, undefined, ] 61st spy call: Array [ 1000,
expect(mockWebSocket).not.toHaveBeenCalledWith('ws://127.0.0.1', []);
expect((vm.ref.status as unknown as Ref<WebSocketStatus>).value).toBe(
'CLOSED'
);
});

it('should remain closed if immediate is false', () => {
vm = useSetup(() => {
const ref = useWebSocket('ws://localhost', {
immediate: false,
});
return { ref };
});

expect((vm.ref.status as unknown as Ref<WebSocketStatus>).value).toBe(
'CLOSED'
);
});
});
Loading