Todorov_Stanimir

02. Christmas Movies JS Advanced Retake Exam - 10 December 2

Dec 10th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { expect } = require('chai');
  2. const ChristmasMovies = require('./02. Christmas Movies_Resources.js');
  3.  
  4. describe("class ChristmasMovies", () => {
  5.     let movies;
  6.     beforeEach(function () {
  7.         movies = new ChristmasMovies();
  8.     });
  9.  
  10.     //test 1
  11.     describe("check constructor of class", function () {
  12.         it("shoud corectly inisialize ane instance with three properties", function () {
  13.             expect(movies.movieCollection).to.deep.equal([]);
  14.             expect(movies.watched).to.deep.equal({});
  15.             expect(movies.actors).to.deep.equal([]);
  16.             expect(movies.movieCollection).to.be.a('Array');
  17.             expect(movies.watched).to.be.a('Object');
  18.             expect(movies.actors).to.be.a('Array');
  19.             expect(movies.movieCollection.length).to.equal(0);
  20.             expect(movies.actors.length).to.equal(0);
  21.             expect(movies).to.deep.equal({ "actors": [], "movieCollection": [], "watched": {} })
  22.         });
  23.     });
  24.     //test 3
  25.     describe("check function buyMovie(movieName, actors)", function () {
  26.         it("shoud throw an error if We have movie in our colection", function () {
  27.             movies.buyMovie('Home Alone', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern']);
  28.             let movie = () => movies.buyMovie('Home Alone', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern'])
  29.             expect(movie).to.throw(`You already own Home Alone in your collection!`);
  30.         });
  31.         it("shoud add new movie if it is not in the collection", function () {
  32.             let movie = movies.buyMovie('Home Alone', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern']);
  33.             expect(movies.movieCollection).to.deep.equals([{ "actors": ["Macaulay Culkin", "Joe Pesci", "Daniel Stern"], "name": "Home Alone" }]);
  34.             expect(movie).to.equal(`You just got Home Alone to your collection in which Macaulay Culkin, Joe Pesci, Daniel Stern are taking part!`);
  35.             expect(movies.movieCollection[0].actors.length).to.equal(3);
  36.         });
  37.     });
  38.     //test 4,5
  39.     describe("check function discardMovie(movieName)", function () {
  40.         it("shoud throw an error if We have movie in our colection", function () {
  41.             movies.buyMovie('Home Alone', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern']);
  42.             let movie = () => movies.discardMovie('Home Alone 2')
  43.             expect(movie).to.throw(`Home Alone 2 is not at your collection!`);
  44.         });
  45.         it("check function discardMovie(movieName)", function () {
  46.             movies.buyMovie('Home Alone', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern']);
  47.             let movie = () => movies.discardMovie('Home Alone')
  48.             expect(movie).to.throw(`Home Alone is not watched!`);
  49.         });
  50.         it("check function discardMovie(movieName)", function () {
  51.             movies.buyMovie('Home Alone', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern']);
  52.             movies.watchMovie('Home Alone')
  53.             let movie = movies.discardMovie('Home Alone')
  54.             expect(movie).to.equal(`You just threw away Home Alone!`);
  55.         });
  56.     });
  57.     //test 6,7
  58.     describe("check function watchMovie()", function () {
  59.         it("shoud throw an error if We have not movie in our colection", function () {
  60.             movies.buyMovie('Home Alone', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern']);
  61.             let movie = () => movies.watchMovie('Home Alone 2')
  62.             expect(movie).to.throw('No such movie in your collection!');
  63.         });
  64.         it("check function discardMovie(movieName)", function () {
  65.             movies.buyMovie('Home Alone', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern']);
  66.             movies.watchMovie('Home Alone')
  67.             // let movie = movies.discardMovie('Home Alone')
  68.             expect(movies.watched).to.deep.equal({ "Home Alone": 1 });
  69.         });
  70.     });
  71.  
  72.     //test 8, 9
  73.     describe("check function favouriteMovie()", function () {
  74.         it("shoud throw an error if We have not movie in our colection", function () {
  75.             let movie = () => movies.favouriteMovie()
  76.             expect(movie).to.throw('You have not watched a movie yet this year!');
  77.         });
  78.         it("check function discardMovie(movieName)", function () {
  79.             movies.buyMovie('Home Alone', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern']);
  80.             movies.buyMovie('Home Alone 2', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern']);
  81.             movies.watchMovie('Home Alone');
  82.             movies.watchMovie('Home Alone 2');
  83.             movies.watchMovie('Home Alone 2');
  84.             let movie = movies.favouriteMovie()
  85.             expect(movies.watched).to.deep.equal({ 'Home Alone': 1, 'Home Alone 2': 2 });
  86.             expect(movie).to.equal(`Your favourite movie is Home Alone 2 and you have watched it 2 times!`)
  87.         });
  88.     });
  89.  
  90.     //test 10, 11
  91.     describe("check function mostStarredActor()", function () {
  92.         it("shoud throw an error if We have not movie in our colection", function () {
  93.             let movie = () => movies.mostStarredActor()
  94.             expect(movie).to.throw('You have not watched a movie yet this year!');
  95.         });
  96.         it("check function mostStarredActor()", function () {
  97.             movies.buyMovie('Home Alone', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern']);
  98.             movies.buyMovie('Home Alone 2', ['Macaulay Culkin']);
  99.             movies.buyMovie('Last Christmas', ['Emilia Clarke', 'Henry Golding']);
  100.             movies.buyMovie('The Grinch', ['Benedict Cumberbatch', 'Pharrell Williams']);
  101.             movies.watchMovie('Home Alone');
  102.             movies.watchMovie('Home Alone');
  103.             movies.watchMovie('Home Alone');
  104.             movies.watchMovie('Home Alone 2');
  105.             movies.watchMovie('The Grinch');
  106.             movies.watchMovie('Last Christmas');
  107.             movies.watchMovie('Home Alone 2');
  108.             movies.watchMovie('Last Christmas');
  109.             movies.discardMovie('The Grinch');
  110.             movies.favouriteMovie();
  111.             let movie = movies.mostStarredActor()
  112.             expect(movie).to.equal('The most starred actor is Macaulay Culkin and starred in 2 movies!')
  113.         });
  114.     });
  115.  
  116. });
  117.  
  118.  
  119. class ChristmasMovies {
  120.     constructor() {
  121.         this.movieCollection = [];
  122.         this.watched = {};
  123.         this.actors = [];
  124.     }
  125.  
  126.     buyMovie(movieName, actors) {
  127.         let movie = this.movieCollection.find(m => movieName === m.name);
  128.         let uniqueActors = new Set(actors);
  129.  
  130.         if (movie === undefined) {
  131.             this.movieCollection.push({ name: movieName, actors: [...uniqueActors] });
  132.             let output = [];
  133.             [...uniqueActors].map(actor => output.push(actor));
  134.             return `You just got ${movieName} to your collection in which ${output.join(', ')} are taking part!`;
  135.         } else {
  136.             throw new Error(`You already own ${movieName} in your collection!`);
  137.         }
  138.     }
  139.  
  140.     discardMovie(movieName) {
  141.         let filtered = this.movieCollection.filter(x => x.name === movieName)
  142.  
  143.         if (filtered.length === 0) {
  144.             throw new Error(`${movieName} is not at your collection!`);
  145.         }
  146.         let index = this.movieCollection.findIndex(m => m.name === movieName);
  147.         this.movieCollection.splice(index, 1);
  148.         let { name, _ } = filtered[0];
  149.         if (this.watched.hasOwnProperty(name)) {
  150.             delete this.watched[name];
  151.             return `You just threw away ${name}!`;
  152.         } else {
  153.             throw new Error(`${movieName} is not watched!`);
  154.         }
  155.  
  156.     }
  157.  
  158.     watchMovie(movieName) {
  159.         let movie = this.movieCollection.find(m => movieName === m.name);
  160.         if (movie) {
  161.             if (!this.watched.hasOwnProperty(movie.name)) {
  162.                 this.watched[movie.name] = 1;
  163.             } else {
  164.                 this.watched[movie.name]++;
  165.             }
  166.         } else {
  167.             throw new Error('No such movie in your collection!');
  168.         }
  169.     }
  170.  
  171.     favouriteMovie() {
  172.         let favourite = Object.entries(this.watched).sort((a, b) => b[1] - a[1]);
  173.         if (favourite.length > 0) {
  174.             return `Your favourite movie is ${favourite[0][0]} and you have watched it ${favourite[0][1]} times!`;
  175.         } else {
  176.             throw new Error('You have not watched a movie yet this year!');
  177.         }
  178.     }
  179.  
  180.     mostStarredActor() {
  181.         let mostStarred = {};
  182.         if (this.movieCollection.length > 0) {
  183.             this.movieCollection.forEach(el => {
  184.                 let { _, actors } = el;
  185.                 actors.forEach(actor => {
  186.                     if (mostStarred.hasOwnProperty(actor)) {
  187.                         mostStarred[actor]++;
  188.                     } else {
  189.                         mostStarred[actor] = 1;
  190.                     }
  191.                 })
  192.             });
  193.             let theActor = Object.entries(mostStarred).sort((a, b) => b[1] - a[1]);
  194.             return `The most starred actor is ${theActor[0][0]} and starred in ${theActor[0][1]} movies!`;
  195.         } else {
  196.             throw new Error('You have not watched a movie yet this year!')
  197.         }
  198.     }
  199. }
  200.  
  201. let christmas = new ChristmasMovies();
  202. christmas.buyMovie('Home Alone', ['Macaulay Culkin', 'Joe Pesci', 'Daniel Stern']);
  203. christmas.buyMovie('Home Alone 2', ['Macaulay Culkin']);
  204. christmas.buyMovie('Last Christmas', ['Emilia Clarke', 'Henry Golding']);
  205. christmas.buyMovie('The Grinch', ['Benedict Cumberbatch', 'Pharrell Williams']);
  206. christmas.watchMovie('Home Alone');
  207. christmas.watchMovie('Home Alone');
  208. christmas.watchMovie('Home Alone');
  209. christmas.watchMovie('Home Alone 2');
  210. christmas.watchMovie('The Grinch');
  211. christmas.watchMovie('Last Christmas');
  212. christmas.watchMovie('Home Alone 2');
  213. christmas.watchMovie('Last Christmas');
  214. christmas.discardMovie('The Grinch');
  215. christmas.favouriteMovie();
  216. console.log(christmas.mostStarredActor());
  217.  
  218. module.exports = ChristmasMovies;
Add Comment
Please, Sign In to add comment