Skip to content

Commit 6006dc2

Browse files
committed
refactor some of the shared hooks
1 parent de966ca commit 6006dc2

File tree

9 files changed

+67
-11
lines changed

9 files changed

+67
-11
lines changed

src/frontend/components/Standings/hooks/useDriverPositions.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {
33
useTelemetryValue,
44
useTelemetry,
55
useSessionDrivers,
6-
useSessionType,
76
useDriverCarIdx,
87
useSessionQualifyingResults,
8+
useCurrentSessionType,
99
} from '@irdashies/context';
1010
import { Standings } from '../createStandings';
1111

@@ -73,8 +73,7 @@ export const useDriverStandings = () => {
7373
const radioTransmitCarIdx = useTelemetryValue('RadioTransmitCarIdx');
7474
const carStates = useCarState();
7575
const playerCarIdx = useDriverCarIdx();
76-
const sessionNum = useTelemetryValue('SessionNum');
77-
const sessionType = useSessionType(sessionNum);
76+
const sessionType = useCurrentSessionType();
7877
const qualifyingPositions = useSessionQualifyingResults();
7978

8079
const driverStandings: Standings[] = useMemo(() => {

src/frontend/context/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ export * from './SessionStore/SessionProvider';
44
export * from './SessionStore/SessionStore';
55
export * from './TelemetryStore/TelemetryProvider';
66
export * from './TelemetryStore/TelemetryStore';
7-
export * from './CarSpeedStore/useCarIdxSpeed';
8-
export * from './LapTimesStore/useCarIdxAverageLapTime';
7+
export * from './shared';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './useCarIdxSpeed';
2+
export * from './useCarIdxAverageLapTime';
3+
export * from './useCurrentSessionType';

src/frontend/context/LapTimesStore/useCarIdxAverageLapTime.spec.tsx renamed to src/frontend/context/shared/useCarIdxAverageLapTime.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, vi } from 'vitest';
22
import { renderHook } from '@testing-library/react';
33
import { useCarIdxAverageLapTime } from './useCarIdxAverageLapTime';
44
import { useTelemetryStore } from '../TelemetryStore/TelemetryStore';
5-
import { useLapTimesStore, useLapTimes } from './LapTimesStore';
5+
import { useLapTimesStore, useLapTimes } from '../LapTimesStore/LapTimesStore';
66
import { useCarIdxClassEstLapTime } from '../SessionStore/SessionStore';
77
import type { Telemetry } from '@irdashies/types';
88

@@ -11,7 +11,7 @@ vi.mock('../TelemetryStore/TelemetryStore', () => ({
1111
useTelemetryStore: vi.fn(),
1212
}));
1313

14-
vi.mock('./LapTimesStore', () => ({
14+
vi.mock('../LapTimesStore/LapTimesStore', () => ({
1515
useLapTimesStore: vi.fn(),
1616
useLapTimes: vi.fn(),
1717
}));

src/frontend/context/LapTimesStore/useCarIdxAverageLapTime.ts renamed to src/frontend/context/shared/useCarIdxAverageLapTime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect } from 'react';
22
import { useTelemetryStore } from '../TelemetryStore/TelemetryStore';
3-
import { useLapTimes, useLapTimesStore } from './LapTimesStore';
3+
import { useLapTimes, useLapTimesStore } from '../LapTimesStore/LapTimesStore';
44
import { useCarIdxClassEstLapTime } from '../SessionStore/SessionStore';
55

66
/**

src/frontend/context/CarSpeedStore/useCarIdxSpeed.spec.tsx renamed to src/frontend/context/shared/useCarIdxSpeed.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, vi } from 'vitest';
22
import { renderHook } from '@testing-library/react';
33
import { useCarIdxSpeed } from './useCarIdxSpeed';
44
import { useTelemetryStore } from '../TelemetryStore/TelemetryStore';
5-
import { useCarSpeedsStore, useCarSpeeds } from './CarSpeedsStore';
5+
import { useCarSpeedsStore, useCarSpeeds } from '../CarSpeedStore/CarSpeedsStore';
66
import { useSessionStore } from '../SessionStore/SessionStore';
77
import type { Telemetry } from '@irdashies/types';
88

@@ -11,7 +11,7 @@ vi.mock('../TelemetryStore/TelemetryStore', () => ({
1111
useTelemetryStore: vi.fn(),
1212
}));
1313

14-
vi.mock('./CarSpeedsStore', () => ({
14+
vi.mock('../CarSpeedStore/CarSpeedsStore', () => ({
1515
useCarSpeedsStore: vi.fn(),
1616
useCarSpeeds: vi.fn(),
1717
}));

src/frontend/context/CarSpeedStore/useCarIdxSpeed.ts renamed to src/frontend/context/shared/useCarIdxSpeed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect } from 'react';
22
import { useTelemetryStore } from '../TelemetryStore/TelemetryStore';
33
import { useSessionStore } from '../SessionStore/SessionStore';
4-
import { useCarSpeeds, useCarSpeedsStore } from './CarSpeedsStore';
4+
import { useCarSpeeds, useCarSpeedsStore } from '../CarSpeedStore/CarSpeedsStore';
55

66
/**
77
* First time this hook is called, it will update the car speeds using both telemetry and session (track length) state.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { renderHook } from '@testing-library/react';
3+
import { useCurrentSessionType } from './useCurrentSessionType';
4+
import { useSessionType, useTelemetryValue } from '@irdashies/context';
5+
6+
// Mock the context hooks
7+
vi.mock('@irdashies/context', () => ({
8+
useSessionType: vi.fn(),
9+
useTelemetryValue: vi.fn(),
10+
}));
11+
12+
describe('useCurrentSession', () => {
13+
beforeEach(() => {
14+
vi.resetAllMocks();
15+
});
16+
17+
it('should return the session type when session number is available', () => {
18+
// Mock the telemetry value for session number
19+
vi.mocked(useTelemetryValue).mockReturnValue(1);
20+
// Mock the session type
21+
vi.mocked(useSessionType).mockReturnValue('Race');
22+
23+
const { result } = renderHook(() => useCurrentSessionType());
24+
25+
expect(result.current).toBe('Race');
26+
expect(useTelemetryValue).toHaveBeenCalledWith('SessionNum');
27+
expect(useSessionType).toHaveBeenCalledWith(1);
28+
});
29+
30+
it('should return undefined when session number is not available', () => {
31+
// Mock the telemetry value as undefined
32+
vi.mocked(useTelemetryValue).mockReturnValue(undefined);
33+
// Mock the session type
34+
vi.mocked(useSessionType).mockReturnValue(undefined);
35+
36+
const { result } = renderHook(() => useCurrentSessionType());
37+
38+
expect(result.current).toBeUndefined();
39+
expect(useTelemetryValue).toHaveBeenCalledWith('SessionNum');
40+
expect(useSessionType).toHaveBeenCalledWith(undefined);
41+
});
42+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useSessionType, useTelemetryValue } from '@irdashies/context';
2+
3+
type SessionType = 'Race' | 'Lone Qualify' | 'Open Qualify' | 'Practice' | 'Offline Testing';
4+
5+
/**
6+
* @returns The current session type. Undefined if sessionNum is unknown.
7+
*/
8+
export const useCurrentSessionType = (): SessionType | undefined => {
9+
const sessionNum = useTelemetryValue('SessionNum');
10+
const sessionType = useSessionType(sessionNum);
11+
12+
return sessionType as SessionType | undefined;
13+
};

0 commit comments

Comments
 (0)