useSelectableSeasons hook and tests for it dropped;

This commit is contained in:
svetlagasheva 2020-05-05 14:39:16 +03:00
parent 80d340801c
commit c9812aa7b0
2 changed files with 0 additions and 143 deletions

View file

@ -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;

View file

@ -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);
});
});
});