1
+ import { HttpResponse , http } from 'msw' ;
2
+ import { setupServer } from 'msw/node' ;
3
+
4
+ /**
5
+ * Common Ghost API response fixtures
6
+ */
7
+ export const fixtures = {
8
+ // Global data fixtures
9
+ config : {
10
+ version : '5.x' ,
11
+ environment : 'testing' ,
12
+ database : 'mysql8' ,
13
+ mail : 'SMTP' ,
14
+ labs : { } ,
15
+ clientExtensions : { } ,
16
+ enableDeveloperExperiments : true ,
17
+ stripeDirect : false
18
+ } ,
19
+
20
+ settings : {
21
+ title : 'Test Site' ,
22
+ description : 'A test Ghost site' ,
23
+ logo : null ,
24
+ cover_image : null ,
25
+ icon : null ,
26
+ accent_color : '#FF1A75' ,
27
+ locale : 'en' ,
28
+ timezone : 'Etc/UTC' ,
29
+ codeinjection_head : null ,
30
+ codeinjection_foot : null ,
31
+ navigation : [ ] ,
32
+ secondary_navigation : [ ]
33
+ } ,
34
+
35
+ site : {
36
+ title : 'Test Site' ,
37
+ url : 'http://localhost:3000' ,
38
+ version : '5.x'
39
+ } ,
40
+
41
+ user : {
42
+ id : '1' ,
43
+ name : 'Test User' ,
44
+ slug : 'test-user' ,
45
+
46
+ profile_image : null ,
47
+ cover_image : null ,
48
+ bio : null ,
49
+ website : null ,
50
+ location : null ,
51
+ facebook : null ,
52
+ twitter : null ,
53
+ accessibility : null ,
54
+ status : 'active' ,
55
+ meta_title : null ,
56
+ meta_description : null ,
57
+ tour : null ,
58
+ last_seen : '2024-01-01T00:00:00.000Z' ,
59
+ created_at : '2024-01-01T00:00:00.000Z' ,
60
+ updated_at : '2024-01-01T00:00:00.000Z' ,
61
+ roles : [ {
62
+ id : '1' ,
63
+ name : 'Administrator' ,
64
+ description : 'Administrators' ,
65
+ created_at : '2024-01-01T00:00:00.000Z' ,
66
+ updated_at : '2024-01-01T00:00:00.000Z'
67
+ } ]
68
+ } ,
69
+
70
+ // Links fixtures
71
+ linksBulkSuccess : {
72
+ bulk : {
73
+ action : 'updateLink' ,
74
+ meta : {
75
+ stats : { successful : 1 , unsuccessful : 0 } ,
76
+ errors : [ ] ,
77
+ unsuccessfulData : [ ]
78
+ }
79
+ }
80
+ } ,
81
+
82
+ // Stats fixtures (from our previous work)
83
+ memberCountHistory : {
84
+ stats : [
85
+ { date : '2024-01-01' , paid : 100 , free : 500 , comped : 10 , paid_subscribed : 5 , paid_canceled : 2 } ,
86
+ { date : '2024-01-02' , paid : 102 , free : 505 , comped : 10 , paid_subscribed : 3 , paid_canceled : 1 }
87
+ ] ,
88
+ meta : { totals : { paid : 102 , free : 505 , comped : 10 } }
89
+ } ,
90
+
91
+ mrrHistory : {
92
+ stats : [
93
+ { date : '2024-01-01' , mrr : 1000 , currency : 'USD' } ,
94
+ { date : '2024-01-02' , mrr : 1020 , currency : 'USD' }
95
+ ] ,
96
+ meta : { totals : { mrr : 1020 } }
97
+ }
98
+ } ;
99
+
100
+ /**
101
+ * Common Ghost API handlers
102
+ */
103
+ export const handlers = {
104
+ // Global data handlers
105
+ browseConfig : http . get ( '/ghost/api/admin/config/' , ( ) => {
106
+ return HttpResponse . json ( fixtures . config ) ;
107
+ } ) ,
108
+
109
+ browseSettings : http . get ( '/ghost/api/admin/settings/' , ( ) => {
110
+ return HttpResponse . json ( { settings : [ fixtures . settings ] } ) ;
111
+ } ) ,
112
+
113
+ browseSite : http . get ( '/ghost/api/admin/site/' , ( ) => {
114
+ return HttpResponse . json ( { site : fixtures . site } ) ;
115
+ } ) ,
116
+
117
+ browseMe : http . get ( '/ghost/api/admin/users/me/' , ( ) => {
118
+ return HttpResponse . json ( { users : [ fixtures . user ] } ) ;
119
+ } ) ,
120
+
121
+ // Links handlers
122
+ updateLinksBulk : http . put ( '/ghost/api/admin/links/bulk/' , ( ) => {
123
+ return HttpResponse . json ( fixtures . linksBulkSuccess ) ;
124
+ } ) ,
125
+
126
+ // Stats handlers
127
+ browseMemberCountHistory : http . get ( '/ghost/api/admin/stats/member_count/' , ( ) => {
128
+ return HttpResponse . json ( fixtures . memberCountHistory ) ;
129
+ } ) ,
130
+
131
+ browseMrrHistory : http . get ( '/ghost/api/admin/stats/mrr/' , ( ) => {
132
+ return HttpResponse . json ( fixtures . mrrHistory ) ;
133
+ } )
134
+ } ;
135
+
136
+ /**
137
+ * Creates an MSW server with common Ghost API handlers
138
+ */
139
+ export function createMswServer ( additionalHandlers : Array < ReturnType < typeof http . get | typeof http . post | typeof http . put | typeof http . delete > > = [ ] ) {
140
+ return setupServer (
141
+ ...Object . values ( handlers ) ,
142
+ ...additionalHandlers
143
+ ) ;
144
+ }
145
+
146
+ /**
147
+ * Sets up MSW server for testing with common lifecycle management
148
+ */
149
+ export function setupMswServer ( additionalHandlers : Array < ReturnType < typeof http . get | typeof http . post | typeof http . put | typeof http . delete > > = [ ] ) {
150
+ const server = createMswServer ( additionalHandlers ) ;
151
+
152
+ beforeAll ( ( ) => server . listen ( ) ) ;
153
+ afterEach ( ( ) => {
154
+ server . resetHandlers ( ) ;
155
+ } ) ;
156
+ afterAll ( ( ) => server . close ( ) ) ;
157
+
158
+ return server ;
159
+ }
160
+
161
+ /**
162
+ * Helper to create custom handlers that override defaults
163
+ */
164
+ export function createHandler ( method : 'get' | 'post' | 'put' | 'delete' , path : string , response : Record < string , unknown > | Array < unknown > , status = 200 ) {
165
+ const httpMethod = http [ method ] ;
166
+ return httpMethod ( path , ( ) => {
167
+ if ( status >= 400 ) {
168
+ return new HttpResponse ( null , { status} ) ;
169
+ }
170
+ return HttpResponse . json ( response ) ;
171
+ } ) ;
172
+ }
173
+
174
+ /**
175
+ * Helper to create error handlers
176
+ */
177
+ export function createErrorHandler ( method : 'get' | 'post' | 'put' | 'delete' , path : string , status = 500 ) {
178
+ const httpMethod = http [ method ] ;
179
+ return httpMethod ( path , ( ) => {
180
+ return new HttpResponse ( null , { status} ) ;
181
+ } ) ;
182
+ }
0 commit comments