1
- import { beforeEach , describe , expect , it , mock } from 'bun:test'
1
+ import { describe , expect , it , beforeAll , afterAll } from 'bun:test'
2
2
import { db } from '@stacksjs/database'
3
3
import { fetchById } from '../customers/fetch'
4
4
import { store } from '../customers/store'
5
- import { update } from '../customers/update '
5
+ import type { Generated } from 'kysely '
6
6
7
- // Mock the database
8
- mock . module ( '@stacksjs/database' , ( ) => ( {
9
- db : {
10
- selectFrom : mock ( ( ) => ( {
11
- where : mock ( ( ) => ( {
12
- selectAll : mock ( ( ) => ( {
13
- executeTakeFirst : mock ( ( ) => Promise . resolve ( {
14
- id : 1 ,
15
- name : 'Test Customer' ,
16
-
17
- phone : '123-456-7890' ,
18
- status : 'Active' ,
19
- avatar : 'https://example.com/avatar.jpg' ,
20
- user_id : 1 ,
21
- created_at : new Date ( ) ,
22
- updated_at : new Date ( ) ,
23
- } ) ) ,
24
- } ) ) ,
25
- } ) ) ,
26
- } ) ) ,
27
- insertInto : mock ( ( ) => ( {
28
- values : mock ( ( ) => ( {
29
- executeTakeFirst : mock ( ( ) => Promise . resolve ( { insertId : 1 } ) ) ,
30
- } ) ) ,
31
- } ) ) ,
32
- updateTable : mock ( ( ) => ( {
33
- set : mock ( ( ) => ( {
34
- where : mock ( ( ) => ( {
35
- execute : mock ( ( ) => Promise . resolve ( ) ) ,
36
- } ) ) ,
37
- } ) ) ,
38
- } ) ) ,
39
- } ,
40
- } ) )
41
-
42
- // Mock the request validation
43
- class MockRequest {
7
+ // Create a request-like object for testing
8
+ class TestRequest {
44
9
private data : Record < string , any > = { }
45
10
46
11
constructor ( data : Record < string , any > ) {
@@ -51,110 +16,64 @@ class MockRequest {
51
16
return Promise . resolve ( )
52
17
}
53
18
54
- get ( key : string ) {
55
- return this . data [ key ]
19
+ get < T = any > ( key : string ) : T {
20
+ return this . data [ key ] as T
56
21
}
57
22
}
58
23
59
24
describe ( 'Customer Module' , ( ) => {
60
- beforeEach ( ( ) => {
61
- // Reset mocks before each test
62
- mock . restore ( )
63
- } )
64
-
65
- describe ( 'fetchById' , ( ) => {
66
- it ( 'should fetch a customer by ID' , async ( ) => {
67
- const customer = await fetchById ( 1 )
68
-
69
- expect ( customer ) . toBeDefined ( )
70
- expect ( customer ?. id ) . toBe ( 1 )
71
- expect ( customer ?. name ) . toBe ( 'Test Customer' )
72
- expect ( customer ?. email ) . toBe ( '[email protected] ' )
73
-
74
- // Verify db was called correctly
75
- expect ( db . selectFrom ) . toHaveBeenCalledWith ( 'customers' )
76
- expect ( db . selectFrom ( 'customers' ) . where ) . toHaveBeenCalledWith ( 'id' , '=' , 1 )
77
- } )
25
+ let testCustomerId : number | undefined
26
+
27
+ // Clean up after all tests
28
+ afterAll ( async ( ) => {
29
+ if ( testCustomerId ) {
30
+ // Delete the test customer from the database
31
+ await db
32
+ . deleteFrom ( 'customers' )
33
+ . where ( 'id' , '=' , testCustomerId )
34
+ . execute ( )
35
+ }
78
36
} )
79
37
80
38
describe ( 'store' , ( ) => {
81
- it ( 'should create a new customer' , async ( ) => {
39
+ it ( 'should create a new customer in the database' , async ( ) => {
40
+ // Create a unique email to avoid conflicts
41
+ const uniqueEmail = `test-${ Date . now ( ) } @example.com`
42
+
82
43
const requestData = {
83
- name : 'New Customer' ,
84
-
85
- phone : '987-654-3210 ' ,
44
+ name : 'Test Customer' ,
45
+ email : uniqueEmail ,
46
+ phone : '555-123-4567 ' ,
86
47
status : 'Active' ,
87
- user_id : 2 ,
48
+ user_id : 1 ,
88
49
}
89
50
90
- const request = new MockRequest ( requestData )
51
+ const request = new TestRequest ( requestData )
91
52
const customer = await store ( request as any )
92
53
93
54
expect ( customer ) . toBeDefined ( )
94
- expect ( customer ?. id ) . toBe ( 1 )
95
55
expect ( customer ?. name ) . toBe ( 'Test Customer' )
96
-
97
- // Verify db was called correctly
98
- expect ( db . insertInto ) . toHaveBeenCalledWith ( 'customers' )
99
- expect ( db . insertInto ( 'customers' ) . values ) . toHaveBeenCalledTimes ( 1 )
100
- } )
101
-
102
- it ( 'should throw an error if email already exists' , async ( ) => {
103
- // Mock the database to throw a duplicate entry error
104
- mock . module ( '@stacksjs/database' , ( ) => ( {
105
- db : {
106
- insertInto : mock ( ( ) => ( {
107
- values : mock ( ( ) => ( {
108
- executeTakeFirst : mock ( ( ) => {
109
- throw new Error ( 'Duplicate entry for key email' )
110
- } ) ,
111
- } ) ) ,
112
- } ) ) ,
113
- } ,
114
- } ) )
115
-
116
- const requestData = {
117
- name : 'Duplicate Customer' ,
118
-
119
- phone : '555-555-5555' ,
120
- }
121
-
122
- const request = new MockRequest ( requestData )
123
-
124
- await expect ( store ( request as any ) ) . rejects . toThrow ( 'A customer with this email already exists' )
56
+ expect ( customer ?. email ) . toBe ( uniqueEmail )
57
+ expect ( customer ?. phone ) . toBe ( '555-123-4567' )
58
+
59
+ // Save the ID for later cleanup and convert from Generated<number> to number
60
+ testCustomerId = customer ?. id !== undefined ? Number ( customer . id ) : undefined
125
61
} )
126
62
} )
127
63
128
- describe ( 'update ' , ( ) => {
129
- it ( 'should update an existing customer' , async ( ) => {
130
- const requestData = {
131
- name : 'Updated Customer' ,
132
-
133
- phone : '111-222-3333' ,
64
+ describe ( 'fetchById ' , ( ) => {
65
+ it ( 'should fetch a customer by ID ' , async ( ) => {
66
+ // Skip if we don't have a customer ID from the previous test
67
+ if ( ! testCustomerId ) {
68
+ console . log ( 'Skipping fetchById test because no customer was created' )
69
+ return
134
70
}
135
71
136
- const request = new MockRequest ( requestData )
137
- const customer = await update ( 1 , request as any )
72
+ const customer = await fetchById ( testCustomerId )
138
73
139
74
expect ( customer ) . toBeDefined ( )
140
- expect ( customer ?. id ) . toBe ( 1 )
141
-
142
- // Verify db was called correctly
143
- expect ( db . updateTable ) . toHaveBeenCalledWith ( 'customers' )
144
- expect ( db . updateTable ( 'customers' ) . set ) . toHaveBeenCalledTimes ( 1 )
145
- expect ( db . selectFrom ) . toHaveBeenCalledWith ( 'customers' )
146
- } )
147
-
148
- it ( 'should return the customer without updating if no data provided' , async ( ) => {
149
- const request = new MockRequest ( { } )
150
- const customer = await update ( 1 , request as any )
151
-
152
- expect ( customer ) . toBeDefined ( )
153
- expect ( customer ?. id ) . toBe ( 1 )
154
-
155
- // Verify db was not called to update
156
- expect ( db . updateTable ) . not . toHaveBeenCalled ( )
157
- expect ( db . selectFrom ) . toHaveBeenCalledWith ( 'customers' )
75
+ expect ( customer ?. id ) . toBe ( testCustomerId )
76
+ expect ( customer ?. name ) . toBe ( 'Test Customer' )
158
77
} )
159
78
} )
160
79
} )
0 commit comments