I am new to unit tests and writing them for my components and class. I have a method which filters duplicate numbers but with the same ID. I wrote this method:
public groupByOrderNumber(
deliveryReceipts: DeliveryReceiptSearch[]
): Map<string, Map<string, DeliveryReceiptItemSearchResult[]>> {
const groupedDeliveryReceipts = new Map<string, Map<string, DeliveryReceiptItemSearchResult[]>>();
for (const receipt of deliveryReceipts) {
const receiptId: string = receipt.id;
groupedDeliveryReceipts.set(receiptId, new Map<string, DeliveryReceiptItemSearchResult[]>());
for (const item of receipt.items) {
const orderNumber = item.orderNumber;
if (groupedDeliveryReceipts.get(receiptId).has(orderNumber)) {
groupedDeliveryReceipts.get(receiptId).get(orderNumber).push(item);
} else {
groupedDeliveryReceipts.get(receiptId).set(orderNumber, [item]);
}
}
}
return groupedDeliveryReceipts;
}
My tests are like this:
describe('groupByOrderNumber', () => {
const deliveryReceipts: DeliveryReceiptSearchMock[] = DeliveryReceiptSearchMock.createListOfReceipts();
it('should not contain identical orderNumbers', () => {
const dReceipts: Map<string, Map<string, DeliveryReceiptItemSearchResult[]>> = service.groupByOrderNumber(
deliveryReceipts
);
for (const receipts of deliveryReceipts) {
const receiptId: string = receipts.id;
for (const item of receipts.items) {
const orderNumber = item.orderNumber;
expect(dReceipts.get(receiptId).has(orderNumber)).toIn
expect(dReceipts.get(receipts.id).set(orderNumber, [item])).not.toEqual(dReceipts.get(receipts.id).get(orderNumber));
}
}
});
});
The test will mock some data for the purpose of the test as seen at the beginning.
I am not quite sure how I test if the orderNumber
is unique to its' receipt.id
.
Is there a way to use some native jest methods?
Example:
receipt.id ('9876') -> orderNumber ('123')
receipt.id ('101112') -> orderNumber ('123')
That's what groupByOrderNumber()
basically does.