Download EPUB

(Medium) When Code Duplication In Tests Is Acceptable - Cart Test Example With Abstraction

Gist 1 min read Original article

This article is too short to generate a summary.

(Medium) When Code Duplication In Tests Is Acceptable - Cart Test Example With Abstraction

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

const FakeCloth = {
any: () => {
return Product()
.withSize('XL') // Mandatory
.withColor('black'); // Mandatory
}
};
module('Shirts cart');
test('a valid product can be retrieved from the cart after being added', () => {
const fakeShirt = FakeCloth.any().asType('shirt');
const cart = ShirtsCart();
cart.addProduct(fakeShirt);
expect(cart).toContain(fakeShirt);
});
test('when we try to add an invalid product to the cart then it\'s not added', () => {
const fakePants = FakeCloth.any().asType('pants');
const cart = ShirtsCart();
cart.addProduct(fakePants);
expect(cart).not.toContain(fakePants);
});

Report this article

Keep this one

14 days free, no card. Then $60 a year ($5 a month, billed yearly) or $10 a month.

Your saves stay readable and exportable, even if you stop paying.