Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 118 additions & 3 deletions src/fillTank.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,124 @@
'use strict';

describe('fillTank', () => {
// const { fillTank } = require('./fillTank');
const { fillTank } = require('./fillTank');

it('should ', () => {});
it('should fill the requested amount of fuel', () => {
const customer = {
money: 3000,
vehicle: {
maxTankCapacity: 40,
fuelRemains: 8,
},
};

// write tests here
fillTank(customer, 50, 10);

expect(customer.vehicle.fuelRemains).toBe(18);
expect(customer.money).toBe(2500);
});

it('should fill the tank fully if amount is not given', () => {
const customer = {
money: 3000,
vehicle: {
maxTankCapacity: 40,
fuelRemains: 8,
},
};

fillTank(customer, 50);

expect(customer.vehicle.fuelRemains).toBe(40);
expect(customer.money).toBe(1400);
});

it('should fill only free space if requested amount is too big', () => {
const customer = {
money: 3000,
vehicle: {
maxTankCapacity: 40,
fuelRemains: 35,
},
};

fillTank(customer, 50, 10);

expect(customer.vehicle.fuelRemains).toBe(40);
expect(customer.money).toBe(2750);
});

it('should fill only the amount the customer can afford', () => {
const customer = {
money: 180,
vehicle: {
maxTankCapacity: 40,
fuelRemains: 10,
},
};

fillTank(customer, 50, 10);

expect(customer.vehicle.fuelRemains).toBe(13.6);
expect(customer.money).toBe(0);
});

it('should round fuel down to one decimal place', () => {
const customer = {
money: 333,
vehicle: {
maxTankCapacity: 40,
fuelRemains: 10,
},
};

fillTank(customer, 50, 10);

expect(customer.vehicle.fuelRemains).toBe(16.6);
expect(customer.money).toBe(3);
});

it('should not fill fuel if rounded amount is less than 2 liters', () => {
const customer = {
money: 90,
vehicle: {
maxTankCapacity: 40,
fuelRemains: 10,
},
};

fillTank(customer, 50, 10);

expect(customer.vehicle.fuelRemains).toBe(10);
expect(customer.money).toBe(90);
});

it('should round fuel price to nearest hundredth', () => {
const customer = {
money: 100,
vehicle: {
maxTankCapacity: 40,
fuelRemains: 10,
},
};

fillTank(customer, 33.333, 3);

expect(customer.vehicle.fuelRemains).toBe(13);
expect(customer.money).toBe(0);
Comment on lines +96 to +108

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test correctly checks the price rounding requirement. To make your test suite more robust, consider adding another test case for an edge case: what happens if rounding the price upwards makes the purchase unaffordable for the customer?

For example, if a customer has 99.99 in their account, and the calculated cost is 99.999, it would round up to 100. According to the 'ALWAYS fill in only what the client can pay' rule, this transaction should not proceed. Testing this interaction between rules would strengthen your test suite.

});

it('should not return anything', () => {
const customer = {
money: 3000,
vehicle: {
maxTankCapacity: 40,
fuelRemains: 8,
},
};

const result = fillTank(customer, 50, 10);

expect(result).toBeUndefined();
});
});
Loading