|
| 1 | +const createMovie = require("./create-movie"); |
| 2 | + |
| 3 | +describe("Create movie", () => { |
| 4 | + it("Requires movie with a title", () => { |
| 5 | + const fakeDatabase = { |
| 6 | + getAll: () => {}, |
| 7 | + saveData: () => {} |
| 8 | + }; |
| 9 | + const title = ""; |
| 10 | + const description = "some description here"; |
| 11 | + const mockCreateMovie = jest.spyOn(fakeDatabase, "getAll"); |
| 12 | + expect(createMovie(fakeDatabase)(title, description)).toBe( |
| 13 | + "Movie title was required" |
| 14 | + ); |
| 15 | + expect(mockCreateMovie).not.toHaveBeenCalled(); |
| 16 | + }); |
| 17 | + |
| 18 | + it("Requires movie with a description", () => { |
| 19 | + const fakeDatabase = { |
| 20 | + getAll: () => {}, |
| 21 | + saveData: () => {} |
| 22 | + }; |
| 23 | + const title = "Star Wars"; |
| 24 | + const description = ""; |
| 25 | + const mockCreateMovie = jest.spyOn(fakeDatabase, "getAll"); |
| 26 | + expect(createMovie(fakeDatabase)(title, description)).toBe( |
| 27 | + "Movie description was required" |
| 28 | + ); |
| 29 | + expect(mockCreateMovie).not.toHaveBeenCalled(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("Requires movie with a title and a description", () => { |
| 33 | + const title = ""; |
| 34 | + const description = ""; |
| 35 | + expect(createMovie()(title, description)).toBe( |
| 36 | + "Movie title and description was required" |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it("Should not have duplicate movie name", () => { |
| 41 | + const fakeDatabase = { |
| 42 | + getAll: () => { |
| 43 | + return [ |
| 44 | + { title: "DespicableMe", description: "Comedy" }, |
| 45 | + { title: "DespicableMe2", description: "Comedy" }, |
| 46 | + { title: "DespicableMe3", description: "Comedy" } |
| 47 | + ]; |
| 48 | + } |
| 49 | + }; |
| 50 | + expect(createMovie(fakeDatabase)("DespicableMe", "Comedy")).toBe( |
| 51 | + "Movie name is duplicate." |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it("Should save the data in JSON format", () => { |
| 56 | + const fakeDatabase = { |
| 57 | + getAll: () => { |
| 58 | + return [ |
| 59 | + { |
| 60 | + id: 1, |
| 61 | + title: "DespicableMe", |
| 62 | + description: "Comedy" |
| 63 | + } |
| 64 | + ]; |
| 65 | + }, |
| 66 | + saveData: () => { |
| 67 | + return { |
| 68 | + id: 2, |
| 69 | + title: "DespicableMe2", |
| 70 | + description: "Comedy" |
| 71 | + }; |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + const expectedData = { |
| 76 | + id: 2, |
| 77 | + title: "DespicableMe2", |
| 78 | + description: "Comedy" |
| 79 | + }; |
| 80 | + |
| 81 | + expect(createMovie(fakeDatabase)("DespicableMe2", "Comedy")).toBe( |
| 82 | + JSON.stringify(expectedData) |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it("Should call the getAll method once", () => { |
| 87 | + const fakeDatabase = { |
| 88 | + getAll: () => { |
| 89 | + return [ |
| 90 | + { |
| 91 | + id: 1, |
| 92 | + title: "DespicableMe", |
| 93 | + description: "Comedy" |
| 94 | + } |
| 95 | + ]; |
| 96 | + }, |
| 97 | + saveData: () => { |
| 98 | + return { |
| 99 | + id: 2, |
| 100 | + title: "DespicableMe2", |
| 101 | + description: "Comedy" |
| 102 | + }; |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + const mockCreateMovie = jest.spyOn(fakeDatabase, "getAll"); |
| 107 | + const result = createMovie(fakeDatabase)("DespicableMe2", "Comedy"); |
| 108 | + |
| 109 | + expect(mockCreateMovie).toHaveBeenCalled(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments