Skip to content

Commit 1ae890b

Browse files
updated the test goeltracker
1 parent 20e430d commit 1ae890b

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

components/dashboard/GoalTracker.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ describe('GoalTracker Component', () => {
8787

8888
beforeEach(() => {
8989
window.localStorage.clear();
90+
91+
// Stub fetch so the background server-sync resolves immediately with
92+
// "no goals saved yet" (source: 'default'). This prevents the skeleton
93+
// loading state from blocking assertions in tests that don't pre-populate
94+
// localStorage — the component will immediately set serverSynced = true
95+
// and render actual values instead of placeholders.
96+
vi.stubGlobal(
97+
'fetch',
98+
vi.fn().mockResolvedValue({
99+
ok: true,
100+
json: async () => ({ goals: { monthly: 100, yearly: 1000 }, source: 'default' }),
101+
})
102+
);
90103
});
91104

92105
it('renders title and defaults correctly when localStorage is empty', () => {

components/dashboard/GoalTracker.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,20 @@ export default function GoalTracker({ username, activity = [] }: GoalTrackerProp
3333
DEFAULT_GOALS
3434
);
3535

36-
// Track whether the server has returned a value yet (to show a loading hint
37-
// only when localStorage had no prior value for this user).
36+
// Detect synchronously at mount whether localStorage had a real stored value
37+
// for this user. If it did, there's no "loading" state — we show values right
38+
// away. The skeleton only appears for brand-new visitors with no local data.
39+
const hadLocalStorageValue = useRef<() => boolean>(() => {
40+
try {
41+
const raw = localStorage.getItem(`commitpulse:goals:${username.toLowerCase()}`);
42+
return raw !== null;
43+
} catch {
44+
return false;
45+
}
46+
});
47+
48+
// serverSynced tracks whether the background fetch has returned yet.
49+
// Only relevant when there was no localStorage hit (hadLocalStorageValue = false).
3850
const [serverSynced, setServerSynced] = useState(false);
3951
const saveControllerRef = useRef<AbortController | null>(null);
4052

0 commit comments

Comments
 (0)