Skip to content

Commit

Permalink
c[finishes #169703465] ache previously selected movies
Browse files Browse the repository at this point in the history
  • Loading branch information
Easybuoy committed Nov 12, 2019
1 parent 34a3173 commit 40c6080
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/actions/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('Actions', () => {
type: LOADING
}
];
store.dispatch(getCharacter([]));
store.dispatch(getCharacter({ characters: [''] }));
expect(store.getActions()).toEqual(expectedActions);
expect(store.getActions()).toMatchSnapshot();
done();
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('Actions', () => {
}
];
const store = mockStore({});
return store.dispatch(getCharacter([''])).then(() => {
return store.dispatch(getCharacter({ characters: [''] })).then(() => {
expect(store.getActions()).toEqual(expectedActions);
done();
});
Expand Down
2 changes: 1 addition & 1 deletion src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const getMovie = movie_url => dispatch => {
};

export const getCharacter = movie => dispatch => {
const { characters } = movie;
const { characters } = movie;

dispatch({ type: LOADING });
return Promise.all(
Expand Down
20 changes: 12 additions & 8 deletions src/components/Common/MovieListDropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { connect } from 'react-redux';
import PropTypes from 'prop-types';

import PreLoader from '../Common/PreLoader';
import { getMovies, selectMovie, getMovie, setCharacters, setMovie } from '../../actions';
import {
MovieListDropdown as StyledMovieListDropdown
} from '../../styles';
getMovies,
selectMovie,
getMovie,
setCharacters,
setMovie
} from '../../actions';
import { MovieListDropdown as StyledMovieListDropdown } from '../../styles';
import Select from './Select';
import { getMovieFromLocalStorage} from '../../utils'
import { getMovieFromLocalStorage } from '../../utils';

export const MovieListDropdown = ({
movies,
Expand All @@ -33,11 +37,11 @@ export const MovieListDropdown = ({
selectMovie(title);

// check if movie exist in localstorage
const existingMovieInLocalStorage = getMovieFromLocalStorage(title)
const existingMovieInLocalStorage = getMovieFromLocalStorage(title);
if (existingMovieInLocalStorage.length > 0) {
// we found the movie in localstorage
setCharacters(existingMovieInLocalStorage[0].characters)
setMovie(existingMovieInLocalStorage[0].movie)
setCharacters(existingMovieInLocalStorage[0].characters);
setMovie(existingMovieInLocalStorage[0].movie);
} else {
//we could not find movie in localstorage, thus get from api
getMovie(url);
Expand Down Expand Up @@ -83,7 +87,7 @@ const mapStateToProps = state => ({
error: state.error.error,
movies: state.swapi.movies,
movie: state.swapi.movie,
selectedMovie: state.swapi.selectedMovie,
selectedMovie: state.swapi.selectedMovie
});

export default connect(
Expand Down
4 changes: 3 additions & 1 deletion src/components/Common/MovieListDropdown.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ describe('<MovieListDropdown />', () => {
movies: getMoviesMock,
getMovies: jest.fn(),
selectMovie: jest.fn(),
getMovie: jest.fn()
getMovie: jest.fn(),
setCharacters: jest.fn(),
setMovie: jest.fn()
};

it('renders the MovieListDropdown component correctly', () => {
Expand Down
8 changes: 7 additions & 1 deletion src/utils/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
formatHeight,
sortHeight,
sortName,
sortGender
sortGender,
initializeLocalStorage
} from './index';

describe('Util', () => {
Expand Down Expand Up @@ -164,4 +165,9 @@ describe('Util', () => {
const response = sortGender(testArray, 'M');
expect(response).toEqual([{ name: 'Ezekiel', height: 10, gender: 'male' }]);
});

it('test initializeLocalStorage', () => {
localStorage.removeItem('movieData')
expect(initializeLocalStorage()).toEqual(undefined);
});
});

0 comments on commit 40c6080

Please sign in to comment.