test: fix unit tests for WebExtension build

This commit is contained in:
xenticore
2025-05-01 18:26:05 -04:00
parent 745087a1cb
commit 91f43295e8
3 changed files with 38 additions and 30 deletions

View File

@@ -1,4 +1,4 @@
import { describe, expect, test, mock } from "bun:test";
import { describe, expect, test, mock, beforeEach, jest } from "bun:test";
import { ItemSchema, ItemSlot, getItemIndexByName, getTradableStatusByDefindex, getTradableStatusByName, linkBotkillerVariants, linkFestiveVariants, prepareSchema } from '../src/content/schemaService'
// Mock the storage and log functions
@@ -13,6 +13,24 @@ mock.module('../src/content/utils/log', () => ({
logError: mock(() => {})
}));
const mockSchemaResponse = [
{
defindex: 1,
item_name: 'Test Item',
item_slot: 'misc',
attributes: [
{ "name": "cannot trade", "class": "cannot_trade", "value": 1 }
],
capabilities: {}
},
{
defindex: 208,
item_name: 'Flame Thrower',
item_slot: 'primary',
capabilities: { can_killstreakify: true }
}
]
const mockSchema: ItemSchema = {
'21': {
name: 'Flame Thrower',
@@ -89,6 +107,7 @@ const mockSchema: ItemSchema = {
}
describe('Schema Service', () => {
test('getItemIndexByName returns correct defindex', () => {
expect(getItemIndexByName(mockSchema, 'Flame Thrower')).toBe(208)
expect(getItemIndexByName(mockSchema, 'Mann Co. Supply Crate Key')).toBe(5021)
@@ -113,25 +132,12 @@ describe('Schema Service', () => {
// Mock GM_fetch response
const mockResponse = {
ok: true,
json: async () => [
{
defindex: 1,
item_name: 'Test Item',
item_slot: 'misc',
attributes: [
{ "name": "cannot trade", "class": "cannot_trade", "value": 1 }
],
capabilities: {}
},
{
defindex: 208,
item_name: 'Flame Thrower',
item_slot: 'primary',
capabilities: { can_killstreakify: true }
}
]
json: async () => mockSchemaResponse
};
// Mock Chrome runtime message
(chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => mockSchemaResponse);
// Mock GM_fetch
globalThis.GM_fetch = mock(async () => mockResponse);