|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | + |
| 3 | +/** |
| 4 | + * PR #678 測試: 修復 CompetitionPage 中 NaN 和缺失數據的顯示問題 |
| 5 | + * |
| 6 | + * 問題:當 total_pnl_pct 為 null/undefined/NaN 時,會顯示 "NaN%" 或 "0.00%" |
| 7 | + * 修復:檢查數據有效性,顯示 "—" 表示缺失數據 |
| 8 | + */ |
| 9 | + |
| 10 | +describe('CompetitionPage - Data Validation Logic (PR #678)', () => { |
| 11 | + /** |
| 12 | + * 測試數據有效性檢查邏輯 |
| 13 | + * 這是 PR #678 引入的核心邏輯 |
| 14 | + */ |
| 15 | + describe('hasValidData check', () => { |
| 16 | + it('should return true for valid numbers', () => { |
| 17 | + const trader1 = { total_pnl_pct: 10.5 } |
| 18 | + const trader2 = { total_pnl_pct: -5.2 } |
| 19 | + |
| 20 | + const hasValidData = |
| 21 | + trader1.total_pnl_pct != null && |
| 22 | + trader2.total_pnl_pct != null && |
| 23 | + !isNaN(trader1.total_pnl_pct) && |
| 24 | + !isNaN(trader2.total_pnl_pct) |
| 25 | + |
| 26 | + expect(hasValidData).toBe(true) |
| 27 | + }) |
| 28 | + |
| 29 | + it('should return false when trader1 has null value', () => { |
| 30 | + const trader1 = { total_pnl_pct: null } |
| 31 | + const trader2 = { total_pnl_pct: 10.5 } |
| 32 | + |
| 33 | + const hasValidData = |
| 34 | + trader1.total_pnl_pct != null && |
| 35 | + trader2.total_pnl_pct != null && |
| 36 | + !isNaN(trader1.total_pnl_pct!) && |
| 37 | + !isNaN(trader2.total_pnl_pct) |
| 38 | + |
| 39 | + expect(hasValidData).toBe(false) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should return false when trader2 has undefined value', () => { |
| 43 | + const trader1 = { total_pnl_pct: 10.5 } |
| 44 | + const trader2 = { total_pnl_pct: undefined } |
| 45 | + |
| 46 | + const hasValidData = |
| 47 | + trader1.total_pnl_pct != null && |
| 48 | + trader2.total_pnl_pct != null && |
| 49 | + !isNaN(trader1.total_pnl_pct) && |
| 50 | + !isNaN(trader2.total_pnl_pct!) |
| 51 | + |
| 52 | + expect(hasValidData).toBe(false) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should return false when trader1 has NaN value', () => { |
| 56 | + const trader1 = { total_pnl_pct: NaN } |
| 57 | + const trader2 = { total_pnl_pct: 10.5 } |
| 58 | + |
| 59 | + const hasValidData = |
| 60 | + trader1.total_pnl_pct != null && |
| 61 | + trader2.total_pnl_pct != null && |
| 62 | + !isNaN(trader1.total_pnl_pct) && |
| 63 | + !isNaN(trader2.total_pnl_pct) |
| 64 | + |
| 65 | + expect(hasValidData).toBe(false) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should return false when both traders have invalid data', () => { |
| 69 | + const trader1 = { total_pnl_pct: null } |
| 70 | + const trader2 = { total_pnl_pct: NaN } |
| 71 | + |
| 72 | + const hasValidData = |
| 73 | + trader1.total_pnl_pct != null && |
| 74 | + trader2.total_pnl_pct != null && |
| 75 | + !isNaN(trader1.total_pnl_pct!) && |
| 76 | + !isNaN(trader2.total_pnl_pct) |
| 77 | + |
| 78 | + expect(hasValidData).toBe(false) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should handle zero as valid data', () => { |
| 82 | + const trader1 = { total_pnl_pct: 0 } |
| 83 | + const trader2 = { total_pnl_pct: 10.5 } |
| 84 | + |
| 85 | + const hasValidData = |
| 86 | + trader1.total_pnl_pct != null && |
| 87 | + trader2.total_pnl_pct != null && |
| 88 | + !isNaN(trader1.total_pnl_pct) && |
| 89 | + !isNaN(trader2.total_pnl_pct) |
| 90 | + |
| 91 | + expect(hasValidData).toBe(true) |
| 92 | + }) |
| 93 | + |
| 94 | + it('should handle negative numbers as valid data', () => { |
| 95 | + const trader1 = { total_pnl_pct: -15.5 } |
| 96 | + const trader2 = { total_pnl_pct: -8.2 } |
| 97 | + |
| 98 | + const hasValidData = |
| 99 | + trader1.total_pnl_pct != null && |
| 100 | + trader2.total_pnl_pct != null && |
| 101 | + !isNaN(trader1.total_pnl_pct) && |
| 102 | + !isNaN(trader2.total_pnl_pct) |
| 103 | + |
| 104 | + expect(hasValidData).toBe(true) |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + /** |
| 109 | + * 測試 gap 計算邏輯 |
| 110 | + * gap 應該只在數據有效時計算 |
| 111 | + */ |
| 112 | + describe('gap calculation', () => { |
| 113 | + it('should calculate gap correctly for valid data', () => { |
| 114 | + const trader1 = { total_pnl_pct: 15.5 } |
| 115 | + const trader2 = { total_pnl_pct: 10.2 } |
| 116 | + |
| 117 | + const hasValidData = |
| 118 | + trader1.total_pnl_pct != null && |
| 119 | + trader2.total_pnl_pct != null && |
| 120 | + !isNaN(trader1.total_pnl_pct) && |
| 121 | + !isNaN(trader2.total_pnl_pct) |
| 122 | + |
| 123 | + const gap = hasValidData |
| 124 | + ? trader1.total_pnl_pct - trader2.total_pnl_pct |
| 125 | + : NaN |
| 126 | + |
| 127 | + expect(gap).toBeCloseTo(5.3, 1) // Allow floating point precision |
| 128 | + expect(isNaN(gap)).toBe(false) |
| 129 | + }) |
| 130 | + |
| 131 | + it('should return NaN for invalid data', () => { |
| 132 | + const trader1 = { total_pnl_pct: null } |
| 133 | + const trader2 = { total_pnl_pct: 10.2 } |
| 134 | + |
| 135 | + const hasValidData = |
| 136 | + trader1.total_pnl_pct != null && |
| 137 | + trader2.total_pnl_pct != null && |
| 138 | + !isNaN(trader1.total_pnl_pct!) && |
| 139 | + !isNaN(trader2.total_pnl_pct) |
| 140 | + |
| 141 | + const gap = hasValidData |
| 142 | + ? trader1.total_pnl_pct! - trader2.total_pnl_pct |
| 143 | + : NaN |
| 144 | + |
| 145 | + expect(isNaN(gap)).toBe(true) |
| 146 | + }) |
| 147 | + |
| 148 | + it('should handle negative gap correctly', () => { |
| 149 | + const trader1 = { total_pnl_pct: 5.0 } |
| 150 | + const trader2 = { total_pnl_pct: 12.0 } |
| 151 | + |
| 152 | + const hasValidData = |
| 153 | + trader1.total_pnl_pct != null && |
| 154 | + trader2.total_pnl_pct != null && |
| 155 | + !isNaN(trader1.total_pnl_pct) && |
| 156 | + !isNaN(trader2.total_pnl_pct) |
| 157 | + |
| 158 | + const gap = hasValidData |
| 159 | + ? trader1.total_pnl_pct - trader2.total_pnl_pct |
| 160 | + : NaN |
| 161 | + |
| 162 | + expect(gap).toBe(-7.0) |
| 163 | + }) |
| 164 | + }) |
| 165 | + |
| 166 | + /** |
| 167 | + * 測試顯示邏輯 |
| 168 | + * 修復後應顯示「—」而非「NaN%」或「0.00%」 |
| 169 | + */ |
| 170 | + describe('display formatting', () => { |
| 171 | + it('should format valid positive percentage correctly', () => { |
| 172 | + const total_pnl_pct = 15.567 |
| 173 | + |
| 174 | + const display = |
| 175 | + total_pnl_pct != null && !isNaN(total_pnl_pct) |
| 176 | + ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` |
| 177 | + : '—' |
| 178 | + |
| 179 | + expect(display).toBe('+15.57%') |
| 180 | + }) |
| 181 | + |
| 182 | + it('should format valid negative percentage correctly', () => { |
| 183 | + const total_pnl_pct = -8.234 |
| 184 | + |
| 185 | + const display = |
| 186 | + total_pnl_pct != null && !isNaN(total_pnl_pct) |
| 187 | + ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` |
| 188 | + : '—' |
| 189 | + |
| 190 | + expect(display).toBe('-8.23%') |
| 191 | + }) |
| 192 | + |
| 193 | + it('should display "—" for null value', () => { |
| 194 | + const total_pnl_pct = null |
| 195 | + |
| 196 | + const display = |
| 197 | + total_pnl_pct != null && !isNaN(total_pnl_pct) |
| 198 | + ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` |
| 199 | + : '—' |
| 200 | + |
| 201 | + expect(display).toBe('—') |
| 202 | + }) |
| 203 | + |
| 204 | + it('should display "—" for undefined value', () => { |
| 205 | + const total_pnl_pct = undefined |
| 206 | + |
| 207 | + const display = |
| 208 | + total_pnl_pct != null && !isNaN(total_pnl_pct) |
| 209 | + ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` |
| 210 | + : '—' |
| 211 | + |
| 212 | + expect(display).toBe('—') |
| 213 | + }) |
| 214 | + |
| 215 | + it('should display "—" for NaN value', () => { |
| 216 | + const total_pnl_pct = NaN |
| 217 | + |
| 218 | + const display = |
| 219 | + total_pnl_pct != null && !isNaN(total_pnl_pct) |
| 220 | + ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` |
| 221 | + : '—' |
| 222 | + |
| 223 | + expect(display).toBe('—') |
| 224 | + }) |
| 225 | + |
| 226 | + it('should format zero correctly', () => { |
| 227 | + const total_pnl_pct = 0 |
| 228 | + |
| 229 | + const display = |
| 230 | + total_pnl_pct != null && !isNaN(total_pnl_pct) |
| 231 | + ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` |
| 232 | + : '—' |
| 233 | + |
| 234 | + expect(display).toBe('+0.00%') |
| 235 | + }) |
| 236 | + }) |
| 237 | + |
| 238 | + /** |
| 239 | + * 測試領先/落後訊息顯示邏輯 |
| 240 | + * 只有在數據有效時才顯示 "領先" 或 "落後" 訊息 |
| 241 | + */ |
| 242 | + describe('leading/trailing message display', () => { |
| 243 | + it('should show leading message when winning with positive gap', () => { |
| 244 | + const isWinning = true |
| 245 | + const gap = 5.2 |
| 246 | + const hasValidData = true |
| 247 | + |
| 248 | + const shouldShowLeading = hasValidData && isWinning && gap > 0 |
| 249 | + |
| 250 | + expect(shouldShowLeading).toBe(true) |
| 251 | + }) |
| 252 | + |
| 253 | + it('should not show leading message when data is invalid', () => { |
| 254 | + const isWinning = true |
| 255 | + const gap = NaN |
| 256 | + const hasValidData = false |
| 257 | + |
| 258 | + const shouldShowLeading = hasValidData && isWinning && gap > 0 |
| 259 | + |
| 260 | + expect(shouldShowLeading).toBe(false) |
| 261 | + }) |
| 262 | + |
| 263 | + it('should show trailing message when losing with negative gap', () => { |
| 264 | + const isWinning = false |
| 265 | + const gap = -3.5 |
| 266 | + const hasValidData = true |
| 267 | + |
| 268 | + const shouldShowTrailing = hasValidData && !isWinning && gap < 0 |
| 269 | + |
| 270 | + expect(shouldShowTrailing).toBe(true) |
| 271 | + }) |
| 272 | + |
| 273 | + it('should not show trailing message when data is invalid', () => { |
| 274 | + const isWinning = false |
| 275 | + const gap = NaN |
| 276 | + const hasValidData = false |
| 277 | + |
| 278 | + const shouldShowTrailing = hasValidData && !isWinning && gap < 0 |
| 279 | + |
| 280 | + expect(shouldShowTrailing).toBe(false) |
| 281 | + }) |
| 282 | + |
| 283 | + it('should show fallback "—" when data is invalid', () => { |
| 284 | + const hasValidData = false |
| 285 | + |
| 286 | + const shouldShowFallback = !hasValidData |
| 287 | + |
| 288 | + expect(shouldShowFallback).toBe(true) |
| 289 | + }) |
| 290 | + }) |
| 291 | + |
| 292 | + /** |
| 293 | + * 測試邊界情況 |
| 294 | + */ |
| 295 | + describe('edge cases', () => { |
| 296 | + it('should handle very small positive numbers', () => { |
| 297 | + const total_pnl_pct = 0.001 |
| 298 | + |
| 299 | + const hasValidData = total_pnl_pct != null && !isNaN(total_pnl_pct) |
| 300 | + |
| 301 | + expect(hasValidData).toBe(true) |
| 302 | + }) |
| 303 | + |
| 304 | + it('should handle very large numbers', () => { |
| 305 | + const total_pnl_pct = 9999.99 |
| 306 | + |
| 307 | + const hasValidData = total_pnl_pct != null && !isNaN(total_pnl_pct) |
| 308 | + |
| 309 | + expect(hasValidData).toBe(true) |
| 310 | + }) |
| 311 | + |
| 312 | + it('should handle Infinity as invalid (produces NaN in calculations)', () => { |
| 313 | + const total_pnl_pct = Infinity |
| 314 | + |
| 315 | + // Infinity 本身不是 NaN,但在減法運算中可能導致問題 |
| 316 | + const hasValidData = total_pnl_pct != null && isFinite(total_pnl_pct) |
| 317 | + |
| 318 | + expect(hasValidData).toBe(false) |
| 319 | + }) |
| 320 | + |
| 321 | + it('should handle -Infinity as invalid', () => { |
| 322 | + const total_pnl_pct = -Infinity |
| 323 | + |
| 324 | + const hasValidData = total_pnl_pct != null && isFinite(total_pnl_pct) |
| 325 | + |
| 326 | + expect(hasValidData).toBe(false) |
| 327 | + }) |
| 328 | + }) |
| 329 | +}) |
0 commit comments