From 0e8acebfcb63c0767fbb3cd5b9774a1ba59b4364 Mon Sep 17 00:00:00 2001 From: Albin Henriksson <albhe428@student.liu.se> Date: Tue, 13 Apr 2021 14:48:36 +0000 Subject: [PATCH] Resolve "Improve testing" --- client/src/__mocks__/axios.js | 1 + client/src/actions/cities.test.ts | 19 ++ client/src/actions/communication.ts | 75 ------- client/src/actions/competitions.test.ts | 73 +++++++ client/src/actions/presentation.test.ts | 59 ++++++ client/src/actions/roles.test.ts | 19 ++ client/src/actions/searchUser.test.ts | 74 +++++++ client/src/actions/types.ts | 8 +- client/src/actions/user.test.ts | 81 ++++++++ client/src/actions/user.ts | 5 +- .../pages/admin/regions/AddRegion.test.tsx | 16 ++ .../src/pages/admin/regions/Regions.test.tsx | 37 ++++ client/src/pages/admin/users/AddUser.test.tsx | 16 ++ .../src/pages/admin/users/EditUser.test.tsx | 16 ++ .../pages/admin/users/UserManager.test.tsx | 43 ++++ client/src/pages/admin/users/UserManager.tsx | 1 - .../components/CompetitionSettings.test.tsx | 6 +- .../components/ImageComponentDisplay.test.tsx | 7 + .../components/TextComponentDisplay.test.tsx | 12 ++ client/src/reducers/citiesReducer.test.ts | 53 +++++ .../src/reducers/presentationReducer.test.ts | 193 ++++++++++++++++++ client/src/reducers/uiReducer.test.ts | 64 ++++++ client/src/reducers/userReducer.test.ts | 45 ++++ client/src/utils/checkAuthentication.test.ts | 59 ++++++ client/src/utils/checkAuthentication.ts | 3 +- 25 files changed, 894 insertions(+), 91 deletions(-) create mode 100644 client/src/actions/cities.test.ts delete mode 100644 client/src/actions/communication.ts create mode 100644 client/src/actions/competitions.test.ts create mode 100644 client/src/actions/presentation.test.ts create mode 100644 client/src/actions/roles.test.ts create mode 100644 client/src/actions/searchUser.test.ts create mode 100644 client/src/actions/user.test.ts create mode 100644 client/src/pages/admin/regions/AddRegion.test.tsx create mode 100644 client/src/pages/admin/regions/Regions.test.tsx create mode 100644 client/src/pages/admin/users/AddUser.test.tsx create mode 100644 client/src/pages/admin/users/EditUser.test.tsx create mode 100644 client/src/pages/admin/users/UserManager.test.tsx create mode 100644 client/src/pages/presentationEditor/components/ImageComponentDisplay.test.tsx create mode 100644 client/src/pages/presentationEditor/components/TextComponentDisplay.test.tsx create mode 100644 client/src/reducers/citiesReducer.test.ts create mode 100644 client/src/reducers/presentationReducer.test.ts create mode 100644 client/src/reducers/uiReducer.test.ts create mode 100644 client/src/reducers/userReducer.test.ts create mode 100644 client/src/utils/checkAuthentication.test.ts diff --git a/client/src/__mocks__/axios.js b/client/src/__mocks__/axios.js index c2953961..40f0914f 100644 --- a/client/src/__mocks__/axios.js +++ b/client/src/__mocks__/axios.js @@ -1,4 +1,5 @@ export default { get: jest.fn().mockImplementation(), post: jest.fn().mockImplementation(), + defaults: { headers: { common: { Authorization: '' } } }, } diff --git a/client/src/actions/cities.test.ts b/client/src/actions/cities.test.ts new file mode 100644 index 00000000..d5298d29 --- /dev/null +++ b/client/src/actions/cities.test.ts @@ -0,0 +1,19 @@ +import mockedAxios from 'axios' +import expect from 'expect' // You can use any testing library +import configureMockStore from 'redux-mock-store' +import thunk from 'redux-thunk' +import { getCities } from './cities' + +const middlewares = [thunk] +const mockStore = configureMockStore(middlewares) + +it('dispatches no actions when failing to get cities', async () => { + console.log = jest.fn() + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.reject(new Error('getting cities failed')) + }) + const store = mockStore({ competitions: { filterParams: [] } }) + await getCities()(store.dispatch) + expect(store.getActions()).toEqual([]) + expect(console.log).toHaveBeenCalled() +}) diff --git a/client/src/actions/communication.ts b/client/src/actions/communication.ts deleted file mode 100644 index d0719cd3..00000000 --- a/client/src/actions/communication.ts +++ /dev/null @@ -1,75 +0,0 @@ -import Types from './types.js' - -export function axiosPost( - path: any, - data: any, - config = undefined, - startCB = undefined, - successCB = undefined, - errorCB = undefined -) { - return { - type: Types.AXIOS_POST, - path, - data, - config, - startCB, - successCB, - errorCB, - } -} - -export function axiosPostSuccess(path: any, data: any, previousAction: any) { - return { - type: Types.AXIOS_POST_SUCCESS, - path, - data, - previousAction, - } -} - -export function axiosPostError(path: any, data: any, previousAction: any) { - return { - type: Types.AXIOS_POST_ERROR, - path, - data, - previousAction, - } -} - -export function axiosGet( - path: any, - data: any, - config = undefined, - startCB = undefined, - successCB = undefined, - errorCB = undefined -) { - return { - type: Types.AXIOS_GET, - path, - data, - config, - startCB, - successCB, - errorCB, - } -} - -export function axiosGetSuccess(path: any, data: any, previousAction: any) { - return { - type: Types.AXIOS_GET_SUCCESS, - path, - data, - previousAction, - } -} - -export function axiosGetError(path: any, data: any, previousAction: any) { - return { - type: Types.AXIOS_GET_ERROR, - path, - data, - previousAction, - } -} diff --git a/client/src/actions/competitions.test.ts b/client/src/actions/competitions.test.ts new file mode 100644 index 00000000..52be36ac --- /dev/null +++ b/client/src/actions/competitions.test.ts @@ -0,0 +1,73 @@ +import mockedAxios from 'axios' +import expect from 'expect' // You can use any testing library +import configureMockStore from 'redux-mock-store' +import thunk from 'redux-thunk' +import { CompetitionFilterParams } from './../interfaces/FilterParams' +import { getCompetitions, setFilterParams } from './competitions' +import Types from './types' + +const middlewares = [thunk] +const mockStore = configureMockStore(middlewares) + +it('dispatches correct actions when getting competitions', async () => { + const compRes: any = { + data: { + items: [ + { + id: 21, + name: 'ggff', + year: 2021, + style_id: 1, + city: { name: 'city_name', id: 5 }, + }, + { + id: 22, + name: 'sssss', + year: 2021, + style_id: 1, + city: { name: 'city_name', id: 5 }, + }, + ], + count: 2, + total_count: 3, + }, + } + + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.resolve(compRes) + }) + const expectedActions = [ + { type: Types.SET_COMPETITIONS, payload: compRes.data.items }, + { type: Types.SET_COMPETITIONS_TOTAL, payload: compRes.data.total_count }, + { type: Types.SET_COMPETITIONS_COUNT, payload: compRes.data.count }, + ] + const store = mockStore({ competitions: { filterParams: [] } }) + await getCompetitions()(store.dispatch, store.getState as any) + expect(store.getActions()).toEqual(expectedActions) +}) + +it('dispatches correct actions when setting filterParams', () => { + const testFilterParams: CompetitionFilterParams = { + page: 0, + pageSize: 3, + name: 'name', + cityId: 0, + styleId: 0, + year: 2000, + } + const expectedActions = [{ type: Types.SET_COMPETITIONS_FILTER_PARAMS, payload: testFilterParams }] + const store = mockStore({}) + setFilterParams(testFilterParams)(store.dispatch) + expect(store.getActions()).toEqual(expectedActions) +}) + +it('dispatches no actions when failing to get competitions', async () => { + console.log = jest.fn() + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.reject(new Error('getting competitions failed')) + }) + const store = mockStore({ competitions: { filterParams: [] } }) + await getCompetitions()(store.dispatch, store.getState as any) + expect(store.getActions()).toEqual([]) + expect(console.log).toHaveBeenCalled() +}) diff --git a/client/src/actions/presentation.test.ts b/client/src/actions/presentation.test.ts new file mode 100644 index 00000000..53ea4847 --- /dev/null +++ b/client/src/actions/presentation.test.ts @@ -0,0 +1,59 @@ +import mockedAxios from 'axios' +import expect from 'expect' // You can use any testing library +import configureMockStore from 'redux-mock-store' +import thunk from 'redux-thunk' +import { Slide } from '../interfaces/Slide' +import { + getPresentationCompetition, + getPresentationTeams, + setCurrentSlide, + setCurrentSlideNext, + setCurrentSlidePrevious, +} from './presentation' +import Types from './types' + +const middlewares = [thunk] +const mockStore = configureMockStore(middlewares) + +it('dispatches no actions when failing to get competitions', async () => { + console.log = jest.fn() + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.reject(new Error('getting competitions failed')) + }) + const store = mockStore({ competitions: { filterParams: [] } }) + await getPresentationCompetition('0')(store.dispatch) + expect(store.getActions()).toEqual([]) + expect(console.log).toHaveBeenCalled() +}) + +it('dispatches no actions when failing to get teams', async () => { + console.log = jest.fn() + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.reject(new Error('getting teams failed')) + }) + const store = mockStore({ competitions: { filterParams: [] } }) + await getPresentationTeams('0')(store.dispatch) + expect(store.getActions()).toEqual([]) + expect(console.log).toHaveBeenCalled() +}) +it('dispatches correct actions when setting slide', () => { + const testSlide: Slide = { competition_id: 0, id: 5, order: 5, timer: 20, title: '' } + const expectedActions = [{ type: Types.SET_PRESENTATION_SLIDE, payload: testSlide }] + const store = mockStore({}) + setCurrentSlide(testSlide)(store.dispatch) + expect(store.getActions()).toEqual(expectedActions) +}) + +it('dispatches correct actions when setting previous slide', () => { + const expectedActions = [{ type: Types.SET_PRESENTATION_SLIDE_PREVIOUS }] + const store = mockStore({}) + setCurrentSlidePrevious()(store.dispatch) + expect(store.getActions()).toEqual(expectedActions) +}) + +it('dispatches correct actions when setting next slide', () => { + const expectedActions = [{ type: Types.SET_PRESENTATION_SLIDE_NEXT }] + const store = mockStore({}) + setCurrentSlideNext()(store.dispatch) + expect(store.getActions()).toEqual(expectedActions) +}) diff --git a/client/src/actions/roles.test.ts b/client/src/actions/roles.test.ts new file mode 100644 index 00000000..94ca142f --- /dev/null +++ b/client/src/actions/roles.test.ts @@ -0,0 +1,19 @@ +import mockedAxios from 'axios' +import expect from 'expect' // You can use any testing library +import configureMockStore from 'redux-mock-store' +import thunk from 'redux-thunk' +import { getRoles } from './roles' + +const middlewares = [thunk] +const mockStore = configureMockStore(middlewares) + +it('dispatches no actions when failing to get roles', async () => { + console.log = jest.fn() + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.reject(new Error('getting roles failed')) + }) + const store = mockStore({ competitions: { filterParams: [] } }) + await getRoles()(store.dispatch) + expect(store.getActions()).toEqual([]) + expect(console.log).toHaveBeenCalled(); +}) diff --git a/client/src/actions/searchUser.test.ts b/client/src/actions/searchUser.test.ts new file mode 100644 index 00000000..66c15507 --- /dev/null +++ b/client/src/actions/searchUser.test.ts @@ -0,0 +1,74 @@ +import mockedAxios from 'axios' +import expect from 'expect' // You can use any testing library +import configureMockStore from 'redux-mock-store' +import thunk from 'redux-thunk' +import { UserFilterParams } from './../interfaces/FilterParams' +import { getSearchUsers, setFilterParams } from './searchUser' +import Types from './types' + +const middlewares = [thunk] +const mockStore = configureMockStore(middlewares) +it('dispatches correct actions when getting users', async () => { + const userRes: any = { + data: { + items: [ + { + id: 21, + name: 'ggff', + email: 'email@test.com', + year: 2021, + role_id: 1, + city_id: 0, + }, + { + id: 22, + name: 'sssss', + email: 'email@test.com', + year: 2021, + role_id: 1, + city_id: 0, + }, + ], + count: 2, + total_count: 3, + }, + } + + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.resolve(userRes) + }) + const expectedActions = [ + { type: Types.SET_SEARCH_USERS, payload: userRes.data.items }, + { type: Types.SET_SEARCH_USERS_TOTAL_COUNT, payload: userRes.data.total_count }, + { type: Types.SET_SEARCH_USERS_COUNT, payload: userRes.data.count }, + ] + const store = mockStore({ searchUsers: { filterParams: [] } }) + await getSearchUsers()(store.dispatch, store.getState as any) + expect(store.getActions()).toEqual(expectedActions) +}) + +it('dispatches correct actions when setting filterParams', () => { + const testFilterParams: UserFilterParams = { + page: 0, + pageSize: 3, + name: 'name', + cityId: 0, + email: 'email@test.com', + roleId: 0, + } + const expectedActions = [{ type: Types.SET_SEARCH_USERS_FILTER_PARAMS, payload: testFilterParams }] + const store = mockStore({}) + setFilterParams(testFilterParams)(store.dispatch) + expect(store.getActions()).toEqual(expectedActions) +}) + +it('dispatches no actions when failing to get users', async () => { + console.log = jest.fn() + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.reject(new Error('getting users failed')) + }) + const store = mockStore({ searchUsers: { filterParams: [] } }) + await getSearchUsers()(store.dispatch, store.getState as any) + expect(store.getActions()).toEqual([]) + expect(console.log).toHaveBeenCalled() +}) diff --git a/client/src/actions/types.ts b/client/src/actions/types.ts index c0b7b629..a8736c4b 100644 --- a/client/src/actions/types.ts +++ b/client/src/actions/types.ts @@ -8,7 +8,7 @@ export default { SET_SEARCH_USERS_COUNT: 'SET_SEARCH_USERS_COUNT', SET_SEARCH_USERS_TOTAL_COUNT: 'SET_SEARCH_USERS_TOTAL_COUNT', SET_ERRORS: 'SET_ERRORS', - CLEAR_ERRORS: 'SET_ERRORS', + CLEAR_ERRORS: 'CLEAR_ERRORS', SET_UNAUTHENTICATED: 'SET_UNAUTHENTICATED', SET_AUTHENTICATED: 'SET_AUTHENTICATED', SET_COMPETITIONS: 'SET_COMPETITIONS', @@ -23,10 +23,4 @@ export default { SET_CITIES: 'SET_CITIES', SET_CITIES_TOTAL: 'SET_CITIES_TOTAL', SET_CITIES_COUNT: 'SET_CITIES_COUNT', - AXIOS_GET: 'AXIOS_GET', - AXIOS_GET_SUCCESS: 'AXIOS_GET_SUCCESS', - AXIOS_GET_ERROR: 'AXIOS_GET_ERROR', - AXIOS_POST: 'AXIOS_POST', - AXIOS_POST_SUCCESS: 'AXIOS_POST_SUCCESS', - AXIOS_POST_ERROR: 'AXIOS_POST_ERROR', } diff --git a/client/src/actions/user.test.ts b/client/src/actions/user.test.ts new file mode 100644 index 00000000..156b1718 --- /dev/null +++ b/client/src/actions/user.test.ts @@ -0,0 +1,81 @@ +import mockedAxios from 'axios' +import expect from 'expect' // You can use any testing library +import { createMemoryHistory } from 'history' +import configureMockStore from 'redux-mock-store' +import thunk from 'redux-thunk' +import Types from './types' +import { loginUser, logoutUser } from './user' + +const middlewares = [thunk] +const mockStore = configureMockStore(middlewares) + +it('dispatches correct actions when logging in user', async () => { + const loginRes: any = { + data: { + access_token: 'TEST_ACCESS_TOKEN', + }, + } + const userDataRes: any = { + data: { + name: 'test_name', + }, + } + ;(mockedAxios.post as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.resolve(loginRes) + }) + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.resolve(userDataRes) + }) + const expectedActions = [ + { type: Types.LOADING_UI }, + { type: Types.LOADING_USER }, + { type: Types.CLEAR_ERRORS }, + { type: Types.SET_USER, payload: { name: 'test_name' } }, + ] + const store = mockStore({}) + const history = createMemoryHistory() + await loginUser({ email: 'test@email.com', password: 'testpassword' }, history)(store.dispatch) + expect(store.getActions()).toEqual(expectedActions) +}) + +it('dispatches correct action when logging out user', async () => { + const store = mockStore({}) + await logoutUser()(store.dispatch) + expect(store.getActions()).toEqual([{ type: Types.SET_UNAUTHENTICATED }]) +}) + +it('dispatches correct action when failing to log in user', async () => { + console.log = jest.fn() + const errorMessage = 'getting teams failed' + ;(mockedAxios.post as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.reject({ response: { data: errorMessage } }) + }) + const store = mockStore({ competitions: { filterParams: [] } }) + const history = createMemoryHistory() + const expectedActions = [{ type: Types.LOADING_UI }, { type: Types.SET_ERRORS, payload: errorMessage }] + await loginUser({ email: 'test@email.com', password: 'testpassword' }, history)(store.dispatch) + expect(store.getActions()).toEqual(expectedActions) + expect(console.log).toHaveBeenCalled() +}) + +it('dispatches correct actions when failing to get user data', async () => { + console.log = jest.fn() + const loginRes: any = { + data: { + access_token: 'TEST_ACCESS_TOKEN', + }, + } + ;(mockedAxios.post as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.resolve(loginRes) + }) + const errorMessage = 'getting teams failed' + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.reject({ response: { data: errorMessage } }) + }) + const store = mockStore({ competitions: { filterParams: [] } }) + const history = createMemoryHistory() + const expectedActions = [{ type: Types.LOADING_UI }, { type: Types.LOADING_USER }, { type: Types.CLEAR_ERRORS }] + await loginUser({ email: 'test@email.com', password: 'testpassword' }, history)(store.dispatch) + expect(store.getActions()).toEqual(expectedActions) + expect(console.log).toHaveBeenCalled() +}) diff --git a/client/src/actions/user.ts b/client/src/actions/user.ts index 72d764c8..30374cca 100644 --- a/client/src/actions/user.ts +++ b/client/src/actions/user.ts @@ -17,10 +17,10 @@ export const loginUser = (userData: AccountLoginModel, history: History) => asyn history.push('/admin') //redirecting to admin page after login success }) .catch((err) => { - console.error(err) + console.log(err) dispatch({ type: Types.SET_ERRORS, - payload: err.response.data, + payload: err && err.response && err.response.data, }) }) } @@ -30,7 +30,6 @@ export const getUserData = () => async (dispatch: AppDispatch) => { await axios .get('/users') .then((res) => { - console.log(res.data) dispatch({ type: Types.SET_USER, payload: res.data, diff --git a/client/src/pages/admin/regions/AddRegion.test.tsx b/client/src/pages/admin/regions/AddRegion.test.tsx new file mode 100644 index 00000000..06b2052a --- /dev/null +++ b/client/src/pages/admin/regions/AddRegion.test.tsx @@ -0,0 +1,16 @@ +import { render } from '@testing-library/react' +import React from 'react' +import { Provider } from 'react-redux' +import { BrowserRouter } from 'react-router-dom' +import store from '../../../store' +import AddRegion from './AddRegion' + +it('renders add region', () => { + render( + <BrowserRouter> + <Provider store={store}> + <AddRegion /> + </Provider> + </BrowserRouter> + ) +}) diff --git a/client/src/pages/admin/regions/Regions.test.tsx b/client/src/pages/admin/regions/Regions.test.tsx new file mode 100644 index 00000000..7046bff0 --- /dev/null +++ b/client/src/pages/admin/regions/Regions.test.tsx @@ -0,0 +1,37 @@ +import { render } from '@testing-library/react' +import mockedAxios from 'axios' +import React from 'react' +import { Provider } from 'react-redux' +import { BrowserRouter } from 'react-router-dom' +import store from '../../../store' +import RegionManager from './Regions' + +it('renders region manager', () => { + const cityRes: any = { + data: { + items: [ + { + id: 1, + name: 'Link\u00f6ping', + }, + { + id: 2, + name: 'Stockholm', + }, + ], + count: 2, + total_count: 3, + }, + } + + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.resolve(cityRes) + }) + render( + <BrowserRouter> + <Provider store={store}> + <RegionManager /> + </Provider> + </BrowserRouter> + ) +}) diff --git a/client/src/pages/admin/users/AddUser.test.tsx b/client/src/pages/admin/users/AddUser.test.tsx new file mode 100644 index 00000000..5c825570 --- /dev/null +++ b/client/src/pages/admin/users/AddUser.test.tsx @@ -0,0 +1,16 @@ +import { render } from '@testing-library/react' +import React from 'react' +import { Provider } from 'react-redux' +import { BrowserRouter } from 'react-router-dom' +import store from '../../../store' +import EditUser from './EditUser' + +it('renders edit user', () => { + render( + <BrowserRouter> + <Provider store={store}> + <EditUser user={{ id: 0, name: '', email: '', role_id: 0, city_id: 0 }} /> + </Provider> + </BrowserRouter> + ) +}) diff --git a/client/src/pages/admin/users/EditUser.test.tsx b/client/src/pages/admin/users/EditUser.test.tsx new file mode 100644 index 00000000..bf2b0090 --- /dev/null +++ b/client/src/pages/admin/users/EditUser.test.tsx @@ -0,0 +1,16 @@ +import { render } from '@testing-library/react' +import React from 'react' +import { Provider } from 'react-redux' +import { BrowserRouter } from 'react-router-dom' +import store from '../../../store' +import AddUser from './AddUser' + +it('renders edit user', () => { + render( + <BrowserRouter> + <Provider store={store}> + <AddUser /> + </Provider> + </BrowserRouter> + ) +}) diff --git a/client/src/pages/admin/users/UserManager.test.tsx b/client/src/pages/admin/users/UserManager.test.tsx new file mode 100644 index 00000000..f50cbed8 --- /dev/null +++ b/client/src/pages/admin/users/UserManager.test.tsx @@ -0,0 +1,43 @@ +import { render } from '@testing-library/react' +import mockedAxios from 'axios' +import React from 'react' +import { Provider } from 'react-redux' +import { BrowserRouter } from 'react-router-dom' +import store from '../../../store' +import UserManager from './UserManager' + +it('renders user manager', () => { + const userRes: any = { + data: { + items: [ + { + id: 1, + name: 'user1', + email: 'user1@email.com', + role_id: 0, + city_id: 0, + }, + { + id: 2, + name: 'Stockholm', + email: 'user2@email.com', + role_id: 0, + city_id: 0, + }, + ], + count: 2, + total_count: 3, + }, + } + + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.resolve(userRes) + }) + render( + <BrowserRouter> + <Provider store={store}> + <UserManager /> + </Provider> + </BrowserRouter> + ) +}) diff --git a/client/src/pages/admin/users/UserManager.tsx b/client/src/pages/admin/users/UserManager.tsx index 11a34128..df9cdec0 100644 --- a/client/src/pages/admin/users/UserManager.tsx +++ b/client/src/pages/admin/users/UserManager.tsx @@ -70,7 +70,6 @@ const UserManager: React.FC = (props: any) => { }, []) useEffect(() => { - console.log('asd') setEditAnchorEl(null) setAnchorEl(null) }, [users]) diff --git a/client/src/pages/presentationEditor/components/CompetitionSettings.test.tsx b/client/src/pages/presentationEditor/components/CompetitionSettings.test.tsx index e6f87e7e..592581d4 100644 --- a/client/src/pages/presentationEditor/components/CompetitionSettings.test.tsx +++ b/client/src/pages/presentationEditor/components/CompetitionSettings.test.tsx @@ -1,7 +1,7 @@ import { render } from '@testing-library/react' import React from 'react' -import CompetitionSettings from './CompetitionSettings' +import ImageComponentDisplay from './ImageComponentDisplay' -it('renders competition settings', () => { - render(<CompetitionSettings />) +it('renders image component display', () => { + render(<ImageComponentDisplay component={{ id: 0, x: 0, y: 0, w: 0, h: 0, type: 0, media_id: 0 }} />) }) diff --git a/client/src/pages/presentationEditor/components/ImageComponentDisplay.test.tsx b/client/src/pages/presentationEditor/components/ImageComponentDisplay.test.tsx new file mode 100644 index 00000000..b726023b --- /dev/null +++ b/client/src/pages/presentationEditor/components/ImageComponentDisplay.test.tsx @@ -0,0 +1,7 @@ +import { render } from '@testing-library/react' +import React from 'react' +import ImageComponentDisplay from './ImageComponentDisplay' + +it('renders competition settings', () => { + render(<ImageComponentDisplay component={{ id: 0, x: 0, y: 0, w: 0, h: 0, media_id: 0, type: 2 }} />) +}) diff --git a/client/src/pages/presentationEditor/components/TextComponentDisplay.test.tsx b/client/src/pages/presentationEditor/components/TextComponentDisplay.test.tsx new file mode 100644 index 00000000..8f61ee36 --- /dev/null +++ b/client/src/pages/presentationEditor/components/TextComponentDisplay.test.tsx @@ -0,0 +1,12 @@ +import { Editor } from '@tinymce/tinymce-react' +import { mount } from 'enzyme' +import React from 'react' +import TextComponentDisplay from './TextComponentDisplay' + +it('renders text component display', () => { + const testText = 'TEST' + const container = mount( + <TextComponentDisplay component={{ id: 0, x: 0, y: 0, w: 0, h: 0, text: testText, type: 2, font: '123123' }} /> + ) + expect(container.find(Editor).prop('initialValue')).toBe(testText) +}) diff --git a/client/src/reducers/citiesReducer.test.ts b/client/src/reducers/citiesReducer.test.ts new file mode 100644 index 00000000..32026895 --- /dev/null +++ b/client/src/reducers/citiesReducer.test.ts @@ -0,0 +1,53 @@ +import Types from '../actions/types' +import citiesReducer from './citiesReducer' + +const initialState = { + cities: [], + total: 0, + count: 0, +} + +it('should return the initial state', () => { + expect(citiesReducer(undefined, {} as any)).toEqual(initialState) +}) + +it('should handle SET_CITIES', () => { + const testCities = [{ name: 'testName', id: 0 }] + expect( + citiesReducer(initialState, { + type: Types.SET_CITIES, + payload: testCities, + }) + ).toEqual({ + cities: testCities, + total: 0, + count: 0, + }) +}) + +it('should handle SET_CITIES_TOTAL', () => { + const testTotal = 123123 + expect( + citiesReducer(initialState, { + type: Types.SET_CITIES_TOTAL, + payload: testTotal, + }) + ).toEqual({ + cities: [], + total: testTotal, + count: 0, + }) +}) +it('should handle SET_CITIES_COUNT', () => { + const testCount = 456456 + expect( + citiesReducer(initialState, { + type: Types.SET_CITIES_COUNT, + payload: testCount, + }) + ).toEqual({ + cities: [], + total: 0, + count: testCount, + }) +}) diff --git a/client/src/reducers/presentationReducer.test.ts b/client/src/reducers/presentationReducer.test.ts new file mode 100644 index 00000000..cf4ded41 --- /dev/null +++ b/client/src/reducers/presentationReducer.test.ts @@ -0,0 +1,193 @@ +import Types from '../actions/types' +import { RichSlide } from '../interfaces/ApiRichModels' +import { Slide } from '../interfaces/Slide' +import presentationReducer from './presentationReducer' + +const initialState = { + competition: { + name: '', + id: 0, + city: { + id: 0, + name: '', + }, + slides: [], + year: 0, + teams: [], + }, + slide: { + competition_id: 0, + id: 0, + order: 0, + timer: 0, + title: '', + }, + teams: [], +} + +it('should return the initial state', () => { + expect(presentationReducer(undefined, {} as any)).toEqual(initialState) +}) + +it('should handle SET_PRESENTATION_COMPETITION', () => { + const testCompetition = { + name: 'testCompName', + id: 4, + city: { + id: 3, + name: 'testCityName', + }, + slides: [{ id: 20 }], + year: 1999, + teams: [], + } + expect( + presentationReducer(initialState, { + type: Types.SET_PRESENTATION_COMPETITION, + payload: testCompetition, + }) + ).toEqual({ + competition: testCompetition, + slide: testCompetition.slides[0], + teams: [], + }) +}) + +it('should handle SET_PRESENTATION_TEAMS', () => { + const testTeams = [ + { + name: 'testTeamName1', + id: 3, + }, + { + name: 'testTeamName2', + id: 5, + }, + ] + expect( + presentationReducer(initialState, { + type: Types.SET_PRESENTATION_TEAMS, + payload: testTeams, + }) + ).toEqual({ + competition: initialState.competition, + slide: initialState.slide, + teams: testTeams, + }) +}) + +it('should handle SET_PRESENTATION_SLIDE', () => { + const testSlide = [ + { + competition_id: 20, + id: 4, + order: 3, + timer: 123, + title: 'testSlideTitle', + }, + ] + expect( + presentationReducer(initialState, { + type: Types.SET_PRESENTATION_SLIDE, + payload: testSlide, + }) + ).toEqual({ + competition: initialState.competition, + slide: testSlide, + teams: initialState.teams, + }) +}) + +describe('should handle SET_PRESENTATION_SLIDE_PREVIOUS', () => { + it('by changing slide to the previous if there is one', () => { + const testPresentationState = { + competition: { + ...initialState.competition, + slides: [ + { competition_id: 0, order: 0 }, + { competition_id: 0, order: 1 }, + ] as RichSlide[], + }, + teams: initialState.teams, + slide: { competition_id: 0, order: 1 } as Slide, + } + expect( + presentationReducer(testPresentationState, { + type: Types.SET_PRESENTATION_SLIDE_PREVIOUS, + }) + ).toEqual({ + competition: testPresentationState.competition, + slide: testPresentationState.competition.slides[0], + teams: testPresentationState.teams, + }) + }) + it('by not changing slide if there is no previous one', () => { + const testPresentationState = { + competition: { + ...initialState.competition, + slides: [ + { competition_id: 0, order: 0 }, + { competition_id: 0, order: 1 }, + ] as RichSlide[], + }, + teams: initialState.teams, + slide: { competition_id: 0, order: 0 } as Slide, + } + expect( + presentationReducer(testPresentationState, { + type: Types.SET_PRESENTATION_SLIDE_PREVIOUS, + }) + ).toEqual({ + competition: testPresentationState.competition, + slide: testPresentationState.competition.slides[0], + teams: testPresentationState.teams, + }) + }) +}) + +describe('should handle SET_PRESENTATION_SLIDE_NEXT', () => { + it('by changing slide to the next if there is one', () => { + const testPresentationState = { + competition: { + ...initialState.competition, + slides: [ + { competition_id: 0, order: 0 }, + { competition_id: 0, order: 1 }, + ] as RichSlide[], + }, + teams: initialState.teams, + slide: { competition_id: 0, order: 0 } as Slide, + } + expect( + presentationReducer(testPresentationState, { + type: Types.SET_PRESENTATION_SLIDE_NEXT, + }) + ).toEqual({ + competition: testPresentationState.competition, + slide: testPresentationState.competition.slides[1], + teams: testPresentationState.teams, + }) + }) + it('by not changing slide if there is no next one', () => { + const testPresentationState = { + competition: { + ...initialState.competition, + slides: [ + { competition_id: 0, order: 0 }, + { competition_id: 0, order: 1 }, + ] as RichSlide[], + }, + teams: initialState.teams, + slide: { competition_id: 0, order: 1 } as Slide, + } + expect( + presentationReducer(testPresentationState, { + type: Types.SET_PRESENTATION_SLIDE_NEXT, + }) + ).toEqual({ + competition: testPresentationState.competition, + slide: testPresentationState.competition.slides[1], + teams: testPresentationState.teams, + }) + }) +}) diff --git a/client/src/reducers/uiReducer.test.ts b/client/src/reducers/uiReducer.test.ts new file mode 100644 index 00000000..cea5c3ad --- /dev/null +++ b/client/src/reducers/uiReducer.test.ts @@ -0,0 +1,64 @@ +import Types from '../actions/types' +import userReducer from './userReducer' + +const initialState = { + authenticated: false, + loading: false, + userInfo: null, +} + +it('should return the initial state', () => { + expect(userReducer(undefined, {} as any)).toEqual(initialState) +}) + +it('should handle SET_AUTHENTICATED', () => { + expect( + userReducer(initialState, { + type: Types.SET_AUTHENTICATED, + }) + ).toEqual({ + authenticated: true, + loading: initialState.loading, + userInfo: initialState.userInfo, + }) +}) + +it('should handle SET_UNAUTHENTICATED', () => { + expect( + userReducer(initialState, { + type: Types.SET_UNAUTHENTICATED, + }) + ).toEqual(initialState) +}) + +it('should handle SET_USER', () => { + const testUserInfo = { + name: 'testName', + email: 'test@email.com', + role: { id: 0, name: 'roleName' }, + city: { id: 0, name: 'cityName' }, + id: 0, + } + expect( + userReducer(initialState, { + type: Types.SET_USER, + payload: testUserInfo, + }) + ).toEqual({ + authenticated: true, + loading: false, + userInfo: testUserInfo, + }) +}) + +it('should handle LOADING_USER', () => { + expect( + userReducer(initialState, { + type: Types.LOADING_USER, + }) + ).toEqual({ + loading: true, + authenticated: initialState.authenticated, + userInfo: initialState.userInfo, + }) +}) diff --git a/client/src/reducers/userReducer.test.ts b/client/src/reducers/userReducer.test.ts new file mode 100644 index 00000000..6ccf0268 --- /dev/null +++ b/client/src/reducers/userReducer.test.ts @@ -0,0 +1,45 @@ +import Types from '../actions/types' +import uiReducer from './uiReducer' + +const initialState = { + loading: false, + errors: null, +} + +it('should return the initial state', () => { + expect(uiReducer(undefined, {} as any)).toEqual(initialState) +}) + +it('should handle SET_ERRORS', () => { + const testError = { message: 'errorMessage' } + expect( + uiReducer(initialState, { + type: Types.SET_ERRORS, + payload: testError, + }) + ).toEqual({ + loading: false, + errors: testError, + }) +}) + +it('should handle CLEAR_ERRORS', () => { + expect( + uiReducer(initialState, { + type: Types.CLEAR_ERRORS, + }) + ).toEqual({ + loading: false, + errors: null, + }) +}) +it('should handle LOADING_UI', () => { + expect( + uiReducer(initialState, { + type: Types.LOADING_UI, + }) + ).toEqual({ + loading: true, + errors: initialState.errors, + }) +}) diff --git a/client/src/utils/checkAuthentication.test.ts b/client/src/utils/checkAuthentication.test.ts new file mode 100644 index 00000000..901f331d --- /dev/null +++ b/client/src/utils/checkAuthentication.test.ts @@ -0,0 +1,59 @@ +import mockedAxios from 'axios' +import Types from '../actions/types' +import store from '../store' +import { CheckAuthentication } from './checkAuthentication' + +it('dispatches correct actions when auth token is ok', async () => { + const userRes: any = { + data: { + name: 'username', + }, + } + + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.resolve(userRes) + }) + + const spy = jest.spyOn(store, 'dispatch') + const testToken = + 'Bearer eyJ0eXAiOiJeyJ0eXAiOiJKV1QeyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDQ5MzksImV4cCI6Mz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VjdCBBZG1pbmlzdHJhdG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxSc' + localStorage.setItem('token', testToken) + await CheckAuthentication() + expect(spy).toBeCalledWith({ type: Types.LOADING_USER }) + expect(spy).toBeCalledWith({ type: Types.SET_AUTHENTICATED }) + expect(spy).toBeCalledWith({ type: Types.SET_USER, payload: userRes.data }) + expect(spy).toBeCalledTimes(3) +}) + +it('dispatches correct actions when getting user data fails', async () => { + console.log = jest.fn() + ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => { + return Promise.reject(new Error('failed getting user data')) + }) + + const spy = jest.spyOn(store, 'dispatch') + const testToken = + 'Bearer eyJ0eXAiOiJeyJ0eXAiOiJKV1QeyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDQ5MzksImV4cCI6MzI1MTI1MjU3NTcsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJHaXZlbk5hbWUiOiJKb2hubnkiLCJTdXJuYW1lIjoiUm9ja2V0IiwiRW1haWwiOiJqcm9ja2V0QGV4YW1wbGUuY29tIiwiUm9sZSI6WyJNYW5hZ2VyIiwiUHJvamVjdCBBZG1pbmlzdHJhdG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxSceyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDQ5MzksImV4cCI6MzI1MTI1MjU3NTcsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJHaXZlbk5hbWUiOiJKb2hubnkiLCJTdXJuYW1lIjoiUm9ja2V0IiwiRW1haWwiOiJqcm9ja2V0QGV4YW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5jb20iLCJHaXZlbk5hbWUiOiJKb2hubnkiLCJTdXJuYW1lIjoiUm9ja2V0IiwiRW1haWwiOiJqcm9ja2V0QGV4YW1wbGUuY29tIiwiUm9sZSI6WyJNYW5hZ2VyIiwiUHJvamVjdCBBZG1pbmlzdHJhdG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxScKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDQ5MzksImV4cCI6MzI1MTI1MjU3NTcsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJHaXZlbk5hbWUiOiJKb2hubnkiLCJTdXJuYW1lIjoiUm9ja2V0IiwiRW1haWwiOiJqcm9ja2V0QGV4YW1wbGUuY29tIiwiUm9sZSI6WyJNYW5hZ2VyIiwiUHJvamVjdCBBZG1pbmlzdHJhdG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxSc' + localStorage.setItem('token', testToken) + await CheckAuthentication() + expect(spy).toBeCalledWith({ type: Types.LOADING_USER }) + expect(spy).toBeCalledWith({ type: Types.SET_UNAUTHENTICATED }) + expect(spy).toBeCalledTimes(2) + expect(console.log).toHaveBeenCalled() +}) + +it('dispatches no actions when no token exists', async () => { + const spy = jest.spyOn(store, 'dispatch') + await CheckAuthentication() + expect(spy).not.toBeCalled() +}) + +it('dispatches correct actions when token is expired', async () => { + const testToken = + 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDY1MTUsImV4cCI6MTU4Njc3MDUxNSwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJFbWFpbCI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJSb2xlIjpbIk1hbmFnZXIiLCJQcm9qZWN0IEFkbWluaXN0cmF0b3IiXX0.R5-oWGGumd-YWPoKyziJmVB8SdX6B9SsV6m7novIfgg' + localStorage.setItem('token', testToken) + const spy = jest.spyOn(store, 'dispatch') + await CheckAuthentication() + expect(spy).toBeCalledWith({ type: Types.SET_UNAUTHENTICATED }) + expect(spy).toBeCalledTimes(1) +}) diff --git a/client/src/utils/checkAuthentication.ts b/client/src/utils/checkAuthentication.ts index 83b73554..23178154 100644 --- a/client/src/utils/checkAuthentication.ts +++ b/client/src/utils/checkAuthentication.ts @@ -15,7 +15,6 @@ export const CheckAuthentication = async () => { if (decodedToken.exp * 1000 >= Date.now()) { axios.defaults.headers.common['Authorization'] = authToken store.dispatch({ type: Types.LOADING_USER }) - console.log('loading user') await axios .get('/users') .then((res) => { @@ -26,7 +25,7 @@ export const CheckAuthentication = async () => { }) }) .catch((error) => { - console.error(error) + console.log(error) UnAuthorized() }) } else { -- GitLab