-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix(web): display '—' for missing data instead of NaN% or 0% #678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hzb1115
merged 2 commits into
NoFxAiOS:dev
from
the-dev-z:fix/competition-missing-data-display
Nov 11, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,329 @@ | ||
| import { describe, it, expect } from 'vitest' | ||
|
|
||
| /** | ||
| * PR #678 測試: 修復 CompetitionPage 中 NaN 和缺失數據的顯示問題 | ||
| * | ||
| * 問題:當 total_pnl_pct 為 null/undefined/NaN 時,會顯示 "NaN%" 或 "0.00%" | ||
| * 修復:檢查數據有效性,顯示 "—" 表示缺失數據 | ||
| */ | ||
|
|
||
| describe('CompetitionPage - Data Validation Logic (PR #678)', () => { | ||
| /** | ||
| * 測試數據有效性檢查邏輯 | ||
| * 這是 PR #678 引入的核心邏輯 | ||
| */ | ||
| describe('hasValidData check', () => { | ||
| it('should return true for valid numbers', () => { | ||
| const trader1 = { total_pnl_pct: 10.5 } | ||
| const trader2 = { total_pnl_pct: -5.2 } | ||
|
|
||
| const hasValidData = | ||
| trader1.total_pnl_pct != null && | ||
| trader2.total_pnl_pct != null && | ||
| !isNaN(trader1.total_pnl_pct) && | ||
| !isNaN(trader2.total_pnl_pct) | ||
|
|
||
| expect(hasValidData).toBe(true) | ||
| }) | ||
|
|
||
| it('should return false when trader1 has null value', () => { | ||
| const trader1 = { total_pnl_pct: null } | ||
| const trader2 = { total_pnl_pct: 10.5 } | ||
|
|
||
| const hasValidData = | ||
| trader1.total_pnl_pct != null && | ||
| trader2.total_pnl_pct != null && | ||
| !isNaN(trader1.total_pnl_pct!) && | ||
| !isNaN(trader2.total_pnl_pct) | ||
|
|
||
| expect(hasValidData).toBe(false) | ||
| }) | ||
|
|
||
| it('should return false when trader2 has undefined value', () => { | ||
| const trader1 = { total_pnl_pct: 10.5 } | ||
| const trader2 = { total_pnl_pct: undefined } | ||
|
|
||
| const hasValidData = | ||
| trader1.total_pnl_pct != null && | ||
| trader2.total_pnl_pct != null && | ||
| !isNaN(trader1.total_pnl_pct) && | ||
| !isNaN(trader2.total_pnl_pct!) | ||
|
|
||
| expect(hasValidData).toBe(false) | ||
| }) | ||
|
|
||
| it('should return false when trader1 has NaN value', () => { | ||
| const trader1 = { total_pnl_pct: NaN } | ||
| const trader2 = { total_pnl_pct: 10.5 } | ||
|
|
||
| const hasValidData = | ||
| trader1.total_pnl_pct != null && | ||
| trader2.total_pnl_pct != null && | ||
| !isNaN(trader1.total_pnl_pct) && | ||
| !isNaN(trader2.total_pnl_pct) | ||
|
|
||
| expect(hasValidData).toBe(false) | ||
| }) | ||
|
|
||
| it('should return false when both traders have invalid data', () => { | ||
| const trader1 = { total_pnl_pct: null } | ||
| const trader2 = { total_pnl_pct: NaN } | ||
|
|
||
| const hasValidData = | ||
| trader1.total_pnl_pct != null && | ||
| trader2.total_pnl_pct != null && | ||
| !isNaN(trader1.total_pnl_pct!) && | ||
| !isNaN(trader2.total_pnl_pct) | ||
|
|
||
| expect(hasValidData).toBe(false) | ||
| }) | ||
|
|
||
| it('should handle zero as valid data', () => { | ||
| const trader1 = { total_pnl_pct: 0 } | ||
| const trader2 = { total_pnl_pct: 10.5 } | ||
|
|
||
| const hasValidData = | ||
| trader1.total_pnl_pct != null && | ||
| trader2.total_pnl_pct != null && | ||
| !isNaN(trader1.total_pnl_pct) && | ||
| !isNaN(trader2.total_pnl_pct) | ||
|
|
||
| expect(hasValidData).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle negative numbers as valid data', () => { | ||
| const trader1 = { total_pnl_pct: -15.5 } | ||
| const trader2 = { total_pnl_pct: -8.2 } | ||
|
|
||
| const hasValidData = | ||
| trader1.total_pnl_pct != null && | ||
| trader2.total_pnl_pct != null && | ||
| !isNaN(trader1.total_pnl_pct) && | ||
| !isNaN(trader2.total_pnl_pct) | ||
|
|
||
| expect(hasValidData).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| /** | ||
| * 測試 gap 計算邏輯 | ||
| * gap 應該只在數據有效時計算 | ||
| */ | ||
| describe('gap calculation', () => { | ||
| it('should calculate gap correctly for valid data', () => { | ||
| const trader1 = { total_pnl_pct: 15.5 } | ||
| const trader2 = { total_pnl_pct: 10.2 } | ||
|
|
||
| const hasValidData = | ||
| trader1.total_pnl_pct != null && | ||
| trader2.total_pnl_pct != null && | ||
| !isNaN(trader1.total_pnl_pct) && | ||
| !isNaN(trader2.total_pnl_pct) | ||
|
|
||
| const gap = hasValidData | ||
| ? trader1.total_pnl_pct - trader2.total_pnl_pct | ||
| : NaN | ||
|
|
||
| expect(gap).toBeCloseTo(5.3, 1) // Allow floating point precision | ||
| expect(isNaN(gap)).toBe(false) | ||
| }) | ||
|
|
||
| it('should return NaN for invalid data', () => { | ||
| const trader1 = { total_pnl_pct: null } | ||
| const trader2 = { total_pnl_pct: 10.2 } | ||
|
|
||
| const hasValidData = | ||
| trader1.total_pnl_pct != null && | ||
| trader2.total_pnl_pct != null && | ||
| !isNaN(trader1.total_pnl_pct!) && | ||
| !isNaN(trader2.total_pnl_pct) | ||
|
|
||
| const gap = hasValidData | ||
| ? trader1.total_pnl_pct! - trader2.total_pnl_pct | ||
| : NaN | ||
|
|
||
| expect(isNaN(gap)).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle negative gap correctly', () => { | ||
| const trader1 = { total_pnl_pct: 5.0 } | ||
| const trader2 = { total_pnl_pct: 12.0 } | ||
|
|
||
| const hasValidData = | ||
| trader1.total_pnl_pct != null && | ||
| trader2.total_pnl_pct != null && | ||
| !isNaN(trader1.total_pnl_pct) && | ||
| !isNaN(trader2.total_pnl_pct) | ||
|
|
||
| const gap = hasValidData | ||
| ? trader1.total_pnl_pct - trader2.total_pnl_pct | ||
| : NaN | ||
|
|
||
| expect(gap).toBe(-7.0) | ||
| }) | ||
| }) | ||
|
|
||
| /** | ||
| * 測試顯示邏輯 | ||
| * 修復後應顯示「—」而非「NaN%」或「0.00%」 | ||
| */ | ||
| describe('display formatting', () => { | ||
| it('should format valid positive percentage correctly', () => { | ||
| const total_pnl_pct = 15.567 | ||
|
|
||
| const display = | ||
| total_pnl_pct != null && !isNaN(total_pnl_pct) | ||
| ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` | ||
| : '—' | ||
|
|
||
| expect(display).toBe('+15.57%') | ||
| }) | ||
|
|
||
| it('should format valid negative percentage correctly', () => { | ||
| const total_pnl_pct = -8.234 | ||
|
|
||
| const display = | ||
| total_pnl_pct != null && !isNaN(total_pnl_pct) | ||
| ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` | ||
| : '—' | ||
|
|
||
| expect(display).toBe('-8.23%') | ||
| }) | ||
|
|
||
| it('should display "—" for null value', () => { | ||
| const total_pnl_pct = null | ||
|
|
||
| const display = | ||
| total_pnl_pct != null && !isNaN(total_pnl_pct) | ||
| ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` | ||
| : '—' | ||
|
|
||
| expect(display).toBe('—') | ||
| }) | ||
|
|
||
| it('should display "—" for undefined value', () => { | ||
| const total_pnl_pct = undefined | ||
|
|
||
| const display = | ||
| total_pnl_pct != null && !isNaN(total_pnl_pct) | ||
| ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` | ||
| : '—' | ||
|
|
||
| expect(display).toBe('—') | ||
| }) | ||
|
|
||
| it('should display "—" for NaN value', () => { | ||
| const total_pnl_pct = NaN | ||
|
|
||
| const display = | ||
| total_pnl_pct != null && !isNaN(total_pnl_pct) | ||
| ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` | ||
| : '—' | ||
|
|
||
| expect(display).toBe('—') | ||
| }) | ||
|
|
||
| it('should format zero correctly', () => { | ||
| const total_pnl_pct = 0 | ||
|
|
||
| const display = | ||
| total_pnl_pct != null && !isNaN(total_pnl_pct) | ||
| ? `${total_pnl_pct >= 0 ? '+' : ''}${total_pnl_pct.toFixed(2)}%` | ||
| : '—' | ||
|
|
||
| expect(display).toBe('+0.00%') | ||
| }) | ||
| }) | ||
|
|
||
| /** | ||
| * 測試領先/落後訊息顯示邏輯 | ||
| * 只有在數據有效時才顯示 "領先" 或 "落後" 訊息 | ||
| */ | ||
| describe('leading/trailing message display', () => { | ||
| it('should show leading message when winning with positive gap', () => { | ||
| const isWinning = true | ||
| const gap = 5.2 | ||
| const hasValidData = true | ||
|
|
||
| const shouldShowLeading = hasValidData && isWinning && gap > 0 | ||
|
|
||
| expect(shouldShowLeading).toBe(true) | ||
| }) | ||
|
|
||
| it('should not show leading message when data is invalid', () => { | ||
| const isWinning = true | ||
| const gap = NaN | ||
| const hasValidData = false | ||
|
|
||
| const shouldShowLeading = hasValidData && isWinning && gap > 0 | ||
|
|
||
| expect(shouldShowLeading).toBe(false) | ||
| }) | ||
|
|
||
| it('should show trailing message when losing with negative gap', () => { | ||
| const isWinning = false | ||
| const gap = -3.5 | ||
| const hasValidData = true | ||
|
|
||
| const shouldShowTrailing = hasValidData && !isWinning && gap < 0 | ||
|
|
||
| expect(shouldShowTrailing).toBe(true) | ||
| }) | ||
|
|
||
| it('should not show trailing message when data is invalid', () => { | ||
| const isWinning = false | ||
| const gap = NaN | ||
| const hasValidData = false | ||
|
|
||
| const shouldShowTrailing = hasValidData && !isWinning && gap < 0 | ||
|
|
||
| expect(shouldShowTrailing).toBe(false) | ||
| }) | ||
|
|
||
| it('should show fallback "—" when data is invalid', () => { | ||
| const hasValidData = false | ||
|
|
||
| const shouldShowFallback = !hasValidData | ||
|
|
||
| expect(shouldShowFallback).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| /** | ||
| * 測試邊界情況 | ||
| */ | ||
| describe('edge cases', () => { | ||
| it('should handle very small positive numbers', () => { | ||
| const total_pnl_pct = 0.001 | ||
|
|
||
| const hasValidData = total_pnl_pct != null && !isNaN(total_pnl_pct) | ||
|
|
||
| expect(hasValidData).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle very large numbers', () => { | ||
| const total_pnl_pct = 9999.99 | ||
|
|
||
| const hasValidData = total_pnl_pct != null && !isNaN(total_pnl_pct) | ||
|
|
||
| expect(hasValidData).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle Infinity as invalid (produces NaN in calculations)', () => { | ||
| const total_pnl_pct = Infinity | ||
|
|
||
| // Infinity 本身不是 NaN,但在減法運算中可能導致問題 | ||
| const hasValidData = total_pnl_pct != null && isFinite(total_pnl_pct) | ||
|
|
||
| expect(hasValidData).toBe(false) | ||
| }) | ||
|
|
||
| it('should handle -Infinity as invalid', () => { | ||
| const total_pnl_pct = -Infinity | ||
|
|
||
| const hasValidData = total_pnl_pct != null && isFinite(total_pnl_pct) | ||
|
|
||
| expect(hasValidData).toBe(false) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的模式可以抽取可复用函数的,辛苦看下怎么处理比较好?
boolean && component 方式