Skip to content

Commit

Permalink
basic unit test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderKolberg committed Dec 7, 2023
1 parent 89044fb commit dc8ee49
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions _examples/golang-sse/webapp/tests/sse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { beforeEach, test, expect, vi } from "vitest";

import {
WebrpcError,
Chat,
SubscribeMessagesReturn,
Message,
WebrpcServerPanicError,
} from "../src/rpc";

const data = [
{
id: 1,
text: "Hello",
authorName: "John",
createdAt: new Date().toDateString(),
},
{
id: 2,
text: "Hi",
authorName: "Joe",
createdAt: new Date().toDateString(),
},
{
id: 3,
text: "How are you?",
authorName: "John",
createdAt: new Date().toDateString(),
},
] satisfies Message[];


function createMockFetch(
{ status, body }: { status?: number; body?: any } = {
status: 200,
body: undefined,
}
) {
return function mockFetch(input: RequestInfo | URL, init?: RequestInit) {
const stream = new ReadableStream({
start(controller) {
data.forEach((item) => {
const buffer = new TextEncoder().encode(
JSON.stringify({ message: item }) + "\n"
);
controller.enqueue(buffer);
});

controller.close();
},
});

body = body || stream;

return Promise.resolve(
new Response(body, {
status,
headers: {
"Content-Type": "application/x-ndjson",
},
})
);
};
}



let onMessage = (msg: any) => {};
let onError = (err: WebrpcError) => {};

beforeEach(() => {
onMessage = (msg: any) => {};
onError = (err: WebrpcError) => {};
});



test("call onOpen right before opening stream", async () => {
const mockFetch = createMockFetch();
const api = new Chat("", mockFetch);

const onOpen = vi.fn();
await api.subscribeMessages(
{ serverTimeoutSec: 10 },
{ onOpen, onMessage, onError }
);

expect(onOpen).toHaveBeenCalled();
});

test("call onMessage with correct data", async () => {
const mockFetch = createMockFetch();
const api = new Chat("", mockFetch);

let messages: Message[] = [];
const onMessage = (msg: SubscribeMessagesReturn) => {
messages.push(msg.message);
};
await api.subscribeMessages({ serverTimeoutSec: 10 }, { onMessage, onError });

expect(messages).toEqual(data);
});

test("call onClose when stream is done", async () => {
const mockFetch = createMockFetch();
const api = new Chat("", mockFetch);

const onClose = vi.fn();

await api.subscribeMessages(
{ serverTimeoutSec: 10 },
{ onMessage, onError, onClose }
);

expect(onClose).toHaveBeenCalled();
});

test("call onError with WebrpcServerPanicError on server panic", async () => {
const mockFetch = createMockFetch({
status: 500,
body: JSON.stringify({ code: -6 }),
});

const api = new Chat("", mockFetch);
let error: WebrpcError | undefined;

const onError = (err: WebrpcError) => {
error = err;
};

await api.subscribeMessages({ serverTimeoutSec: 10 }, { onMessage, onError });

expect(error).toEqual(new WebrpcServerPanicError());
});

0 comments on commit dc8ee49

Please sign in to comment.