Skip to content

Commit ded5f59

Browse files
committed
Volunteer-team-preference
1 parent 882906e commit ded5f59

File tree

4 files changed

+176
-5
lines changed

4 files changed

+176
-5
lines changed

src/Components/forms/helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const initialState = {
1010
interestedInCYF: '',
1111
industry: '',
1212
hearAboutCYF: '',
13+
teamId: '',
1314
hearAboutCYFFromEmployer: false,
1415
employer: '',
1516
guidePeople: ListsData.guidePeopleSkillList,
@@ -33,6 +34,7 @@ export const initialState = {
3334
email: false,
3435
city: false,
3536
cityId: false,
37+
teamId: false,
3638
interestedInVolunteer: false,
3739
interestedInCYF: false,
3840
agreeToTOU: false,

src/Components/forms/index.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,27 @@ import './index.css'
1111

1212
const path = `${domain()}${appPath}`
1313
class Forms extends Component {
14-
state = initialState
14+
state = {
15+
teamOptions: [],
16+
...initialState
17+
}
18+
19+
componentDidMount() {
20+
this.fetchTeamData()
21+
this.props.loadCities()
22+
}
23+
24+
fetchTeamData = async () => {
25+
try {
26+
const response = await axios.get(`${domain()}/teams`)
27+
const teamData = response.data.teams
28+
teamData.sort((a, b) => a.name.localeCompare(b.name))
29+
30+
this.setState({ teamOptions: teamData })
31+
} catch (error) {
32+
console.error('Error fetching team data:', error)
33+
}
34+
}
1535

1636
handleMagicLinkRequest = async e => {
1737
e.preventDefault()
@@ -88,17 +108,24 @@ class Forms extends Component {
88108
const { name, value, type, checked } = e.target
89109
const { errors } = this.state
90110
errors[name] = false
91-
if (e && e.target) {
111+
112+
// Handle teamId separately
113+
if (name === 'teamId') {
114+
this.setState({ teamId: value })
115+
} else {
116+
// Handle other form fields
92117
this.setState({
93118
[name]: type === 'checkbox' ? checked : value,
94119
submitted: false,
95120
errors,
96121
formInComplete: false,
97122
err: null
98123
})
99-
}
100-
if (name === 'hearAboutCYF') {
101-
this.setState({ hearAboutCYFFromEmployer: value === 'Employer' })
124+
125+
// Check for special case where name is 'hearAboutCYF'
126+
if (name === 'hearAboutCYF') {
127+
this.setState({ hearAboutCYFFromEmployer: value === 'Employer' })
128+
}
102129
}
103130
}
104131

@@ -150,6 +177,7 @@ class Forms extends Component {
150177
email,
151178
tel,
152179
cityId,
180+
teamId,
153181
interestedInVolunteer,
154182
interestedInCYF,
155183
industry,
@@ -169,6 +197,7 @@ class Forms extends Component {
169197
lastName,
170198
email,
171199
cityId,
200+
teamId,
172201
employer: hearAboutCYFFromEmployer ? employer : true,
173202
interestedInVolunteer,
174203
tel,
@@ -190,6 +219,7 @@ class Forms extends Component {
190219
email,
191220
tel,
192221
cityId,
222+
teamId,
193223
interestedInVolunteer,
194224
interestedInCYF,
195225
industry,
@@ -361,6 +391,7 @@ class Forms extends Component {
361391
onChange={this.onChange}
362392
telOnChange={this.telOnChange}
363393
onChangeCheckList={this.onChangeCheckList}
394+
teamOptions={this.state.teamOptions}
364395
{...this.props}
365396
{...this.state}
366397
/>

src/Components/forms/inputs/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export default class VolunteerForm extends Component {
1717
tel,
1818
cities,
1919
cityId,
20+
teamId,
21+
teamOptions,
2022
email,
2123
telOnChange,
2224
errors,
@@ -104,6 +106,28 @@ export default class VolunteerForm extends Component {
104106
placeholder="Just one or two sentences as required..."
105107
type="textarea"
106108
/>
109+
<DropDown
110+
data-testid="testId"
111+
onChange={onChange}
112+
value={teamId}
113+
name="teamId"
114+
arrayList={teamOptions}
115+
isEmpty={errors.teamId}
116+
label={
117+
<span>
118+
Please select the team you want to volunteer for. You can read
119+
about the teams on this
120+
<a
121+
href="https://codeyourfuture.io/volunteers/"
122+
target="_blank"
123+
rel="noopener noreferrer"
124+
>
125+
{' '}
126+
page
127+
</a>
128+
</span>
129+
}
130+
/>
107131
<DropDown
108132
onChange={onChange}
109133
value={industry}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import React from 'react'
2+
import { render, screen, fireEvent } from '@testing-library/react'
3+
import DropDown from './dropDown'
4+
5+
const teamOptions = [
6+
{ _id: 'team0', name: 'Select here' },
7+
{ _id: 'team1', name: 'Education' },
8+
{ _id: 'team2', name: 'Mentorship' },
9+
{ _id: 'team3', name: 'Outreach' },
10+
{ _id: 'team4', name: 'Personal Development' },
11+
{ _id: 'team5', name: 'Tech Project' }
12+
].map(team => ({ ...team, name: team.name.toLowerCase() }))
13+
14+
const errors = {
15+
teamId: false
16+
}
17+
18+
const VolunteerForm = ({ teamOptions }) => {
19+
return (
20+
<form data-testid="testId">
21+
<DropDown
22+
onChange={() => {}}
23+
value=""
24+
name="teamId"
25+
arrayList={teamOptions}
26+
isEmpty={errors.teamId}
27+
label={
28+
<span>
29+
Please select the team you want to volunteer for. You can read about
30+
the teams on this
31+
<a
32+
href="https://codeyourfuture.io/volunteers/"
33+
target="_blank"
34+
rel="noopener noreferrer"
35+
>
36+
{' '}
37+
page
38+
</a>
39+
</span>
40+
}
41+
/>
42+
</form>
43+
)
44+
}
45+
46+
describe('Volunteer-team-preference', () => {
47+
// Testing form content
48+
it('testing form', () => {
49+
render(<VolunteerForm teamOptions={teamOptions} />)
50+
expect(screen.getByTestId('testId')).toHaveTextContent(
51+
'Please select the team you want to volunteer for.'
52+
)
53+
})
54+
55+
// Test to see if we can open a new tab
56+
it('Opening CYF page in a new tab/page', () => {
57+
render(<VolunteerForm teamOptions={teamOptions} />)
58+
const linkElement = screen.getByText('page')
59+
expect(linkElement).toHaveAttribute(
60+
'href',
61+
'https://codeyourfuture.io/volunteers/'
62+
)
63+
expect(linkElement).toHaveAttribute('target', '_blank')
64+
})
65+
66+
it('team options are correctly displayed in the dropdown', () => {
67+
render(<VolunteerForm teamOptions={teamOptions} />)
68+
teamOptions.forEach(option => {
69+
const selectOptions = screen.queryAllByText(option.name)
70+
selectOptions.forEach(selectOption => {
71+
expect(selectOption).toBeInTheDocument()
72+
})
73+
})
74+
})
75+
76+
it('new volunteer cannot skip this question', () => {
77+
render(<VolunteerForm teamOptions={teamOptions} />)
78+
const selectElement = screen.getByLabelText(
79+
/Please select the team you want to volunteer for/i
80+
)
81+
fireEvent.change(selectElement, { target: { value: '' } })
82+
fireEvent.blur(selectElement)
83+
expect(screen.getByTestId('testId')).toHaveTextContent(
84+
'Please select the team you want to volunteer for.'
85+
)
86+
})
87+
88+
// Check if the teams are in alphabetical order
89+
it('form updates with new team', () => {
90+
const newTeam = { _id: 'team6', name: 'New Team' }
91+
const updatedTeamOptions = [...teamOptions, newTeam]
92+
render(<VolunteerForm teamOptions={updatedTeamOptions} />)
93+
const selectElement = screen.getByLabelText(
94+
/Please select the team you want to volunteer for/i
95+
)
96+
expect(selectElement).toContainHTML(
97+
'<option value="team6">New Team</option>'
98+
)
99+
})
100+
101+
it('form updates with deleted team', () => {
102+
const deletedTeam = { _id: 'team1', name: 'Education' }
103+
const updatedTeamOptions = teamOptions.filter(
104+
team => team._id !== deletedTeam._id
105+
)
106+
render(<VolunteerForm teamOptions={updatedTeamOptions} />)
107+
const selectElement = screen.getByLabelText(
108+
/Please select the team you want to volunteer for/i
109+
)
110+
expect(selectElement).not.toContainHTML(
111+
'<option value="team1">Education</option>'
112+
)
113+
})
114+
})

0 commit comments

Comments
 (0)