Skip to content

Commit 2e7236e

Browse files
feat(small): Extract ResetSection component from ConnectView.tsx (#8955)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: arii <342438+arii@users.noreply.github.com>
1 parent c8e300c commit 2e7236e

1 file changed

Lines changed: 57 additions & 20 deletions

File tree

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/**
22
* @jest-environment jsdom
33
*/
4-
import { render, screen, fireEvent } from '@testing-library/react'
4+
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
55
import ConnectView from './ConnectView'
66
import { WorkoutStatus } from '../../../types/workout'
7+
import { MeasurementSystem, Gender } from '../../../types/core'
78

89
describe('ConnectView', () => {
910
const defaultProps = {
@@ -13,27 +14,31 @@ describe('ConnectView', () => {
1314
setUserName: jest.fn(),
1415
userAge: '30',
1516
setUserAge: jest.fn(),
16-
userHeight: '5.9',
17+
onAgeBlur: jest.fn(),
18+
ageError: null,
19+
userHeight: { cm: '180', feet: '5', inches: '11' },
1720
setUserHeight: jest.fn(),
21+
onHeightBlur: jest.fn(),
22+
heightError: null,
1823
userWeight: '154',
1924
setUserWeight: jest.fn(),
20-
unit: 'imperial' as 'metric' | 'imperial',
21-
setUnit: jest.fn(),
22-
ageError: null,
23-
heightError: null,
25+
onWeightBlur: jest.fn(),
2426
weightError: null,
25-
validateAge: jest.fn(),
26-
validateHeight: jest.fn(),
27-
validateWeight: jest.fn(),
27+
gender: 'MALE' as Gender,
28+
setGender: jest.fn(),
29+
unitSystem: 'IMPERIAL' as MeasurementSystem,
30+
onUnitChange: jest.fn(),
2831
isConnected: false,
2932
deviceStatus: 'Disconnected',
3033
batteryLevel: null,
3134
onConnect: jest.fn(),
3235
onDisconnect: jest.fn(),
3336
onForgetDevice: jest.fn().mockResolvedValue(undefined),
3437
isSupported: true,
38+
signalPeriodMs: 1000,
3539
currentHR: 0,
36-
hrZoneProps: { percentage: 0, progressColor: 'grey' },
40+
hrZoneProps: { percentage: 0 },
41+
zone: { min: 0, max: 0, name: 'Rest', color: 'grey' },
3742
connectionStatus: 'Disconnected',
3843
bluetoothConnected: false,
3944
hasStarted: false,
@@ -44,33 +49,44 @@ describe('ConnectView', () => {
4449
onEndWorkout: jest.fn(),
4550
}
4651

47-
it('displays an error message for invalid age', () => {
52+
it('displays an error message for invalid age only when ageError is set', () => {
53+
// Negative assertion: no error initially
54+
const { rerender } = render(<ConnectView {...defaultProps} />)
55+
expect(screen.queryByText('Invalid age')).not.toBeInTheDocument()
56+
57+
// Positive assertion: error appears when prop is set
4858
const props = { ...defaultProps, ageError: 'Invalid age' }
49-
render(<ConnectView {...props} />)
59+
rerender(<ConnectView {...props} />)
5060
expect(screen.getByText('Invalid age')).toBeInTheDocument()
5161
})
5262

53-
it('displays an error message for invalid height', () => {
63+
it('displays an error message for invalid height only when heightError is set', () => {
64+
const { rerender } = render(<ConnectView {...defaultProps} />)
65+
expect(screen.queryByText('Invalid height')).not.toBeInTheDocument()
66+
5467
const props = { ...defaultProps, heightError: 'Invalid height' }
55-
render(<ConnectView {...props} />)
68+
rerender(<ConnectView {...props} />)
5669
expect(screen.getByText('Invalid height')).toBeInTheDocument()
5770
})
5871

59-
it('displays an error message for invalid weight', () => {
72+
it('displays an error message for invalid weight only when weightError is set', () => {
73+
const { rerender } = render(<ConnectView {...defaultProps} />)
74+
expect(screen.queryByText('Invalid weight')).not.toBeInTheDocument()
75+
6076
const props = { ...defaultProps, weightError: 'Invalid weight' }
61-
render(<ConnectView {...props} />)
77+
rerender(<ConnectView {...props} />)
6278
expect(screen.getByText('Invalid weight')).toBeInTheDocument()
6379
})
6480

65-
it('calls setUnit when the unit toggle is clicked', () => {
81+
it('calls onUnitChange when the unit toggle is clicked', () => {
6682
render(<ConnectView {...defaultProps} />)
67-
const metricButton = screen.getByText('Metric (kg, cm)')
83+
const metricButton = screen.getByLabelText('metric')
6884
fireEvent.click(metricButton)
69-
expect(defaultProps.setUnit).toHaveBeenCalledWith('metric')
85+
expect(defaultProps.onUnitChange).toHaveBeenCalledWith('METRIC')
7086
})
7187

7288
it('renders metric inputs when unit is metric', () => {
73-
const props = { ...defaultProps, unit: 'metric' as 'metric' | 'imperial' }
89+
const props = { ...defaultProps, unitSystem: 'METRIC' as MeasurementSystem }
7490
render(<ConnectView {...props} />)
7591
expect(screen.getByLabelText('Your Height (cm)')).toBeInTheDocument()
7692
expect(screen.getByLabelText('Your Weight (kg)')).toBeInTheDocument()
@@ -82,4 +98,25 @@ describe('ConnectView', () => {
8298
expect(screen.getByLabelText('Inches')).toBeInTheDocument()
8399
expect(screen.getByLabelText('Your Weight (lbs)')).toBeInTheDocument()
84100
})
101+
102+
it('renders the reset section with correct text', () => {
103+
render(<ConnectView {...defaultProps} />)
104+
expect(screen.getByText('Reset Permissions & Settings')).toBeInTheDocument()
105+
expect(
106+
screen.getByText(
107+
'Resets stored permissions and device settings, including Bluetooth connection.'
108+
)
109+
).toBeInTheDocument()
110+
})
111+
112+
it('calls onReset when reset button is clicked', async () => {
113+
render(<ConnectView {...defaultProps} />)
114+
const resetButton = screen.getByText('Reset Permissions & Settings')
115+
fireEvent.click(resetButton)
116+
117+
await waitFor(() => {
118+
expect(defaultProps.onForgetDevice).toHaveBeenCalled()
119+
expect(defaultProps.onReset).toHaveBeenCalled()
120+
})
121+
})
85122
})

0 commit comments

Comments
 (0)