From c9812aa7b0fd7a9544c67d2cf1fdd66c9abfad00 Mon Sep 17 00:00:00 2001 From: svetlagasheva Date: Tue, 5 May 2020 14:39:16 +0300 Subject: [PATCH] useSelectableSeasons hook and tests for it dropped; --- .../VideosList/useSelectableSeasons.js | 91 ------------------- tests/hooks.spec.js | 52 ----------- 2 files changed, 143 deletions(-) delete mode 100644 src/routes/MetaDetails/VideosList/useSelectableSeasons.js delete mode 100644 tests/hooks.spec.js diff --git a/src/routes/MetaDetails/VideosList/useSelectableSeasons.js b/src/routes/MetaDetails/VideosList/useSelectableSeasons.js deleted file mode 100644 index 980625ee1..000000000 --- a/src/routes/MetaDetails/VideosList/useSelectableSeasons.js +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (C) 2017-2020 Smart code 203358507 - -const React = require('react'); - -const reducer = (state, action) => { - switch (action.type) { - case 'videos-changed': { - const seasons = action.videos - .map(({ season }) => season) - .filter((season, index, seasons) => { - return season !== null && - !isNaN(season) && - typeof season === 'number' && - seasons.indexOf(season) === index; - }) - .sort((a, b) => a - b); - const selectedSeason = seasons.includes(state.selectedSeason) ? - state.selectedSeason - : - seasons.length > 0 ? - seasons[seasons.length - 1] - : - null; - return { - ...state, - videos: action.videos, - seasons, - selectedSeason - }; - } - case 'season-changed': { - const selectedSeason = state.seasons.includes(action.season) ? - action.season - : - state.season; - return { - ...state, - selectedSeason - }; - } - default: { - return state; - } - } -}; - -const initializer = (videos) => { - const initialState = { - videos: [], - seasons: [], - selectedSeason: null - }; - const initAction = { - type: 'videos-changed', - videos - }; - - return reducer(initialState, initAction); -}; - -const useSelectableSeasons = (videos) => { - const [state, dispatch] = React.useReducer( - reducer, - videos, - initializer - ); - const selectSeason = React.useCallback((season) => { - dispatch({ - type: 'season-changed', - season - }); - }, []); - const videosForSeason = React.useMemo(() => { - return state.videos - .filter((video) => { - return state.selectedSeason === null || video.season === state.selectedSeason; - }) - .sort((a, b) => { - return a.episode - b.episode; - }); - }, [state.videos, state.selectedSeason]); - React.useEffect(() => { - dispatch({ - type: 'videos-changed', - videos - }); - }, [videos]); - return [state.seasons, state.selectedSeason, videosForSeason, selectSeason]; -}; - -module.exports = useSelectableSeasons; diff --git a/tests/hooks.spec.js b/tests/hooks.spec.js deleted file mode 100644 index 7a68de98e..000000000 --- a/tests/hooks.spec.js +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (C) 2017-2020 Smart code 203358507 - -const { renderHook, act } = require('@testing-library/react-hooks'); -const useSelectableSeasons = require('../src/routes/MetaDetails/VideosList/useSelectableSeasons'); - -const videos = [{ 'season': 4 }, { 'season': 5 }, { 'season': 4 }, { 'season': 7 }]; - -describe('hooks tests', () => { - describe('useSelectableSeasons hook', () => { - it('match 7', async () => { - const { result } = renderHook(() => useSelectableSeasons(videos)); - const [, selectedSeason] = result.current; - expect(selectedSeason).toBe(7); - }); - - it('match 5', async () => { - const { result } = renderHook(() => useSelectableSeasons(videos)); - - act(() => { - const [, , , selectSeason] = result.current; - selectSeason(5); - }); - - const [, selectedSeason] = result.current; - expect(selectedSeason).toBe(5); - }); - - it('not match 6', async () => { - const { result } = renderHook(() => useSelectableSeasons(videos)); - - act(() => { - const [, , , selectSeason] = result.current; - selectSeason(6); - }); - - const [, selectedSeason] = result.current; - expect(selectedSeason).toBe(undefined); - }); - - it('not match $', async () => { - const { result } = renderHook(() => useSelectableSeasons(videos)); - - act(() => { - const [, , , selectSeason] = result.current; - selectSeason('$'); - }); - - const [, selectedSeason] = result.current; - expect(selectedSeason).toBe(undefined); - }); - }); -});