Skip to content

Commit

Permalink
Split up Auction test into units by flow
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaw92 committed Nov 15, 2020
1 parent 3d20606 commit 41f4d2c
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions code/in-search-of-a-unit/test/Auction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,47 @@ function testOffer(amount: number, currency = 'PLN', user?: string): Offer {
};
}

function stuffAuction(auction: Auction, count: number) {
const madeOffers = [];
for (let i = 0; i < 5; i++) {
const offer = testOffer(10);
madeOffers.push(offer);
auction.makeOffer(offer);
}
return madeOffers;
}

describe('Auction', function() {
it('should carry out an auction and select the winners', async function() {
it('should immediately reject an offer that could not possibly win', async function() {
const notifier = new EmailNotifier();
const sale = new Auction('PLN', 5, notifier);
// Initially, everybody goes into auction at 10 PLN:
const initialOffers = [];
for (let i = 0; i < 5; i++) {
const offer = testOffer(10);
initialOffers.push(offer);
sale.makeOffer(offer);
}
const initialOffers = stuffAuction(sale, 5);
// Subsequent offers for the same amount should be rejected:
const sixthOfferResult = await sale.makeOffer(testOffer(10));
assert.strictEqual(sixthOfferResult, false);
// However, a new offer should annul the last one made:
});
it('should replace the last offer if a better one comes in', async function() {
const notifier = new EmailNotifier();
const sale = new Auction('PLN', 5, notifier);
const initialOffers = stuffAuction(sale, 5);
// A new offer should annul the last one made:
const lastOffer = sale.getQualifyingOffers().pop()!;
const betterOffer = testOffer(15);
await sale.makeOffer(betterOffer);
assert.ok(sale.getQualifyingOffers().includes(betterOffer));
assert.ok(!sale.getQualifyingOffers().includes(lastOffer));
});
it('should select the winners after some offers are replaced', async function() {
const notifier = new EmailNotifier();
const sale = new Auction('PLN', 5, notifier);
const initialOffers = stuffAuction(sale, 5);
const lastOffer = sale.getQualifyingOffers().pop()!;
const betterOffer = testOffer(15);
await sale.makeOffer(betterOffer);
const winners = await sale.end();
// Make sure the winners are: 4/5 of the initial offers and
// the one better offer which was made later.
const winners = await sale.end();
assert.strictEqual(winners.length, 5);
for (let offer of initialOffers.slice(0, -1)) {
assert.ok(winners.includes(offer));
Expand Down

0 comments on commit 41f4d2c

Please sign in to comment.