Skip to content

Instantly share code, notes, and snippets.

@Balastrong
Created August 17, 2022 08:34
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 Balastrong/f8cf757a5b8e39d9cca16a6cdc75a695 to your computer and use it in GitHub Desktop.
Save Balastrong/f8cf757a5b8e39d9cca16a6cdc75a695 to your computer and use it in GitHub Desktop.
wrand - tests 1
const items = [
{ original: "Bronze", weight: 20 },
{ original: "Silver", weight: 10 },
{ original: "Gold", weight: 3 },
{ original: "Platinum", weight: 1 },
];
describe("RandomPicker", () => {
it("should be created with a list of items with their weight", () => {
const picker = new RandomPicker(items);
expect(picker).toBeDefined();
});
it("should be able to pick an item", () => {
const picker = new RandomPicker(items);
const pickedItem = picker.pick();
expect(items.some((i) => i.original === pickedItem)).toBeTruthy();
});
it("should be able to pick N items", () => {
const picker = new RandomPicker(items);
const picked = picker.pickMany(3);
expect(picked.length).toBe(3);
expect(
picked.every((p) => items.some((i) => i.original === p))
).toBeTruthy();
});
it("works with objects", () => {
const objectItems = [
{ original: { name: "Bronze" }, weight: 20 },
{ original: { name: "Silver" }, weight: 10 },
{ original: { name: "Gold" }, weight: 3 },
{ original: { name: "Platinum" }, weight: 1 },
];
const picker = new RandomPicker(objectItems);
const pickedItem = picker.pick();
expect(
objectItems.some((item) => item.original.name === pickedItem.name)
).toBeTruthy();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment