diff --git a/components/__tests__/reducers.test.ts b/components/__tests__/reducers.test.ts index 4c87609..c2b4a49 100644 --- a/components/__tests__/reducers.test.ts +++ b/components/__tests__/reducers.test.ts @@ -1,5 +1,5 @@ import Movie from "../../model/Movie" -import {describe, expect, test} from '@jest/globals' +import {describe, expect} from '@jest/globals' import appReducer from "../../redux/reducers/appReducer" import {ADD_FAVOURITE, ADD_WATCHLATER, FETCH_TRENDING_MOVIE, LOAD_FAVOURITE, LOAD_WATCHLATER, POP_FIRST_TRENDING} from "../../redux/constants" @@ -66,24 +66,107 @@ describe('test ADD_WATCHLATER', () => { }) }) }) -/* - case LOAD_WATCHLATER: - // @ts-ignore - return {...state, watchLaterMovies: action.payload}; - case LOAD_FAVOURITE: - // @ts-ignore - return {...state, favouriteMovies: action.payload}; - case FETCH_TRENDING_ID: - // @ts-ignore - return {...state, trendingIDs: action.payload}; - case FETCH_WATCHLATER: - // @ts-ignore - return {...state, watchLaterMovies: action.payload}; - case FETCH_FAVOURITE: - // @ts-ignore - return {...state, favouriteMovies: action.payload}; - case FETCH_TRENDING_MOVIE: - return {...state, trendingMovies: action.payload}; - case POP_FIRST_TRENDING: - return {...state, trendingMovies: [...state.trendingMovies.filter((item: Movie) => item !== action.payload)]}; -*/ \ No newline at end of file + +describe('test LOAD_WATCHLATER', () => { + + let initialState = { + trendingIDs: [], + trendingMovies: [], + watchLaterMovies: [], + favouriteMovies: [], + } + + it('should handle LOAD_WATCHLATER', () => { + const watchLater = new Movie(1, "Test", "", 1, 5, "2023", ["Halloween"], "", "") + const MovieList = [watchLater] + expect( + appReducer(initialState, { + type: LOAD_WATCHLATER, + payload: MovieList, + }) + ).toEqual({ + trendingIDs: [], + trendingMovies: [], + watchLaterMovies: [watchLater, ...initialState.watchLaterMovies], + favouriteMovies: [], + }) + }) +}) + +describe('test LOAD_FAVOURITE', () => { + + let initialState = { + trendingIDs: [], + trendingMovies: [], + watchLaterMovies: [], + favouriteMovies: [], + } + + it('should handle LOAD_FAVOURITE', () => { + const favourite = new Movie(1, "Test", "", 1, 5, "2023", ["Halloween"], "", "") + const MovieList = [favourite] + expect( + appReducer(initialState, { + type: LOAD_FAVOURITE, + payload: MovieList, + }) + ).toEqual({ + trendingIDs: [], + trendingMovies: [], + watchLaterMovies: [], + favouriteMovies: [favourite, ...initialState.favouriteMovies], + }) + }) +}) + +describe('test FETCH_TRENDING_MOVIE', () => { + + let initialState = { + trendingIDs: [], + trendingMovies: [], + watchLaterMovies: [], + favouriteMovies: [], + } + + it('should handle FETCH_TRENDING_MOVIE', () => { + const trending = new Movie(1, "Test", "", 1, 5, "2023", ["Halloween"], "", "") + const MovieList = [trending] + expect( + appReducer(initialState, { + type: FETCH_TRENDING_MOVIE, + payload: MovieList, + }) + ).toEqual({ + trendingIDs: [], + trendingMovies: [trending, ...initialState.trendingMovies], + watchLaterMovies: [], + favouriteMovies: [], + }) + }) +}) + +describe('test POP_FIRST_TRENDING', () => { + + const trending = new Movie(1, "Test", "", 1, 5, "2023", ["Halloween"], "", "") + + let initialState = { + trendingIDs: [], + trendingMovies: [trending], + watchLaterMovies: [], + favouriteMovies: [], + } + + it('should handle POP_FIRST_TRENDING', () => { + expect( + appReducer(initialState, { + type: POP_FIRST_TRENDING, + payload: trending, + }) + ).toEqual({ + trendingIDs: [], + trendingMovies: [...initialState.trendingMovies.filter((item: Movie) => item !== trending)], + watchLaterMovies: [], + favouriteMovies: [], + }) + }) +}) diff --git a/redux/reducers/appReducer.ts b/redux/reducers/appReducer.ts index fd953ca..d3ac46f 100644 --- a/redux/reducers/appReducer.ts +++ b/redux/reducers/appReducer.ts @@ -3,7 +3,7 @@ import Movie from "../../model/Movie"; const initialState = { trendingIDs: [], - trendingMovies: [], + trendingMovies: [] as Movie[], watchLaterMovies: [] as Movie[], favouriteMovies: [] as Movie[], }