From 08e4a37c944877867a5d9e78dafe56aa3964c094 Mon Sep 17 00:00:00 2001 From: Serene Aronow Date: Thu, 15 Feb 2024 18:42:31 -0500 Subject: [PATCH 1/2] Did canTakeItemAmounts in a smart way, removed lodash-es from dependencies, and add tests for canTakeItemAmounts --- jest.config.js | 1 + package-lock.json | 47 +----- package.json | 2 - .../features/inventory/IgtInventory.ts | 24 +-- tests/smoke/Game.spec.ts | 2 +- .../features/inventory/Inventory.spec.ts | 155 ++++++++++++++++++ tsconfig.json | 3 +- 7 files changed, 178 insertions(+), 56 deletions(-) diff --git a/jest.config.js b/jest.config.js index dceb846..25ba9ed 100644 --- a/jest.config.js +++ b/jest.config.js @@ -8,6 +8,7 @@ module.exports = { testMatch: ["/tests/**/*.{ts, js}"], moduleNameMapper: { "@/(.*)": "/src/$1", + "@tests/(.*)": "/tests/$1", "^lodash-es$": "lodash" }, } diff --git a/package-lock.json b/package-lock.json index 9439955..317e22e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,13 +9,11 @@ "version": "0.0.0-development", "license": "MIT", "dependencies": { - "lodash-es": "^4.17.21", "mousetrap": "^1.6.5", "strongly-typed-events": "^2.1.5" }, "devDependencies": { "@types/jest": "^27.0.2", - "@types/lodash-es": "^4.17.5", "@types/mousetrap": "^1.6.8", "@typescript-eslint/eslint-plugin": "^4.33.0", "@typescript-eslint/parser": "^4.33.0", @@ -2114,21 +2112,6 @@ "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==", "dev": true }, - "node_modules/@types/lodash": { - "version": "4.14.175", - "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.175.tgz", - "integrity": "sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==", - "dev": true - }, - "node_modules/@types/lodash-es": { - "version": "4.17.5", - "resolved": "https://registry.npmjs.org/@types/lodash-es/-/lodash-es-4.17.5.tgz", - "integrity": "sha512-SHBoI8/0aoMQWAgUHMQ599VM6ZiSKg8sh/0cFqqlQQMyY9uEplc0ULU5yQNzcvdR4ZKa0ey8+vFmahuRbOCT1A==", - "dev": true, - "dependencies": { - "@types/lodash": "*" - } - }, "node_modules/@types/minimist": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz", @@ -7256,11 +7239,6 @@ "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, - "node_modules/lodash-es": { - "version": "4.17.21", - "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", - "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==" - }, "node_modules/lodash.capitalize": { "version": "4.2.1", "resolved": "https://registry.npmjs.org/lodash.capitalize/-/lodash.capitalize-4.2.1.tgz", @@ -10622,6 +10600,11 @@ "safer-buffer": "^2.0.2", "tweetnacl": "~0.14.0" }, + "bin": { + "sshpk-conv": "bin/sshpk-conv", + "sshpk-sign": "bin/sshpk-sign", + "sshpk-verify": "bin/sshpk-verify" + }, "engines": { "node": ">=0.10.0" } @@ -14969,21 +14952,6 @@ "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==", "dev": true }, - "@types/lodash": { - "version": "4.14.175", - "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.175.tgz", - "integrity": "sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==", - "dev": true - }, - "@types/lodash-es": { - "version": "4.17.5", - "resolved": "https://registry.npmjs.org/@types/lodash-es/-/lodash-es-4.17.5.tgz", - "integrity": "sha512-SHBoI8/0aoMQWAgUHMQ599VM6ZiSKg8sh/0cFqqlQQMyY9uEplc0ULU5yQNzcvdR4ZKa0ey8+vFmahuRbOCT1A==", - "dev": true, - "requires": { - "@types/lodash": "*" - } - }, "@types/minimist": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz", @@ -18864,11 +18832,6 @@ "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, - "lodash-es": { - "version": "4.17.21", - "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", - "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==" - }, "lodash.capitalize": { "version": "4.2.1", "resolved": "https://registry.npmjs.org/lodash.capitalize/-/lodash.capitalize-4.2.1.tgz", diff --git a/package.json b/package.json index 77a0eb7..be20ea0 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "license": "MIT", "devDependencies": { "@types/jest": "^27.0.2", - "@types/lodash-es": "^4.17.5", "@types/mousetrap": "^1.6.8", "@typescript-eslint/eslint-plugin": "^4.33.0", "@typescript-eslint/parser": "^4.33.0", @@ -36,7 +35,6 @@ "typescript-transform-paths": "^3.3.1" }, "dependencies": { - "lodash-es": "^4.17.21", "mousetrap": "^1.6.5", "strongly-typed-events": "^2.1.5" }, diff --git a/src/ig-template/features/inventory/IgtInventory.ts b/src/ig-template/features/inventory/IgtInventory.ts index 45389ea..31b4de2 100644 --- a/src/ig-template/features/inventory/IgtInventory.ts +++ b/src/ig-template/features/inventory/IgtInventory.ts @@ -1,5 +1,3 @@ -import {cloneDeep} from 'lodash-es'; - import {InventorySlot} from "@/ig-template/features/inventory/InventorySlot"; import {ItemId} from "@/ig-template/features/items/ItemId"; import {IgtFeature} from "@/ig-template/features/IgtFeature"; @@ -180,16 +178,22 @@ export class IgtInventory extends IgtFeature { return total; } - /** - * This method very inefficiently clones the inventory, and simulates adding the items see if they can be taken. - * It's also the only reason we're using lodash... - * TODO do this in a smart way. + /** + * Returns if the inventory can lose the given amounts of items */ canTakeItemAmounts(itemAmounts: ItemAmount[]): boolean { - const clonedInventory = cloneDeep(this); - for (const item of itemAmounts) { - const amountLeft = clonedInventory.gainItem(this._itemList.getItem(item.id), item.amount); - if (amountLeft !== 0) { + if (!itemAmounts.length) { + return false + } + + const mergedItemAmounts: Record = {}; + for (const itemAmount of itemAmounts) { + const { id, amount } = itemAmount; + const currentTotalAmount = (mergedItemAmounts[id] || 0) + amount; + mergedItemAmounts[id] = currentTotalAmount; + + const itemsInInventory = this.getTotalAmount(id); + if (itemsInInventory < currentTotalAmount) { return false; } } diff --git a/tests/smoke/Game.spec.ts b/tests/smoke/Game.spec.ts index b2c9dcd..f2dcddf 100644 --- a/tests/smoke/Game.spec.ts +++ b/tests/smoke/Game.spec.ts @@ -8,7 +8,7 @@ import {IgtStatistics} from "@/ig-template/features/statistics/IgtStatistics"; import {IgtAchievements} from "@/ig-template/features/achievements"; import {IgtFeatures} from "@/ig-template/IgtFeatures"; -class DummyGame extends IgtGame { +export class DummyGame extends IgtGame { features: IgtFeatures; constructor(features: IgtFeatures) { diff --git a/tests/unit/ig-template/features/inventory/Inventory.spec.ts b/tests/unit/ig-template/features/inventory/Inventory.spec.ts index 3f8646e..34d23c4 100644 --- a/tests/unit/ig-template/features/inventory/Inventory.spec.ts +++ b/tests/unit/ig-template/features/inventory/Inventory.spec.ts @@ -4,6 +4,9 @@ import {ItemId} from "@/ig-template/features/items/ItemId"; import {ItemType} from "@/ig-template/features/items/ItemType"; import {AbstractConsumable} from "@/ig-template/features/items/AbstractConsumable"; import {InventorySlot} from "@/ig-template/features/inventory/InventorySlot"; +import { ItemAmount } from "@/ig-template/features/items/ItemAmount"; +import { DummyGame } from "@tests/smoke/Game.spec"; +import { IgtItemList } from "@/ig-template/features/items/IgtItemList"; export class ExampleItem extends AbstractItem { @@ -222,4 +225,156 @@ describe('Inventory', () => { expect(inventory.isEmpty()).toBe(true); expect(consumable.isConsumed).toBe(true); }); + + describe('canTakeItemAmounts', () => { + + class TestItemList extends IgtItemList { + _itemList = new Map(); + + constructor(items: AbstractItem[]) { + super('item-list'); + items.forEach(item => { + this._itemList.set(item.id, item); + }) + } + + getItem(id: ItemId): AbstractItem { + return this._itemList.get(id) as AbstractItem; + } + } + + let inventory: IgtInventory; + const aSingleItem = (id: string) => new ItemAmount(id, 1); + const fiveItems = (id: string) => new ItemAmount(id, 5); + const itemWithHigherStack = (id: string) => new ExampleItem(id, 6); + let testGame: DummyGame; + + beforeEach(() => { + inventory = new IgtInventory(1); + testGame = new DummyGame({ + itemList: new TestItemList([item1]), + inventory: inventory + }); + testGame.initialize(); + }) + + test('returns true if there are enough items', () => { + // Act + inventory.gainItem(item1, 4); + const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id)]); + + // Assert + expect(canTake).toBe(true); + }); + + test('returns false if there are not enough items', () => { + // Act + inventory.gainItem(item1, 4); + const canTake = inventory.canTakeItemAmounts([fiveItems(item1.id)]); + + // Assert + expect(canTake).toBe(false); + }); + + test('returns true if there are exactly enough items', () => { + // Act + inventory.gainItem(item1, 5); + const canTake = inventory.canTakeItemAmounts([fiveItems(item1.id)]); + + // Assert + expect(canTake).toBe(true); + }); + + test('returns false if there are no items to start with', () => { + expect(inventory.isEmpty()).toBe(true); + + // Act + const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id)]); + + // Assert + expect(canTake).toBe(false); + }); + + test('returns false if there are no items to take passed in', () => { + // Act + inventory.gainItem(item1, 4); + const emptyItemAmounts: ItemAmount[] = []; + const canTake = inventory.canTakeItemAmounts(emptyItemAmounts); + + // Assert + expect(canTake).toBe(false); + }) + + describe('handles multiple items', () => { + + beforeEach(() => { + inventory = new IgtInventory(2); + testGame = new DummyGame({ + itemList: new TestItemList([item1]), + inventory: inventory + }); + testGame.initialize(); + }); + + test('that are the same, returning true if there are enough items', () => { + // Act + inventory.gainItem(item1, 4); + const item = aSingleItem(item1.id); + const canTake = inventory.canTakeItemAmounts([item, item]); + + // Assert + expect(canTake).toBe(true); + }); + + test('that are the same, returning false if there are not enough items', () => { + // Act + inventory.gainItem(item1, 4); + const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id), fiveItems(item1.id)]); + + // Assert + expect(canTake).toBe(false); + }); + + test('that are the same, returning true if there are exactly enough items', () => { + // Act + const id = item1.id; + inventory.gainItem(itemWithHigherStack(id), 6); + const canTake = inventory.canTakeItemAmounts([aSingleItem(id), fiveItems(id)]); + + // Assert + expect(canTake).toBe(true); + }); + + test('that are different, returning true if there are enough items', () => { + // Act + inventory.gainItem(item1, 4); + inventory.gainItem(item2, 2); + const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id), aSingleItem(item2.id)]); + + // Assert + expect(canTake).toBe(true); + }); + + test('that are different, returning false if there are not enough items', () => { + // Act + inventory.gainItem(item1, 4); + inventory.gainItem(item2, 2); + const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id), fiveItems(item2.id)]); + + // Assert + expect(canTake).toBe(false); + }); + + test('that are different, returning true if there are exactly enough items', () => { + // Act + inventory.gainItem(item1, 5); + inventory.gainItem(item2, 2); + const item = aSingleItem(item2.id); + const canTake = inventory.canTakeItemAmounts([fiveItems(item1.id), item, item]); + + // Assert + expect(canTake).toBe(true); + }); + }); + }); }) diff --git a/tsconfig.json b/tsconfig.json index 28022db..39ec48e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,8 @@ "esModuleInterop": true, "sourceMap": true, "paths": { - "@/*": ["src/*"] + "@/*": ["src/*"], + "@tests/*": ["tests/*"], }, "plugins": [ From 4e35ad0c040885fad89ccec33ad973366ba620ee Mon Sep 17 00:00:00 2001 From: Serene Aronow Date: Sat, 17 Feb 2024 15:51:28 -0500 Subject: [PATCH 2/2] Ran linting --- .../features/inventory/IgtInventory.ts | 42 ++- tests/smoke/Game.spec.ts | 2 +- .../features/inventory/Inventory.spec.ts | 319 +++++++++--------- tsconfig.json | 2 +- 4 files changed, 180 insertions(+), 185 deletions(-) diff --git a/src/ig-template/features/inventory/IgtInventory.ts b/src/ig-template/features/inventory/IgtInventory.ts index c287e3b..7d044b8 100644 --- a/src/ig-template/features/inventory/IgtInventory.ts +++ b/src/ig-template/features/inventory/IgtInventory.ts @@ -1,5 +1,3 @@ -import { cloneDeep } from 'lodash-es'; - import { InventorySlot } from '@/ig-template/features/inventory/InventorySlot'; import { ItemId } from '@/ig-template/features/items/ItemId'; import { IgtFeature } from '@/ig-template/features/IgtFeature'; @@ -177,28 +175,28 @@ export class IgtInventory extends IgtFeature { return total; } - /** - * Returns if the inventory can lose the given amounts of items - */ - canTakeItemAmounts(itemAmounts: ItemAmount[]): boolean { - if (!itemAmounts.length) { - return false - } - - const mergedItemAmounts: Record = {}; - for (const itemAmount of itemAmounts) { - const { id, amount } = itemAmount; - const currentTotalAmount = (mergedItemAmounts[id] || 0) + amount; - mergedItemAmounts[id] = currentTotalAmount; - - const itemsInInventory = this.getTotalAmount(id); - if (itemsInInventory < currentTotalAmount) { - return false; - } - } - return true; + /** + * Returns if the inventory can lose the given amounts of items + */ + canTakeItemAmounts(itemAmounts: ItemAmount[]): boolean { + if (!itemAmounts.length) { + return false; } + const mergedItemAmounts: Record = {}; + for (const itemAmount of itemAmounts) { + const { id, amount } = itemAmount; + const currentTotalAmount = (mergedItemAmounts[id] || 0) + amount; + mergedItemAmounts[id] = currentTotalAmount; + + const itemsInInventory = this.getTotalAmount(id); + if (itemsInInventory < currentTotalAmount) { + return false; + } + } + return true; + } + hasItemAmounts(amounts: ItemAmount[]): boolean { for (const amount of amounts) { if (!this.hasItemAmount(amount)) { diff --git a/tests/smoke/Game.spec.ts b/tests/smoke/Game.spec.ts index c054748..90ce28f 100644 --- a/tests/smoke/Game.spec.ts +++ b/tests/smoke/Game.spec.ts @@ -11,7 +11,7 @@ import { IgtAchievements } from '@/ig-template/features/achievements'; import { IgtFeatures } from '@/ig-template/IgtFeatures'; export class DummyGame extends IgtGame { - features: IgtFeatures; + features: IgtFeatures; constructor(features: IgtFeatures) { super(); diff --git a/tests/unit/ig-template/features/inventory/Inventory.spec.ts b/tests/unit/ig-template/features/inventory/Inventory.spec.ts index 8794e57..0c2ce35 100644 --- a/tests/unit/ig-template/features/inventory/Inventory.spec.ts +++ b/tests/unit/ig-template/features/inventory/Inventory.spec.ts @@ -1,15 +1,14 @@ import { beforeEach, describe, expect, test } from 'vitest'; -import { IgtInventory } from "@/ig-template/features/inventory/IgtInventory"; -import {AbstractItem} from "@/ig-template/features/items/AbstractItem"; -import {ItemId} from "@/ig-template/features/items/ItemId"; -import {ItemType} from "@/ig-template/features/items/ItemType"; -import {AbstractConsumable} from "@/ig-template/features/items/AbstractConsumable"; -import {InventorySlot} from "@/ig-template/features/inventory/InventorySlot"; -import { ItemAmount } from "@/ig-template/features/items/ItemAmount"; -import { DummyGame } from "@tests/smoke/Game.spec"; -import { IgtItemList } from "@/ig-template/features/items/IgtItemList"; - +import { IgtInventory } from '@/ig-template/features/inventory/IgtInventory'; +import { AbstractItem } from '@/ig-template/features/items/AbstractItem'; +import { ItemId } from '@/ig-template/features/items/ItemId'; +import { ItemType } from '@/ig-template/features/items/ItemType'; +import { AbstractConsumable } from '@/ig-template/features/items/AbstractConsumable'; +import { InventorySlot } from '@/ig-template/features/inventory/InventorySlot'; +import { ItemAmount } from '@/ig-template/features/items/ItemAmount'; +import { DummyGame } from '@tests/smoke/Game.spec'; +import { IgtItemList } from '@/ig-template/features/items/IgtItemList'; export class ExampleItem extends AbstractItem { constructor(id: ItemId, maxStack: number) { @@ -214,162 +213,160 @@ describe('Inventory', () => { const inventory: IgtInventory = new IgtInventory(1); inventory.gainItem(consumable); - // Act - inventory.consumeItem(0); - // Assert - expect(inventory.isEmpty()).toBe(true); - expect(consumable.isConsumed).toBe(true); - }); + // Act + inventory.consumeItem(0); + // Assert + expect(inventory.isEmpty()).toBe(true); + expect(consumable.isConsumed).toBe(true); + }); - describe('canTakeItemAmounts', () => { - - class TestItemList extends IgtItemList { - _itemList = new Map(); - - constructor(items: AbstractItem[]) { - super('item-list'); - items.forEach(item => { - this._itemList.set(item.id, item); - }) - } - - getItem(id: ItemId): AbstractItem { - return this._itemList.get(id) as AbstractItem; - } - } - - let inventory: IgtInventory; - const aSingleItem = (id: string) => new ItemAmount(id, 1); - const fiveItems = (id: string) => new ItemAmount(id, 5); - const itemWithHigherStack = (id: string) => new ExampleItem(id, 6); - let testGame: DummyGame; - - beforeEach(() => { - inventory = new IgtInventory(1); - testGame = new DummyGame({ - itemList: new TestItemList([item1]), - inventory: inventory - }); - testGame.initialize(); - }) - - test('returns true if there are enough items', () => { - // Act - inventory.gainItem(item1, 4); - const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id)]); - - // Assert - expect(canTake).toBe(true); - }); - - test('returns false if there are not enough items', () => { - // Act - inventory.gainItem(item1, 4); - const canTake = inventory.canTakeItemAmounts([fiveItems(item1.id)]); - - // Assert - expect(canTake).toBe(false); - }); + describe('canTakeItemAmounts', () => { + class TestItemList extends IgtItemList { + _itemList = new Map(); - test('returns true if there are exactly enough items', () => { - // Act - inventory.gainItem(item1, 5); - const canTake = inventory.canTakeItemAmounts([fiveItems(item1.id)]); - - // Assert - expect(canTake).toBe(true); + constructor(items: AbstractItem[]) { + super('item-list'); + items.forEach((item) => { + this._itemList.set(item.id, item); }); + } + + getItem(id: ItemId): AbstractItem { + return this._itemList.get(id) as AbstractItem; + } + } + + let inventory: IgtInventory; + const aSingleItem = (id: string) => new ItemAmount(id, 1); + const fiveItems = (id: string) => new ItemAmount(id, 5); + const itemWithHigherStack = (id: string) => new ExampleItem(id, 6); + let testGame: DummyGame; + + beforeEach(() => { + inventory = new IgtInventory(1); + testGame = new DummyGame({ + itemList: new TestItemList([item1]), + inventory: inventory, + }); + testGame.initialize(); + }); - test('returns false if there are no items to start with', () => { - expect(inventory.isEmpty()).toBe(true); + test('returns true if there are enough items', () => { + // Act + inventory.gainItem(item1, 4); + const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id)]); - // Act - const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id)]); - - // Assert - expect(canTake).toBe(false); - }); + // Assert + expect(canTake).toBe(true); + }); + + test('returns false if there are not enough items', () => { + // Act + inventory.gainItem(item1, 4); + const canTake = inventory.canTakeItemAmounts([fiveItems(item1.id)]); + + // Assert + expect(canTake).toBe(false); + }); + + test('returns true if there are exactly enough items', () => { + // Act + inventory.gainItem(item1, 5); + const canTake = inventory.canTakeItemAmounts([fiveItems(item1.id)]); + + // Assert + expect(canTake).toBe(true); + }); + + test('returns false if there are no items to start with', () => { + expect(inventory.isEmpty()).toBe(true); + + // Act + const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id)]); + + // Assert + expect(canTake).toBe(false); + }); + + test('returns false if there are no items to take passed in', () => { + // Act + inventory.gainItem(item1, 4); + const emptyItemAmounts: ItemAmount[] = []; + const canTake = inventory.canTakeItemAmounts(emptyItemAmounts); + + // Assert + expect(canTake).toBe(false); + }); - test('returns false if there are no items to take passed in', () => { - // Act - inventory.gainItem(item1, 4); - const emptyItemAmounts: ItemAmount[] = []; - const canTake = inventory.canTakeItemAmounts(emptyItemAmounts); - - // Assert - expect(canTake).toBe(false); - }) - - describe('handles multiple items', () => { - - beforeEach(() => { - inventory = new IgtInventory(2); - testGame = new DummyGame({ - itemList: new TestItemList([item1]), - inventory: inventory - }); - testGame.initialize(); - }); - - test('that are the same, returning true if there are enough items', () => { - // Act - inventory.gainItem(item1, 4); - const item = aSingleItem(item1.id); - const canTake = inventory.canTakeItemAmounts([item, item]); - - // Assert - expect(canTake).toBe(true); - }); - - test('that are the same, returning false if there are not enough items', () => { - // Act - inventory.gainItem(item1, 4); - const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id), fiveItems(item1.id)]); - - // Assert - expect(canTake).toBe(false); - }); - - test('that are the same, returning true if there are exactly enough items', () => { - // Act - const id = item1.id; - inventory.gainItem(itemWithHigherStack(id), 6); - const canTake = inventory.canTakeItemAmounts([aSingleItem(id), fiveItems(id)]); - - // Assert - expect(canTake).toBe(true); - }); - - test('that are different, returning true if there are enough items', () => { - // Act - inventory.gainItem(item1, 4); - inventory.gainItem(item2, 2); - const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id), aSingleItem(item2.id)]); - - // Assert - expect(canTake).toBe(true); - }); - - test('that are different, returning false if there are not enough items', () => { - // Act - inventory.gainItem(item1, 4); - inventory.gainItem(item2, 2); - const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id), fiveItems(item2.id)]); - - // Assert - expect(canTake).toBe(false); - }); - - test('that are different, returning true if there are exactly enough items', () => { - // Act - inventory.gainItem(item1, 5); - inventory.gainItem(item2, 2); - const item = aSingleItem(item2.id); - const canTake = inventory.canTakeItemAmounts([fiveItems(item1.id), item, item]); - - // Assert - expect(canTake).toBe(true); - }); + describe('handles multiple items', () => { + beforeEach(() => { + inventory = new IgtInventory(2); + testGame = new DummyGame({ + itemList: new TestItemList([item1]), + inventory: inventory, }); + testGame.initialize(); + }); + + test('that are the same, returning true if there are enough items', () => { + // Act + inventory.gainItem(item1, 4); + const item = aSingleItem(item1.id); + const canTake = inventory.canTakeItemAmounts([item, item]); + + // Assert + expect(canTake).toBe(true); + }); + + test('that are the same, returning false if there are not enough items', () => { + // Act + inventory.gainItem(item1, 4); + const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id), fiveItems(item1.id)]); + + // Assert + expect(canTake).toBe(false); + }); + + test('that are the same, returning true if there are exactly enough items', () => { + // Act + const id = item1.id; + inventory.gainItem(itemWithHigherStack(id), 6); + const canTake = inventory.canTakeItemAmounts([aSingleItem(id), fiveItems(id)]); + + // Assert + expect(canTake).toBe(true); + }); + + test('that are different, returning true if there are enough items', () => { + // Act + inventory.gainItem(item1, 4); + inventory.gainItem(item2, 2); + const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id), aSingleItem(item2.id)]); + + // Assert + expect(canTake).toBe(true); + }); + + test('that are different, returning false if there are not enough items', () => { + // Act + inventory.gainItem(item1, 4); + inventory.gainItem(item2, 2); + const canTake = inventory.canTakeItemAmounts([aSingleItem(item1.id), fiveItems(item2.id)]); + + // Assert + expect(canTake).toBe(false); + }); + + test('that are different, returning true if there are exactly enough items', () => { + // Act + inventory.gainItem(item1, 5); + inventory.gainItem(item2, 2); + const item = aSingleItem(item2.id); + const canTake = inventory.canTakeItemAmounts([fiveItems(item1.id), item, item]); + + // Assert + expect(canTake).toBe(true); + }); }); -}) + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 548737d..5d6155b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,7 @@ "skipLibCheck": true, "paths": { "@/*": ["./src/*"], - "@tests/*": ["./tests/*"], + "@tests/*": ["./tests/*"] }, /* Bundler mode */