Skip to content

Instantly share code, notes, and snippets.

@OlivierJM
Created July 21, 2021 16:36
Show Gist options
  • Save OlivierJM/9ddf760a498ecde0acd8d7b9da1449cc to your computer and use it in GitHub Desktop.
Save OlivierJM/9ddf760a498ecde0acd8d7b9da1449cc to your computer and use it in GitHub Desktop.
import * as actions from "../actions/";
import * as types from "../utils/types";
import { moviesMock } from "../utils/moviemock";
describe("actions", () => {
it("should create an action to fetch popular movies", () => {
const fetchMovieAction = {
type: types.FETCH_POPULAR_MOVIES,
movies: moviesMock
};
expect(actions.fetchPopularMovies(moviesMock)).toEqual(fetchMovieAction);
});
it("should create an action to find details of a given movie", () => {
const movieDetailAction = {
type: types.FETCH_MOVIES_DETAILS,
details: moviesMock[0]
};
expect(actions.fetchMovieDetails(moviesMock[0])).toEqual(movieDetailAction);
});
it("should create an action to search for movies", () => {
const searchAction = {
type: types.SEARCH_MOVIES,
movies: moviesMock
};
expect(actions.searchMovie(moviesMock)).toEqual(searchAction);
});
it("should create an action to handle errors", () => {
const error = "something wrong happened";
const errorAction = {
type: types.ERROR_FETCHING,
error
};
expect(actions.errorFetching(error)).toEqual(errorAction);
});
it("should create an action for loading status", () => {
const fetchingAction = {
type: types.FETCH_LOADING
};
expect(actions.fetching()).toEqual(fetchingAction);
});
});
@OlivierJM
Copy link
Author

OlivierJM commented Jul 21, 2021

// reducers
import { movieReducer, initialState } from "../reducers/";
import * as types from "../utils/types";
import { moviesMock } from "../utils/moviemock";

const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
describe("reducers", () => {
  it("should return the initial state", () => {
    expect(movieReducer(undefined, {})).toEqual(initialState);
  });
  it("should search for movies", () => {
    const searchAction = {
      type: types.SEARCH_MOVIES,
      movies: moviesMock
    };
    wait(300);
    const results = movieReducer(initialState, searchAction);
    expect(results.movies).toEqual(moviesMock);
  });
  it("should fetch for popular movies", () => {
    const popularMovieAction = {
      type: types.FETCH_POPULAR_MOVIES,
      movies: moviesMock
    };
    wait(300);
    const response = movieReducer({}, popularMovieAction);
    expect(response.movies).toEqual(moviesMock);
  });
  it("should handle loading state of the fetch", () => {
    const fetching = {
      type: types.FETCH_LOADING,
      loading: true
    };
    wait(300);
    const loadingStatus = movieReducer({}, fetching);
    expect(loadingStatus.loading).toBe(true);
  });
  it("should return movie details", () => {
    const fetchMovieDetail = {
      type: types.FETCH_MOVIES_DETAILS,
      details: moviesMock[0]
    };
    wait(300);
    const movieDetail = movieReducer({}, fetchMovieDetail);
    expect(movieDetail.details).toEqual(moviesMock[0]);
  });
});

@OlivierJM
Copy link
Author

//moviesMock

import { Movie } from "./types";

type genre = {
  id: number;
  name: string;
};

const moviesMock: Movie[] = [
  {
    vote_count: 17,
    id: 280960,
    video: false,
    vote_average: 5.4,
    title: "Catarina and the others",
    popularity: 388.325,
    poster_path: "/kZMCbp0o46Tsg43omSHNHJKNTx9.jpg",
    original_language: "pt",
    original_title: "Catarina e os Outros",
    genre_ids: [18, 9648],
    backdrop_path: "/9nDiMhvL3FtaWMsvvvzQIuq276X.jpg",
    adult: false,
    overview:
      "Outside, the first sun rays break the dawn.  Sixteen years old Catarina can't fall asleep.  Inconsequently, in the big city adults are moved by desire...  Catarina found she is HIV positive. She wants to drag everyone else along.",
    release_date: "2011-03-01"
  },
  {
    vote_count: 334,
    id: 458156,
    video: false,
    vote_average: 7.5,
    title: "John Wick: Chapter 3 – Parabellum",
    popularity: 578.653,
    poster_path: "/ziEuG1essDuWuC5lpWUaw1uXY2O.jpg",
    original_language: "en",
    original_title: "John Wick: Chapter 3 – Parabellum",
    genre_ids: [80, 28, 53],
    backdrop_path: "/kcga7xuiQVqM9WHfWnAbidxU7hd.jpg",
    adult: false,
    overview:
      "Super-assassin John Wick returns with a $14 million price tag on his head and an army of bounty-hunting killers on his trail. After killing a member of the shadowy international assassin’s guild, the High Table, John Wick is excommunicado, but the world’s most ruthless hit men and women await his every turn.",
    release_date: "2019-05-15"
  }
];
const genresMock: genre[] = [
  {
    id: 18,
    name: "Drama"
  },
  {
    id: 80,
    name: "Crime"
  },
  {
    id: 35,
    name: "Comedy"
  }
];
export { moviesMock, genresMock };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment