From a290ebcf5e1dfa0823421cdb4d9b42d9c41a515e Mon Sep 17 00:00:00 2001 From: Matt Loberg Date: Fri, 6 Nov 2020 21:26:18 -0600 Subject: [PATCH] test: fix magic item table tests --- src/data/table.test.ts | 8 +++----- src/data/table.ts | 40 ++++++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/data/table.test.ts b/src/data/table.test.ts index 412c51e..be37b38 100644 --- a/src/data/table.test.ts +++ b/src/data/table.test.ts @@ -1,10 +1,8 @@ import tables from './table'; -jest.mock('../utils', () => { - return { - random: jest.fn((x) => x[0]), - }; -}); +jest.mock('lodash', () => ({ + sample: jest.fn((x) => x[0]), +})); jest.mock('./spells', () => { return () => [ { diff --git a/src/data/table.ts b/src/data/table.ts index 69b42cb..2661190 100644 --- a/src/data/table.ts +++ b/src/data/table.ts @@ -1,8 +1,15 @@ +import { sample } from 'lodash'; + import { Dictionary } from '../types'; -import { random } from '../utils'; import spells from './spells'; -const spell = async (lvl: number) => random((await spells()).filter((s) => s.level === lvl)).spell; +const spell = async (lvl: number) => { + const list = await spells(); + const options = list.filter((s) => s.level === lvl); + const selected = sample(options); + + return selected ? selected.spell : `A level ${lvl} spell`; +}; const table: Dictionary<(string | (() => Promise))[]> = { a: [ @@ -173,18 +180,19 @@ const table: Dictionary<(string | (() => Promise))[]> = { ], g: [ ...new Array(11).fill('Weapon, +2'), // 01–11 - ...new Array(3).fill(() => - random([ - // 12–14 - 'Figurine of wondrous power (Bronze griffon)', - 'Figurine of wondrous power (Ebony fly)', - 'Figurine of wondrous power (Golden lion)', - 'Figurine of wondrous power (Ivory goat)', - 'Figurine of wondrous power (Marble elephant)', - 'Figurine of wondrous power (Onyx dog)', - 'Figurine of wondrous power (Onyx dog)', - 'Figurine of wondrous power (Serpentine owl)', - ]), + ...new Array(3).fill( + (): string => + sample([ + // 12–14 + 'Figurine of wondrous power (Bronze griffon)', + 'Figurine of wondrous power (Ebony fly)', + 'Figurine of wondrous power (Golden lion)', + 'Figurine of wondrous power (Ivory goat)', + 'Figurine of wondrous power (Marble elephant)', + 'Figurine of wondrous power (Onyx dog)', + 'Figurine of wondrous power (Onyx dog)', + 'Figurine of wondrous power (Serpentine owl)', + ]) ?? 'Figurine of wondrous power', ), 'Adamantine armor (breastplate)', // 15 'Adamantine armor (splint)', // 16 @@ -374,7 +382,7 @@ const table: Dictionary<(string | (() => Promise))[]> = { ...new Array(2).fill('Armor, +2 studded leather'), // 72–73 ...new Array(2).fill('Well of many worlds'), // 74–75 (): string => - random([ + sample([ // 76 'Armor, +2 half plate', 'Armor, +2 half plate', @@ -388,7 +396,7 @@ const table: Dictionary<(string | (() => Promise))[]> = { 'Armor, +3 splint', 'Armor, +3 half plate', 'Armor, +3 plate', - ]), + ]) ?? 'Armor +2/+3', 'Apparatus of Kwalish', // 77 'Armor of invulnerability', // 78 'Belt of storm giant strength', // 79