-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcat.test.ts
88 lines (79 loc) · 1.81 KB
/
concat.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { routerToServerAndClientNew } from './___testHelpers';
import { expectTypeOf } from 'expect-type';
import { konn } from 'konn';
import { TRPCError, initTRPC } from '../src';
type User = {
id: string;
name: string;
};
type Context = {
user: User | null;
};
const mockUser: User = {
id: '123',
name: 'John Doe',
};
const ctx = konn()
.beforeEach(() => {
const t = initTRPC.context<Context>().create();
const isAuthed = t.middleware(({ next, ctx }) => {
if (!ctx.user) {
throw new TRPCError({
code: 'UNAUTHORIZED',
});
}
return next({
ctx: {
user: ctx.user,
},
});
});
const addFoo = t.middleware(({ next }) => {
return next({
ctx: {
foo: 'bar' as const,
},
});
});
const proc1 = t.procedure.use(isAuthed);
const proc2 = t.procedure.use(addFoo);
const combined = t.procedure.unstable_concat(proc1).unstable_concat(proc2);
const appRouter = t.router({
getContext: combined.mutation(({ ctx }) => {
return ctx;
}),
});
const opts = routerToServerAndClientNew(appRouter, {
server: {
createContext() {
return {
user: mockUser,
};
},
},
});
const client = opts.client;
return {
close: opts.close,
client,
proxy: opts.proxy,
};
})
.afterEach(async (ctx) => {
await ctx?.close?.();
})
.done();
test('decorate independently', async () => {
const result = await ctx.proxy.getContext.mutate();
// This is correct
expect(result).toEqual({
user: mockUser,
foo: 'bar',
});
// This is not correct
expectTypeOf(result).toMatchTypeOf<{
// TODO FIXME: this is a bug in the type inference
// user: User;
foo: 'bar';
}>();
});