Skip to content
Snippets Groups Projects
Commit 3968d98d authored by Albin Henriksson's avatar Albin Henriksson
Browse files

Fix more bugs originating from changes to api

parent 575b2fc8
No related branches found
No related tags found
1 merge request!44Resolve "test competition view"
import { render } from '@testing-library/react' import { render } from '@testing-library/react'
import React from 'react' import React from 'react'
import { act } from 'react-dom/test-utils'
import { Provider } from 'react-redux' import { Provider } from 'react-redux'
import App from './App' import App from './App'
import store from './store' import store from './store'
test('renders app', () => { test('renders app', async () => {
render( await act(async () => {
<Provider store={store}> render(
<App /> <Provider store={store}>
</Provider> <App />
) </Provider>
)
})
}) })
export default { export default {
get: jest.fn().mockImplementation(), get: jest.fn().mockImplementation(),
post: jest.fn().mockImplementation(),
} }
...@@ -30,15 +30,10 @@ export const getUserData = () => async (dispatch: AppDispatch) => { ...@@ -30,15 +30,10 @@ export const getUserData = () => async (dispatch: AppDispatch) => {
await axios await axios
.get('/users') .get('/users')
.then((res) => { .then((res) => {
console.log(res.data)
dispatch({ dispatch({
type: Types.SET_USER, type: Types.SET_USER,
payload: { payload: res.data,
id: res.data.id,
name: res.data.name,
email: res.data.email,
roleId: res.data.role_id,
cityId: res.data.city_id,
},
}) })
}) })
.catch((err) => { .catch((err) => {
......
import { City } from './City'
export interface Competition { export interface Competition {
name: string name: string
city_id: number
style_id: number
year: number
id: number id: number
city: City
year: number
} }
import { City } from './City'
export interface Competition {
name: string
city: City
style_id: number
year: number
id: number
}
...@@ -37,8 +37,9 @@ const competitionSchema: Yup.SchemaOf<AddCompetitionFormModel> = Yup.object({ ...@@ -37,8 +37,9 @@ const competitionSchema: Yup.SchemaOf<AddCompetitionFormModel> = Yup.object({
const AddCompetition: React.FC = (props: any) => { const AddCompetition: React.FC = (props: any) => {
const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(null) const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(null)
const [selectedCity, setSelectedCity] = React.useState<City | undefined>()
const cities = useAppSelector((state) => state.cities.cities) const cities = useAppSelector((state) => state.cities.cities)
const userCity = useAppSelector((state) => state.user.userInfo?.city)
const [selectedCity, setSelectedCity] = React.useState<City | undefined>(userCity)
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget) setAnchorEl(event.currentTarget)
} }
...@@ -80,7 +81,7 @@ const AddCompetition: React.FC = (props: any) => { ...@@ -80,7 +81,7 @@ const AddCompetition: React.FC = (props: any) => {
} }
const competitionInitialValues: AddCompetitionFormModel = { const competitionInitialValues: AddCompetitionFormModel = {
model: { name: '', city: noCitySelected, year: currentYear }, model: { name: '', city: userCity?.name ? userCity.name : noCitySelected, year: currentYear },
} }
return ( return (
<div> <div>
......
...@@ -8,37 +8,41 @@ import CompetitionManager from './CompetitionManager' ...@@ -8,37 +8,41 @@ import CompetitionManager from './CompetitionManager'
it('renders competition manager', () => { it('renders competition manager', () => {
const cityRes: any = { const cityRes: any = {
data: [ data: {
{ items: [
id: 1, {
name: 'Link\u00f6ping', id: 1,
}, name: 'Link\u00f6ping',
{ },
id: 2, {
name: 'Stockholm', id: 2,
}, name: 'Stockholm',
], },
],
count: 2,
total_count: 3,
},
} }
const compRes: any = { const compRes: any = {
data: { data: {
competitions: [ items: [
{ {
id: 21, id: 21,
name: 'ggff', name: 'ggff',
year: 2021, year: 2021,
style_id: 1, style_id: 1,
city_id: 1, city: cityRes.data.items[0],
}, },
{ {
id: 22, id: 22,
name: 'sssss', name: 'sssss',
year: 2021, year: 2021,
style_id: 1, style_id: 1,
city_id: 1, city: cityRes.data.items[1],
}, },
], ],
count: 2, count: 2,
total: 3, total_count: 3,
}, },
} }
......
...@@ -152,7 +152,7 @@ const CompetitionManager: React.FC = (props: any) => { ...@@ -152,7 +152,7 @@ const CompetitionManager: React.FC = (props: any) => {
{row.name} {row.name}
</Button> </Button>
</TableCell> </TableCell>
<TableCell align="right">{cities.find((city) => city.id === row.city_id)?.name || ''}</TableCell> <TableCell align="right">{cities.find((city) => city.id === row.city.id)?.name || ''}</TableCell>
<TableCell align="right">{row.year}</TableCell> <TableCell align="right">{row.year}</TableCell>
<TableCell align="right"> <TableCell align="right">
<Button onClick={(event) => handleClick(event, row.id)}> <Button onClick={(event) => handleClick(event, row.id)}>
......
//in userReducer.ts
import { AnyAction } from 'redux' import { AnyAction } from 'redux'
import Types from '../actions/types' import Types from '../actions/types'
import { City } from '../interfaces/City'
import { Competition } from './../interfaces/Competition'
interface UserInfo { interface UserInfo {
name: string name: string
email: string email: string
roleId: number role: Competition
cityId: number city: City
id: number
} }
interface UserState { interface UserState {
......
...@@ -22,12 +22,7 @@ export const CheckAuthentication = async () => { ...@@ -22,12 +22,7 @@ export const CheckAuthentication = async () => {
store.dispatch({ type: Types.SET_AUTHENTICATED }) store.dispatch({ type: Types.SET_AUTHENTICATED })
store.dispatch({ store.dispatch({
type: Types.SET_USER, type: Types.SET_USER,
payload: { payload: res.data,
name: res.data.name,
email: res.data.email,
roleId: res.data.role_id,
cityId: res.data.city_id,
},
}) })
}) })
.catch((error) => { .catch((error) => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment