Skip to content

Commit 419d889

Browse files
committed
tests: Added unit test for card use-cases
1 parent 1b88c0a commit 419d889

19 files changed

+698
-12
lines changed

api/src/application/use-cases/card/CheckProjectMemberByCardAndList.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ class CheckProjectMemberByCardAndListUseCase {
66
}
77

88
async execute(userId, listId, cardId) {
9+
if (!userId) throw new Error('userId was not provided');
10+
if (!listId) throw new Error('listId was not provided');
11+
if (!cardId) throw new Error('cardId was not provided');
12+
913
const card = await this.cardRepository.checkProjectMemberByCardAndList(
1014
userId,
1115
listId,
1216
cardId,
1317
);
14-
return new CardDto(card);
18+
19+
return card?.id ? new CardDto(card) : {};
1520
}
1621
}
1722

api/src/application/use-cases/card/CreateCardUseCase.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@ class CreateCardUseCase {
77
}
88

99
async execute(listId, cardData) {
10+
if (!listId) throw new Error('listId was not provided');
11+
1012
const cardEntity = new CardEntity(cardData);
1113
const card = await this.cardRepository.create(listId, cardEntity);
14+
15+
if (!card?.id) {
16+
throw new Error('Something went wrong creating the card.');
17+
}
18+
1219
return new CardDto(card);
1320
}
1421
}

api/src/application/use-cases/card/DeleteCardUseCase.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ class DeleteCardUseCase {
44
}
55

66
async execute(cardId) {
7-
return this.cardRepository.delete(cardId);
7+
if (!cardId) throw new Error('cardId was not provided');
8+
9+
const result = await this.cardRepository.delete(cardId);
10+
11+
if (result === 0) {
12+
throw new Error(
13+
'Something went wrong deleting the card. Maybe the cardId does not exist',
14+
);
15+
}
16+
17+
return result;
818
}
919
}
1020

api/src/application/use-cases/card/GetAllCardInformationUseCase.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ class GetAllCardInformationUseCase {
66
}
77

88
async execute(listId, cardId) {
9+
if (!listId) throw new Error('listId was not provided');
10+
if (!cardId) throw new Error('cardId was not provided');
11+
912
const card = await this.cardRepository.findAllCardInformation(
1013
listId,
1114
cardId,
1215
);
13-
return CardDto.withAllCardInformation(card);
16+
17+
return card?.id ? CardDto.withAllCardInformation(card) : {};
1418
}
1519
}
1620

api/src/application/use-cases/card/GetAllUseCase.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ class GetAllUseCase {
44
}
55

66
async execute(listId) {
7-
return this.cardRepository.findAll(listId);
7+
if (!listId) throw new Error('listId was not provided');
8+
9+
const cards = await this.cardRepository.findAll(listId);
10+
11+
return cards?.length > 0 ? cards : [];
812
}
913
}
1014

api/src/application/use-cases/card/GetCardUseCase.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ class GetCardUseCase {
66
}
77

88
async execute(cardId) {
9+
if (!cardId) throw new Error('cardId was not provided');
10+
911
const card = await this.cardRepository.findOneById(cardId);
10-
return new CardDto(card);
12+
13+
return card?.id ? new CardDto(card) : {};
1114
}
1215
}
1316

api/src/application/use-cases/card/GetProjectMemberByCardUseCase.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ class GetProjectMemberByCardUseCase {
99
}
1010

1111
async execute(userId, cardId) {
12+
if (!userId) throw new Error('userId was not provided');
13+
if (!cardId) throw new Error('cardId was not provided');
14+
1215
const card = await this.cardRepository.findOneByIdWithList(cardId);
16+
1317
if (!card?.id) throw boom.notFound('Card not found');
18+
1419
const projectMember =
1520
await this.projectMemberRepository.getProjectMemberByCard(userId, card);
16-
return new ProjectMemberDto(projectMember);
21+
22+
return projectMember?.id ? new ProjectMemberDto(projectMember) : {};
1723
}
1824
}
1925

api/src/application/use-cases/card/UpdateCardUseCase.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class UpdateCardUseCase {
99
}
1010

1111
async execute(cardId, cardData) {
12+
if (!cardId) throw new Error('cardId was not provided');
13+
if (!cardData) throw new Error('cardData was not provided');
14+
1215
const cardName = new CardName(cardData.newName).value;
1316
const cardDescription = new CardDescription(cardData.description).value;
1417

api/src/domain/value-objects/cardDescription.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class CardDescription {
66
this.value = value;
77
}
88

9-
// eslint-disable-next-line class-methods-use-this
109
ensureIsValid(value) {
1110
if (typeof value !== 'string' || value.trim() === '') {
1211
throw boom.badData('CardDescription must be a non-empty string.');
@@ -16,9 +15,9 @@ class CardDescription {
1615
}
1716
}
1817

19-
get value() {
20-
return this.value;
21-
}
18+
// get value() {
19+
// return this.value;
20+
// }
2221

2322
toString() {
2423
return this.value;

api/src/domain/value-objects/cardName.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class CardName {
88
this.#value = value;
99
}
1010

11-
// eslint-disable-next-line class-methods-use-this
1211
validate(value) {
1312
if (typeof value !== 'string' || value.trim() === '') {
1413
throw boom.badData('CardName must be a non-empty string.');

0 commit comments

Comments
 (0)