Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

question from:https://stackoverflow.com/questions/66064233/jest-unit-test-check-for-unique-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
213 views
Welcome To Ask or Share your Answers For Others

1 Answer

You could just create a nested dictionary and immediately fail if you see a duplicated identifier on the nested dictionary checking the order number.

In this manner we avoid looping more than once over the entire collection.

function testReceipts(deliveryReceipts) {
  const orderCheck = {}
  for (const receipt of deliveryReceipts) {
    orderCheck[receipt.id] = Boolean(orderCheck[receipt.id]) ? orderCheck[receipt.id] : {};
    for (const item of receipt.items) {
      const orderNumber = item.orderNumber;
      expect(!orderCheck[receipt.id].hasOwnProperty(orderNumber)).toBeTruthy();
      orderCheck[receipt.id][orderNumber] = ""
    }
  }
}

just for the heck of it I wrote a small stub to run the testReceipts test

// arrange
const knownGood = [{
    id: 1,
    items: [{
      orderNumber: 1
    }, {
      orderNumber: 2
    }, {
      orderNumber: 3
    }]
  },
  {
    id: 2,
    items: [{
      orderNumber: 1
    }, {
      orderNumber: 2
    }, {
      orderNumber: 3
    }]
  }
];

const knownBad = [{
    id: 1,
    items: [{
      orderNumber: 1
    }, {
      orderNumber: 2
    }, {
      orderNumber: 1
    }]
  },
  {
    id: 2,
    items: [{
      orderNumber: 1
    }, {
      orderNumber: 2
    }, {
      orderNumber: 3
    }]
  }
];

// assert
testReceipts(knownGood); // pass.
testReceipts(knownBad); // Bad code, bad!

// I didn't want to install Jest, so I made my own little expect wrapper. You'd obviously use Jest.
function expect(arg) {
  return ({
    toBeTruthy: () => {
      if (!Boolean(arg)) console.error("Bad code, bad!");
    }
  })
}



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...