1
1
import { jest } from "@jest/globals" ;
2
2
3
3
import { PrismaClient } from "@prisma/client" ;
4
- import { createPrefixedIdsExtension , extendPrismaClient } from "../index" ;
4
+ import {
5
+ createPrefixedIdsExtension ,
6
+ extendPrismaClient ,
7
+ PrefixConfig ,
8
+ } from "../index" ;
9
+
10
+ type MockUser = {
11
+ id : string ;
12
+ name : string ;
13
+ } ;
14
+
15
+ type CreateArgs = {
16
+ data : Record < string , unknown > ;
17
+ } ;
5
18
6
19
// Mock PrismaClient
7
20
jest . mock ( "@prisma/client" , ( ) => {
8
21
return {
9
22
PrismaClient : jest . fn ( ) . mockImplementation ( ( ) => ( {
10
23
$extends : jest . fn ( ) ,
24
+ user : {
25
+ create : jest . fn ( ) ,
26
+ findUnique : jest . fn ( ) ,
27
+ } ,
11
28
} ) ) ,
12
29
} ;
13
30
} ) ;
@@ -18,12 +35,12 @@ jest.mock("nanoid", () => ({
18
35
} ) ) ;
19
36
20
37
describe ( "PrefixedIdsExtension" , ( ) => {
21
- let prisma : PrismaClient ;
38
+ let prisma : jest . Mocked < PrismaClient > ;
22
39
const mockQuery = jest . fn ( ( args : any ) => Promise . resolve ( args ) ) ;
23
40
24
41
beforeEach ( ( ) => {
25
42
jest . clearAllMocks ( ) ;
26
- prisma = new PrismaClient ( ) ;
43
+ prisma = new PrismaClient ( ) as jest . Mocked < PrismaClient > ;
27
44
} ) ;
28
45
29
46
describe ( "createPrefixedIdsExtension" , ( ) => {
@@ -115,6 +132,36 @@ describe("PrefixedIdsExtension", () => {
115
132
expect ( result . data [ 0 ] ) . toHaveProperty ( "id" ) ;
116
133
expect ( result . data [ 1 ] ) . toHaveProperty ( "id" ) ;
117
134
} ) ;
135
+
136
+ it ( "should generate IDs with uppercase letters" , async ( ) : Promise < void > => {
137
+ const prefixConfig : PrefixConfig < "User" > = {
138
+ prefixes : {
139
+ User : "usr" ,
140
+ } ,
141
+ // Force an ID with uppercase for testing
142
+ idGenerator : ( prefix : string ) : string => {
143
+ return `${ prefix } _ABC123DEF` ;
144
+ } ,
145
+ } ;
146
+
147
+ const extension = createPrefixedIdsExtension ( prefixConfig ) ;
148
+ prisma . $extends ( extension ) ;
149
+
150
+ // Mock the create operation
151
+ prisma . user . create . mockResolvedValueOnce ( {
152
+ id : "usr_ABC123DEF" ,
153
+ name : "Test User" ,
154
+ } ) ;
155
+
156
+ const user = await prisma . user . create ( {
157
+ data : {
158
+ name : "Test User" ,
159
+ } ,
160
+ } ) ;
161
+
162
+ // Test that the ID matches our pattern including uppercase
163
+ expect ( user . id ) . toMatch ( / ^ u s r _ [ A - Z 0 - 9 ] + $ / ) ;
164
+ } ) ;
118
165
} ) ;
119
166
120
167
describe ( "extendPrismaClient" , ( ) => {
0 commit comments