|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | 2 | import { |
3 | 3 | calculateMonthlyTotal, |
| 4 | + calculateYearlyTotal, |
4 | 5 | convertCurrency, |
5 | 6 | formatPrice, |
6 | 7 | normalizeToMonthly, |
| 8 | + normalizeToYearly, |
7 | 9 | type SubscriptionForCalculation, |
8 | 10 | } from './currency'; |
9 | 11 |
|
@@ -44,8 +46,9 @@ describe('normalizeToMonthly', () => { |
44 | 46 | expect(normalizeToMonthly(12000, 'yearly')).toBe(1000); |
45 | 47 | }); |
46 | 48 |
|
47 | | - it('multiplies weekly price by 4', () => { |
48 | | - expect(normalizeToMonthly(2500, 'weekly')).toBe(10000); |
| 49 | + it('multiplies weekly price by 52/12 for accurate monthly estimate', () => { |
| 50 | + // 2500 * (52/12) ≈ 10833.33 |
| 51 | + expect(normalizeToMonthly(2500, 'weekly')).toBeCloseTo(10833.33, 1); |
49 | 52 | }); |
50 | 53 |
|
51 | 54 | it('divides quarterly price by 3', () => { |
@@ -192,3 +195,101 @@ describe('calculateMonthlyTotal', () => { |
192 | 195 | expect(result.hasMixedCurrencies).toBe(true); |
193 | 196 | }); |
194 | 197 | }); |
| 198 | + |
| 199 | +describe('normalizeToYearly', () => { |
| 200 | + it('returns same price for yearly billing', () => { |
| 201 | + expect(normalizeToYearly(120000, 'yearly')).toBe(120000); |
| 202 | + }); |
| 203 | + |
| 204 | + it('multiplies monthly price by 12', () => { |
| 205 | + expect(normalizeToYearly(10000, 'monthly')).toBe(120000); |
| 206 | + }); |
| 207 | + |
| 208 | + it('multiplies weekly price by 52', () => { |
| 209 | + expect(normalizeToYearly(1000, 'weekly')).toBe(52000); |
| 210 | + }); |
| 211 | + |
| 212 | + it('multiplies quarterly price by 4', () => { |
| 213 | + expect(normalizeToYearly(30000, 'quarterly')).toBe(120000); |
| 214 | + }); |
| 215 | +}); |
| 216 | + |
| 217 | +describe('calculateYearlyTotal', () => { |
| 218 | + it('returns 0 for empty subscriptions', () => { |
| 219 | + const result = calculateYearlyTotal([], 'KRW'); |
| 220 | + expect(result.total).toBe(0); |
| 221 | + expect(result.hasMixedCurrencies).toBe(false); |
| 222 | + }); |
| 223 | + |
| 224 | + it('calculates yearly total for monthly subscription', () => { |
| 225 | + const subscriptions: SubscriptionForCalculation[] = [ |
| 226 | + { |
| 227 | + price: 10000, |
| 228 | + currency: 'KRW', |
| 229 | + billingCycle: 'monthly', |
| 230 | + isActive: true, |
| 231 | + }, |
| 232 | + ]; |
| 233 | + // ₩10,000/month * 12 = ₩120,000/year |
| 234 | + const result = calculateYearlyTotal(subscriptions, 'KRW'); |
| 235 | + expect(result.total).toBe(120000); |
| 236 | + }); |
| 237 | + |
| 238 | + it('calculates yearly total for weekly subscription (52 weeks)', () => { |
| 239 | + const subscriptions: SubscriptionForCalculation[] = [ |
| 240 | + { price: 1000, currency: 'KRW', billingCycle: 'weekly', isActive: true }, |
| 241 | + ]; |
| 242 | + // ₩1,000/week * 52 = ₩52,000/year |
| 243 | + const result = calculateYearlyTotal(subscriptions, 'KRW'); |
| 244 | + expect(result.total).toBe(52000); |
| 245 | + }); |
| 246 | + |
| 247 | + it('returns same amount for yearly subscription', () => { |
| 248 | + const subscriptions: SubscriptionForCalculation[] = [ |
| 249 | + { |
| 250 | + price: 120000, |
| 251 | + currency: 'KRW', |
| 252 | + billingCycle: 'yearly', |
| 253 | + isActive: true, |
| 254 | + }, |
| 255 | + ]; |
| 256 | + const result = calculateYearlyTotal(subscriptions, 'KRW'); |
| 257 | + expect(result.total).toBe(120000); |
| 258 | + }); |
| 259 | + |
| 260 | + it('calculates yearly total for quarterly subscription', () => { |
| 261 | + const subscriptions: SubscriptionForCalculation[] = [ |
| 262 | + { |
| 263 | + price: 30000, |
| 264 | + currency: 'KRW', |
| 265 | + billingCycle: 'quarterly', |
| 266 | + isActive: true, |
| 267 | + }, |
| 268 | + ]; |
| 269 | + // ₩30,000/quarter * 4 = ₩120,000/year |
| 270 | + const result = calculateYearlyTotal(subscriptions, 'KRW'); |
| 271 | + expect(result.total).toBe(120000); |
| 272 | + }); |
| 273 | + |
| 274 | + it('handles complex scenario with mixed currencies and billing cycles', () => { |
| 275 | + const subscriptions: SubscriptionForCalculation[] = [ |
| 276 | + { |
| 277 | + price: 10000, |
| 278 | + currency: 'KRW', |
| 279 | + billingCycle: 'monthly', |
| 280 | + isActive: true, |
| 281 | + }, |
| 282 | + { price: 120, currency: 'USD', billingCycle: 'yearly', isActive: true }, |
| 283 | + { price: 30, currency: 'EUR', billingCycle: 'quarterly', isActive: true }, |
| 284 | + { price: 500, currency: 'JPY', billingCycle: 'weekly', isActive: false }, |
| 285 | + ]; |
| 286 | + // KRW: ₩10,000/month * 12 = ₩120,000/year |
| 287 | + // USD: $120/year = ₩168,000/year |
| 288 | + // EUR: €30/quarter * 4 = €120/year = ₩180,000/year |
| 289 | + // JPY: inactive, ignored |
| 290 | + // Total: ₩468,000 |
| 291 | + const result = calculateYearlyTotal(subscriptions, 'KRW'); |
| 292 | + expect(result.total).toBe(468000); |
| 293 | + expect(result.hasMixedCurrencies).toBe(true); |
| 294 | + }); |
| 295 | +}); |
0 commit comments