| 1 |
/** |
| 2 |
* Shared test utilities for the Analytics app. |
| 3 |
* |
| 4 |
* Provides: |
| 5 |
* - A pre-configured Redux store factory |
| 6 |
* - A custom `render` that wraps components in Provider + MemoryRouter |
| 7 |
* - Mock data factories for every entity the app uses |
| 8 |
*/ |
| 9 |
import { configureStore } from '@reduxjs/toolkit'; |
| 10 |
import { render } from '@testing-library/react'; |
| 11 |
import { Provider } from 'react-redux'; |
| 12 |
import { MemoryRouter } from 'react-router'; |
| 13 |
import { analyticsApi } from '../../features/api/analyticsApi'; |
| 14 |
import dateRangeReducer from '../../features/dateRange/dateRangeSlice'; |
| 15 |
|
| 16 |
// --------------------------------------------------------------------------- |
| 17 |
// Store factory |
| 18 |
// --------------------------------------------------------------------------- |
| 19 |
export function createTestStore(preloadedState = {}) { |
| 20 |
return configureStore({ |
| 21 |
reducer: { |
| 22 |
[analyticsApi.reducerPath]: analyticsApi.reducer, |
| 23 |
dateRange: dateRangeReducer, |
| 24 |
}, |
| 25 |
middleware: (getDefaultMiddleware) => |
| 26 |
getDefaultMiddleware().concat(analyticsApi.middleware), |
| 27 |
preloadedState, |
| 28 |
}); |
| 29 |
} |
| 30 |
|
| 31 |
// --------------------------------------------------------------------------- |
| 32 |
// Custom render with all providers |
| 33 |
// --------------------------------------------------------------------------- |
| 34 |
export function renderWithProviders( |
| 35 |
ui, |
| 36 |
{ |
| 37 |
preloadedState = {}, |
| 38 |
store = createTestStore(preloadedState), |
| 39 |
route = '/', |
| 40 |
...renderOptions |
| 41 |
} = {} |
| 42 |
) { |
| 43 |
function Wrapper({ children }) { |
| 44 |
return ( |
| 45 |
<Provider store={store}> |
| 46 |
<MemoryRouter initialEntries={[route]}>{children}</MemoryRouter> |
| 47 |
</Provider> |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
return { |
| 52 |
store, |
| 53 |
...render(ui, { wrapper: Wrapper, ...renderOptions }), |
| 54 |
}; |
| 55 |
} |
| 56 |
|
| 57 |
// --------------------------------------------------------------------------- |
| 58 |
// Mock data factories |
| 59 |
// --------------------------------------------------------------------------- |
| 60 |
export function createMockSummary(overrides = {}) { |
| 61 |
return { |
| 62 |
data: { |
| 63 |
active_campaigns: { |
| 64 |
current: 12, |
| 65 |
previous: 10, |
| 66 |
change_percent: 20.0, |
| 67 |
trend: 'up', |
| 68 |
}, |
| 69 |
net_sales: { |
| 70 |
current: 45230.5, |
| 71 |
previous: 38120.25, |
| 72 |
change_percent: 18.6, |
| 73 |
trend: 'up', |
| 74 |
}, |
| 75 |
discount_sales: { |
| 76 |
current: 12450.0, |
| 77 |
previous: 10200.0, |
| 78 |
change_percent: 22.1, |
| 79 |
trend: 'up', |
| 80 |
}, |
| 81 |
total_orders: { |
| 82 |
current: 342, |
| 83 |
previous: 290, |
| 84 |
change_percent: 17.9, |
| 85 |
trend: 'up', |
| 86 |
}, |
| 87 |
disco_orders: { |
| 88 |
current: 156, |
| 89 |
previous: 180, |
| 90 |
change_percent: -13.3, |
| 91 |
trend: 'down', |
| 92 |
}, |
| 93 |
customers: { |
| 94 |
current: 98, |
| 95 |
previous: 85, |
| 96 |
change_percent: 15.3, |
| 97 |
trend: 'up', |
| 98 |
}, |
| 99 |
...overrides, |
| 100 |
}, |
| 101 |
}; |
| 102 |
} |
| 103 |
|
| 104 |
export function createMockCampaign(overrides = {}) { |
| 105 |
return { |
| 106 |
campaign_id: 1, |
| 107 |
campaign_name: 'Summer Sale', |
| 108 |
intent: 'Product', |
| 109 |
valid_date: { from: '2025-06-01', to: '2025-06-30' }, |
| 110 |
orders: 45, |
| 111 |
customers: 32, |
| 112 |
revenue: 12500.0, |
| 113 |
status: 'active', |
| 114 |
total_orders: 45, |
| 115 |
total_customers: 32, |
| 116 |
average_order_value: 277.78, |
| 117 |
...overrides, |
| 118 |
}; |
| 119 |
} |
| 120 |
|
| 121 |
export function createMockCampaignsList(count = 3) { |
| 122 |
return { |
| 123 |
data: Array.from({ length: count }, (_, i) => |
| 124 |
createMockCampaign({ |
| 125 |
campaign_id: i + 1, |
| 126 |
campaign_name: `Campaign ${i + 1}`, |
| 127 |
revenue: 10000 - i * 1000, |
| 128 |
}) |
| 129 |
), |
| 130 |
collection: { count, total: count, total_pages: 1 }, |
| 131 |
}; |
| 132 |
} |
| 133 |
|
| 134 |
export function createMockProduct(overrides = {}) { |
| 135 |
return { |
| 136 |
id: 1, |
| 137 |
name: 'Premium Widget', |
| 138 |
image: 'https://example.com/widget.jpg', |
| 139 |
unit_price: 49.99, |
| 140 |
categories: [{ name: 'Widgets' }], |
| 141 |
campaigns: [{ name: 'Summer Sale' }], |
| 142 |
total_orders: 120, |
| 143 |
total_customers: 85, |
| 144 |
total_revenue: 5999.8, |
| 145 |
...overrides, |
| 146 |
}; |
| 147 |
} |
| 148 |
|
| 149 |
export function createMockProductsList(count = 3) { |
| 150 |
return { |
| 151 |
data: Array.from({ length: count }, (_, i) => |
| 152 |
createMockProduct({ |
| 153 |
id: i + 1, |
| 154 |
name: `Product ${i + 1}`, |
| 155 |
total_revenue: 5000 - i * 500, |
| 156 |
}) |
| 157 |
), |
| 158 |
collection: { count, total: count, total_pages: 1 }, |
| 159 |
}; |
| 160 |
} |
| 161 |
|
| 162 |
export function createMockOrder(overrides = {}) { |
| 163 |
return { |
| 164 |
id: 101, |
| 165 |
customer_name: 'John Doe', |
| 166 |
campaigns: [{ name: 'Summer Sale' }], |
| 167 |
products: [{ name: 'Premium Widget' }], |
| 168 |
revenue: 149.99, |
| 169 |
quantity: 2, |
| 170 |
date: '2025-05-10', |
| 171 |
...overrides, |
| 172 |
}; |
| 173 |
} |
| 174 |
|
| 175 |
export function createMockOrdersList(count = 3) { |
| 176 |
return { |
| 177 |
data: Array.from({ length: count }, (_, i) => |
| 178 |
createMockOrder({ |
| 179 |
id: 101 + i, |
| 180 |
customer_name: `Customer ${i + 1}`, |
| 181 |
revenue: 200 - i * 25, |
| 182 |
}) |
| 183 |
), |
| 184 |
collection: { count, total: count, total_pages: 1 }, |
| 185 |
}; |
| 186 |
} |
| 187 |
|
| 188 |
export function createMockCustomer(overrides = {}) { |
| 189 |
return { |
| 190 |
id: 1, |
| 191 |
name: 'Jane Smith', |
| 192 |
email: '[email protected]', |
| 193 |
city: 'New York', |
| 194 |
avatar: '', |
| 195 |
campaigns: [{ name: 'Summer Sale' }], |
| 196 |
orders: 5, |
| 197 |
total_spent: 1250.0, |
| 198 |
...overrides, |
| 199 |
}; |
| 200 |
} |
| 201 |
|
| 202 |
export function createMockCustomersList(count = 3) { |
| 203 |
return { |
| 204 |
data: Array.from({ length: count }, (_, i) => |
| 205 |
createMockCustomer({ |
| 206 |
id: i + 1, |
| 207 |
name: `Customer ${i + 1}`, |
| 208 |
email: `customer${i + 1}@example.com`, |
| 209 |
total_spent: 2000 - i * 200, |
| 210 |
}) |
| 211 |
), |
| 212 |
collection: { count, total: count, total_pages: 1 }, |
| 213 |
}; |
| 214 |
} |
| 215 |
|
| 216 |
export function createMockRevenueChart(days = 7) { |
| 217 |
return { |
| 218 |
data: Array.from({ length: days }, (_, i) => ({ |
| 219 |
date: `2025-05-${String(i + 1).padStart(2, '0')}`, |
| 220 |
net_sales: 1000 + i * 100, |
| 221 |
discount_sales: 300 + i * 30, |
| 222 |
})), |
| 223 |
interval: 'day', |
| 224 |
}; |
| 225 |
} |
| 226 |
|
| 227 |
export function createMockIntentsPerformance() { |
| 228 |
return { |
| 229 |
data: [ |
| 230 |
{ intent: 'Product', revenue: 5000, orders: 50, percentage: 40 }, |
| 231 |
{ intent: 'Cart', revenue: 2500, orders: 25, percentage: 20 }, |
| 232 |
{ intent: 'Shipping', revenue: 1875, orders: 18, percentage: 15 }, |
| 233 |
{ intent: 'Bulk', revenue: 1250, orders: 12, percentage: 10 }, |
| 234 |
{ intent: 'BOGO', revenue: 1000, orders: 10, percentage: 8 }, |
| 235 |
{ intent: 'Bundle', revenue: 625, orders: 6, percentage: 5 }, |
| 236 |
{ intent: 'Others', revenue: 250, orders: 3, percentage: 2 }, |
| 237 |
], |
| 238 |
total_revenue: 12500, |
| 239 |
}; |
| 240 |
} |
| 241 |
|