-
-
Save Balastrong/f8cf757a5b8e39d9cca16a6cdc75a695 to your computer and use it in GitHub Desktop.
wrand - tests 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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