|
10 | 10 | preferred_max_items: max_items |
11 | 11 | ) |
12 | 12 | end |
13 | | - let(:line_item) do |
14 | | - mock_model( |
15 | | - Spree::LineItem, quantity: quantity |
16 | | - ) |
17 | | - end |
| 13 | + |
18 | 14 | let(:first_item) { 0 } |
19 | 15 | let(:additional_item) { 0 } |
20 | 16 | let(:max_items) { 0 } |
21 | 17 |
|
22 | | - let(:line_item) do |
23 | | - mock_model( |
24 | | - Spree::LineItem, quantity: quantity |
25 | | - ) |
26 | | - end |
27 | | - |
28 | 18 | it_behaves_like "a promotion calculator" |
29 | 19 |
|
30 | | - context "compute" do |
| 20 | + context "compute_line_item" do |
| 21 | + let(:line_item) do |
| 22 | + mock_model( |
| 23 | + Spree::LineItem, quantity: quantity |
| 24 | + ) |
| 25 | + end |
| 26 | + |
31 | 27 | subject { calculator.compute(line_item) } |
32 | 28 |
|
33 | 29 | context "with all amounts 0" do |
|
176 | 172 | end |
177 | 173 | end |
178 | 174 |
|
| 175 | + context "compute_price" do |
| 176 | + let(:variant) { mock_model(Spree::Variant) } |
| 177 | + let(:price) { mock_model(Spree::Price, amount: 12, variant: variant, currency: "USD") } |
| 178 | + let(:order) { mock_model(Spree::Order, line_items: [line_item]) } |
| 179 | + let(:line_item_quantity) { 0 } |
| 180 | + let(:line_item) do |
| 181 | + mock_model( |
| 182 | + Spree::LineItem, |
| 183 | + quantity: line_item_quantity, |
| 184 | + variant: variant |
| 185 | + ) |
| 186 | + end |
| 187 | + |
| 188 | + subject { calculator.compute(price, { order: order, quantity: quantity }) } |
| 189 | + |
| 190 | + context "with no order given" do |
| 191 | + let(:order) { nil } |
| 192 | + |
| 193 | + context "when first_item and additional_item have values" do |
| 194 | + let(:first_item) { 1.13 } |
| 195 | + let(:additional_item) { 2.11 } |
| 196 | + |
| 197 | + context "with quantity 2" do |
| 198 | + let(:quantity) { 2 } |
| 199 | + |
| 200 | + it { is_expected.to eq(1.62) } |
| 201 | + end |
| 202 | + end |
| 203 | + end |
| 204 | + |
| 205 | + context "if nothing is in the cart" do |
| 206 | + let(:line_item_quantity) { 0 } |
| 207 | + |
| 208 | + context "when first_item and additional_items have values" do |
| 209 | + let(:first_item) { 1.13 } |
| 210 | + let(:additional_item) { 2.11 } |
| 211 | + |
| 212 | + context "with quantity 0" do |
| 213 | + let(:quantity) { 0 } |
| 214 | + |
| 215 | + it { is_expected.to eq 0 } |
| 216 | + end |
| 217 | + |
| 218 | + context "with quantity 1" do |
| 219 | + let(:quantity) { 1 } |
| 220 | + |
| 221 | + it { is_expected.to eq 1.13 } |
| 222 | + end |
| 223 | + |
| 224 | + context "with quantity 2" do |
| 225 | + let(:quantity) { 2 } |
| 226 | + |
| 227 | + it { is_expected.to eq 1.62 } |
| 228 | + end |
| 229 | + |
| 230 | + context "with quantity 10" do |
| 231 | + let(:quantity) { 3 } |
| 232 | + |
| 233 | + it { is_expected.to eq 1.78 } |
| 234 | + end |
| 235 | + |
| 236 | + context "with quantity 10" do |
| 237 | + let(:quantity) { 10 } |
| 238 | + |
| 239 | + it { is_expected.to eq 2.01 } |
| 240 | + end |
| 241 | + |
| 242 | + context "with max_items 5" do |
| 243 | + let(:max_items) { 5 } |
| 244 | + |
| 245 | + context "with quantity 0" do |
| 246 | + let(:quantity) { 0 } |
| 247 | + |
| 248 | + it { is_expected.to eq 0 } |
| 249 | + end |
| 250 | + |
| 251 | + context "with quantity 1" do |
| 252 | + let(:quantity) { 1 } |
| 253 | + |
| 254 | + it { is_expected.to eq 1.13 } |
| 255 | + end |
| 256 | + |
| 257 | + context "with quantity 2" do |
| 258 | + let(:quantity) { 2 } |
| 259 | + |
| 260 | + it { is_expected.to eq 1.62 } |
| 261 | + end |
| 262 | + |
| 263 | + context "with quantity 5" do |
| 264 | + let(:quantity) { 5 } |
| 265 | + |
| 266 | + it { is_expected.to eq 1.91 } |
| 267 | + end |
| 268 | + |
| 269 | + context "with quantity 10" do |
| 270 | + let(:quantity) { 10 } |
| 271 | + |
| 272 | + it { is_expected.to eq 0.96 } |
| 273 | + end |
| 274 | + end |
| 275 | + end |
| 276 | + end |
| 277 | + |
| 278 | + context "with items already in the cart" do |
| 279 | + let(:line_item_quantity) { 2 } |
| 280 | + |
| 281 | + context "when first_item and additional_items have values" do |
| 282 | + let(:first_item) { 1.13 } |
| 283 | + let(:additional_item) { 2.11 } |
| 284 | + |
| 285 | + context "with quantity 0" do |
| 286 | + let(:quantity) { 0 } |
| 287 | + |
| 288 | + it { is_expected.to eq 0 } |
| 289 | + end |
| 290 | + |
| 291 | + context "with quantity 1" do |
| 292 | + let(:quantity) { 1 } |
| 293 | + |
| 294 | + it { is_expected.to eq 2.11 } |
| 295 | + end |
| 296 | + |
| 297 | + context "with quantity 2" do |
| 298 | + let(:quantity) { 2 } |
| 299 | + |
| 300 | + it { is_expected.to eq 2.11 } |
| 301 | + end |
| 302 | + |
| 303 | + context "with quantity 10" do |
| 304 | + let(:quantity) { 3 } |
| 305 | + |
| 306 | + it { is_expected.to eq 2.11 } |
| 307 | + end |
| 308 | + |
| 309 | + context "with quantity 10" do |
| 310 | + let(:quantity) { 10 } |
| 311 | + |
| 312 | + it { is_expected.to eq 2.11 } |
| 313 | + end |
| 314 | + |
| 315 | + context "with max_items 5" do |
| 316 | + let(:max_items) { 5 } |
| 317 | + |
| 318 | + context "with quantity 0" do |
| 319 | + let(:quantity) { 0 } |
| 320 | + |
| 321 | + it { is_expected.to eq 0 } |
| 322 | + end |
| 323 | + |
| 324 | + context "with quantity 1" do |
| 325 | + let(:quantity) { 1 } |
| 326 | + |
| 327 | + it { is_expected.to eq 2.11 } |
| 328 | + end |
| 329 | + |
| 330 | + context "with quantity 2" do |
| 331 | + let(:quantity) { 2 } |
| 332 | + |
| 333 | + it { is_expected.to eq 2.11 } |
| 334 | + end |
| 335 | + |
| 336 | + context "with quantity 5" do |
| 337 | + let(:quantity) { 5 } |
| 338 | + |
| 339 | + it { is_expected.to eq 1.27 } |
| 340 | + end |
| 341 | + |
| 342 | + context "with quantity 10" do |
| 343 | + let(:quantity) { 10 } |
| 344 | + |
| 345 | + it { is_expected.to eq 0.63 } |
| 346 | + end |
| 347 | + end |
| 348 | + end |
| 349 | + end |
| 350 | + end |
| 351 | + |
179 | 352 | it "allows creation of new object with all the attributes" do |
180 | 353 | attributes = { preferred_first_item: 1, preferred_additional_item: 1, preferred_max_items: 1 } |
181 | 354 | calculator = described_class.new(attributes) |
|
0 commit comments