Skip to content

Instantly share code, notes, and snippets.

@laugri
Last active December 3, 2018 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laugri/4dfa5dbfc507148467a648dcbf82407b to your computer and use it in GitHub Desktop.
Save laugri/4dfa5dbfc507148467a648dcbf82407b to your computer and use it in GitHub Desktop.
DDD Example - use case test
import { articleTestFactory } from '~/domain/article';
import { shoppingCartTestFactory, itemTestFactory } from '~/domain/shoppingCart';
import { InMemoryShoppingCartRepository } from '~/infrastructure/InMemoryShoppingCartRepository';
import { InMemoryArticleRepository } from '~/infrastructure/InMemoryArticleRepository';
describe('addArticleToCart', () => {
it('should update, persist and return the cart with a new article', async () => {
// Arrange
const shoppingCartRepository = new InMemoryShoppingCartRepository();
const shoppingCart = shoppingCartTestFactory({items: []})
shoppingCartRepository.store(shoppingCart);
const articleRepository = new InMemoryArticleRepository();
const article = articleTestFactory();
articleRepository.store(article);
// Act
const newShoppingCart = await addArticleToCart(
{
shoppingCartRepository,
articleRepository
shoppingCartId: shoppingCart.id,
articleId: article.id,
quantity: 1,
},
);
// Assert
const expectedShoppingCart = shoppingCartTestFactory({
items: [itemTestFactory({ article, quantity: 1 })],
});
expect(newShoppingCart).toEqual(expectedShoppingCart);
expect(
await shoppingCartRepository.findById(shoppingCart.id),
).toEqual(expectedShoppingCart);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment