Skip to content

Commit

Permalink
feat(test): implement automatic testing
Browse files Browse the repository at this point in the history
  • Loading branch information
edorgeville committed May 23, 2024
1 parent 0f11060 commit 544ddf1
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 45 deletions.
13 changes: 12 additions & 1 deletion test/appRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@ const t = initTRPC.create();
const publicProcedure = t.procedure;
const router = t.router;

const state = { count: 0 };

export const appRouter = router({
greet: publicProcedure
.input((val: unknown) => {
if (typeof val === 'string') return val;
throw new Error(`Invalid input: ${typeof val}`);
})
.query(({ input }) => ({ greeting: `hello, ${input}!` }))
.query(({ input }) => ({ greeting: `hello, ${input}!` })),
countUp: publicProcedure
.input((val: unknown) => {
if (typeof val === 'number') return val;
throw new Error(`Invalid input: ${typeof val}`);
})
.mutation(({ input }) => {
state.count += input;
return state.count;
})
});
9 changes: 9 additions & 0 deletions test/broker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Aedes from 'aedes';
import { createServer } from 'net';

const aedes = new Aedes();
const server = createServer(aedes.handle);

server.listen(1883, () => {
console.log('server started and listening on port 1883');
});
26 changes: 0 additions & 26 deletions test/client.ts

This file was deleted.

64 changes: 64 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { createTRPCProxyClient } from '@trpc/client';
import Aedes from 'aedes';
import { once } from 'events';
import mqtt from 'mqtt';
import { createServer } from 'net';

import { createMQTTHandler } from '../src/adapter';
import { mqttLink } from '../src/link';
import { AppRouter, appRouter } from './appRouter';

const requestTopic = 'rpc/request';

const aedes = new Aedes();
// aedes.on('publish', (packet, client) => console.log(packet.topic, packet.payload.toString()));
const broker = createServer(aedes.handle);
broker.listen(1883);
const mqttClient = mqtt.connect('mqtt://localhost');

createMQTTHandler({
client: mqttClient,
requestTopic,
router: appRouter
});

const client = createTRPCProxyClient<AppRouter>({
links: [
mqttLink({
client: mqttClient,
requestTopic
})
]
});

beforeAll(async () => {
await once(broker, 'listening');
await once(mqttClient, 'connect');
});

test('broker is listening', () => {
expect(broker.listening).toBe(true);
});

test('mqtt client is connected', () => {
expect(mqttClient.connected).toBe(true);
});

test('greet query', async () => {
const greeting = await client.greet.query('world');
expect(greeting).toEqual({ greeting: 'hello, world!' });
});

test('countUp mutation', async () => {
const addOne = await client.countUp.mutate(1);
expect(addOne).toBe(1);

const addTwo = await client.countUp.mutate(2);
expect(addTwo).toBe(3);
});

afterAll(async () => {
mqttClient.end();
broker.close();
aedes.close();
});
18 changes: 0 additions & 18 deletions test/server.ts

This file was deleted.

0 comments on commit 544ddf1

Please sign in to comment.