-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
169 lines (139 loc) · 5.52 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Store, Movie, Rental, PriceCode, Statement, Customer } from './engine.js'
let chai = require('chai')
chai.should()
let store = new Store()
let cinderella = store.addMovie('Cinderella', Store.PRICE_CODE_CHILDRENS)
let star_wars = store.addMovie('Star Wars', Store.PRICE_CODE_REGULAR)
let gladiator = store.addMovie('Gladiator', Store.PRICE_CODE_NEW_RELEASE)
let john_smith = store.addCustomer('John Smith')
john_smith.addRental(cinderella, 5)
john_smith.addRental(star_wars, 5)
john_smith.addRental(gladiator, 5)
describe("Store", function() {
describe("movies", function() {
it("should be 3", function(){
store.movies.length.should.equal(3)
})
it("should have the correct titles", function(){
store.movies[0].title.should.equal('Cinderella')
store.movies[1].title.should.equal('Star Wars')
store.movies[2].title.should.equal('Gladiator')
})
it("should have the correct price codes", function(){
store.movies[0].priceCode.name.should.equal('CHILDRENS')
store.movies[1].priceCode.name.should.equal('REGULAR')
store.movies[2].priceCode.name.should.equal('NEW RELEASE')
})
})
describe("customers", function() {
it("should be 1", function(){
store.customers.length.should.equal(1)
})
it("should have the correct name", function(){
store.customers[0].name.should.equal('John Smith')
})
})
let customer = store.customers[0]
describe("rentals", function() {
it("should be 3", function(){
customer.rentals.length.should.equal(3)
})
it("should be for the correct movies", function(){
customer.rentals[0].movie.title.should.equal('Cinderella')
customer.rentals[1].movie.title.should.equal('Star Wars')
customer.rentals[2].movie.title.should.equal('Gladiator')
})
it("should be for the correct number of days rented", function(){
customer.rentals[0].daysRented.should.equal(5)
customer.rentals[1].daysRented.should.equal(5)
customer.rentals[2].daysRented.should.equal(5)
})
})
})
describe("Statement", () => {
describe("calculateRental()", () => {
it("contains the expected data", () => {
const cinderella = new Movie('Cinderella', Store.PRICE_CODE_CHILDRENS)
const star_wars = new Movie('Star Wars', Store.PRICE_CODE_REGULAR)
const gladiator = new Movie('Gladiator', Store.PRICE_CODE_NEW_RELEASE)
const john_smith = new Customer("John Smith")
john_smith.addRental(cinderella, 5)
john_smith.addRental(star_wars, 5)
john_smith.addRental(gladiator, 5)
const statement = new Statement(john_smith)
statement.rentalSummary.should.deep.equal([
{ movieTitle: 'Cinderella', rentalPrice: 3 },
{ movieTitle: 'Star Wars', rentalPrice: 6.5 },
{ movieTitle: 'Gladiator', rentalPrice: 15 },
])
statement.totalAmount.should.equal(24.5)
statement.frequentRenterPoints.should.equal(4)
})
})
describe("print()", () => {
it("prints the statement in expected format", () => {
const customer = store.customers[0]
const expected =
`Rental record for John Smith
\tCinderella\t3
\tStar Wars\t6.5
\tGladiator\t15
Amount owed is 24.5
You earned 4 frequent renter points.`
customer.printStatement().should.equal(expected)
})
})
})
describe("Rental", () => {
describe("calculatePrice()", () => {
describe("price for a Regular movie", () => {
it("correctly calculates for less than or equal to 2 days", () => {
const movie = new Movie("Hamilton", new PriceCode(PriceCode.REGULAR))
const rental1 = new Rental(movie, 1)
rental1.calculatePrice().should.equal(2)
const rental2 = new Rental(movie, 2)
rental2.calculatePrice().should.equal(2)
})
it("correctly calculates for above 2 days", () => {
const movie = new Movie("Hamilton", new PriceCode(PriceCode.REGULAR))
const rental = new Rental(movie, 3)
rental.calculatePrice().should.equal(3.5)
})
})
describe("price for a New Release movie", () => {
it("correctly calculates the price", () => {
const movie = new Movie("Hamilton", new PriceCode(PriceCode.NEW_RELEASE))
const rental = new Rental(movie, 3)
rental.calculatePrice().should.equal(9)
})
})
describe("price for a Childrens movie", () => {
it("correctly calculates for less than or equal to 3 days", () => {
const movie = new Movie("Hamilton", new PriceCode(PriceCode.CHILDRENS))
const rental1 = new Rental(movie, 1)
rental1.calculatePrice().should.equal(1.5)
const rental2 = new Rental(movie, 3)
rental2.calculatePrice().should.equal(1.5)
})
it("correctly calculates for above 3 days", () => {
const movie = new Movie("Hamilton", new PriceCode(PriceCode.CHILDRENS))
const rental1 = new Rental(movie, 4)
rental1.calculatePrice().should.equal(1.5)
const rental2 = new Rental(movie, 5)
rental2.calculatePrice().should.equal(3)
})
})
})
describe("calculateFrequentRenterPoints()", () => {
it("returns 2 for two-day New Release rentals", () => {
const movie = new Movie("Hamilton", new PriceCode(PriceCode.NEW_RELEASE))
const rental = new Rental(movie, 3)
rental.calculateFrequentRenterPoints().should.equal(2)
})
it("returns 1 for all others", () => {
const movie = new Movie("Hamilton", new PriceCode(PriceCode.REGULAR))
const rental = new Rental(movie, 3)
rental.calculateFrequentRenterPoints().should.equal(1)
})
})
})