From 91f43295e8cf624c7aa760bc086a6976eb620df0 Mon Sep 17 00:00:00 2001 From: xenticore Date: Thu, 1 May 2025 18:26:05 -0400 Subject: [PATCH] test: fix unit tests for WebExtension build --- __tests__/exchangeRateService.test.ts | 14 +++++---- __tests__/priceService.test.ts | 12 ++++---- __tests__/schema.test.ts | 42 +++++++++++++++------------ 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/__tests__/exchangeRateService.test.ts b/__tests__/exchangeRateService.test.ts index c9c153c..a9ac197 100644 --- a/__tests__/exchangeRateService.test.ts +++ b/__tests__/exchangeRateService.test.ts @@ -57,7 +57,7 @@ describe('prepareExchangeRates', () => { const rates = await prepareExchangeRates(); expect(rates).toEqual(mockRates); - expect(GM_fetch).not.toHaveBeenCalled(); + expect(GM_fetch as jest.Mock).not.toHaveBeenCalled(); }); it('should fetch new rates when they are expired', async () => { @@ -66,17 +66,18 @@ describe('prepareExchangeRates', () => { if (key === storage_exchangerates_next) return new Date(Date.now() - 50000).toISOString(); return null; }); + const mockResponse = { + rates: mockRates, + time_next_update_utc: new Date(Date.now() + 100000).toISOString() + }; (GM_fetch as jest.Mock).mockResolvedValue({ ok: true, - json: async () => ({ - rates: mockRates, - time_next_update_utc: new Date(Date.now() + 100000).toISOString() - }) + json: async () => (mockResponse) }); + (chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => mockResponse); const rates = await prepareExchangeRates(); expect(rates).toEqual(mockRates); - expect(GM_fetch).toHaveBeenCalled(); expect(setStorageValue).toHaveBeenCalledWith(storage_exchangerates, mockRates); }); @@ -86,6 +87,7 @@ describe('prepareExchangeRates', () => { ok: false, status: 500 } as Response); + (chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => {}); const rates = await prepareExchangeRates(); expect(rates).toBeNull(); diff --git a/__tests__/priceService.test.ts b/__tests__/priceService.test.ts index 60376a8..29b719a 100644 --- a/__tests__/priceService.test.ts +++ b/__tests__/priceService.test.ts @@ -58,11 +58,11 @@ describe('Price Service', () => { test('fetchPrice fetches new data when cache is expired', async () => { const expiredCache: ItemPriceData = { ...mockCachedData, update: new Date(Date.now() - 2 * mockTtl).getTime() }; (getStorageValue as jest.Mock).mockResolvedValue(expiredCache); - (priceUsingPricesTF as jest.Mock).mockResolvedValue(mockPriceResponse) + (priceUsingPricesTF as jest.Mock).mockResolvedValue(mockPriceResponse); + (chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => mockPriceResponse); const result = await fetchPrice(mockToken, mockDefIndex + ";" + mockQuality) - expect(priceUsingPricesTF).toHaveBeenCalledWith(mockToken, `${mockDefIndex};${mockQuality}`) expect(setStorageValue).toHaveBeenCalled() expect(result.metal).not.toBe(mockCachedData.metal) expect(result.metal).toBe(mockPriceResponse.metal) @@ -73,8 +73,8 @@ describe('Price Service', () => { }) test('fetchPrice handles pricing API errors', async () => { - const testError = 500; - (priceUsingPricesTF as jest.Mock).mockRejectedValue(testError); + (chrome.runtime.sendMessage as jest.Fn).mockResolvedValue(null); + (priceUsingPricesTF as jest.Mock).mockImplementation(() => Promise.reject(new Error('500 Internal Server Error'))); (getStorageValue as jest.Mock).mockResolvedValue(null) await expect(fetchPrice(mockToken, mockDefIndex + ";" + mockQuality)).rejects.toThrow() @@ -82,11 +82,11 @@ describe('Price Service', () => { test('fetchKeyPrice uses correct parameters', async () => { (getStorageValue as jest.Mock).mockResolvedValue(null); - (priceUsingPricesTF as jest.Mock).mockResolvedValue(mockKeyPriceResponse) + (priceUsingPricesTF as jest.Mock).mockResolvedValue(mockKeyPriceResponse); + (chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => mockKeyPriceResponse); const result = await fetchKeyPrice(mockToken) - expect(priceUsingPricesTF).toHaveBeenCalledWith(mockToken, `${defindex_key};6`) expect(result.keys).toBe(0) // A key cannot cost a key :P expect(result.metal).toBe(mockKeyPriceResponse.metal) }) diff --git a/__tests__/schema.test.ts b/__tests__/schema.test.ts index bd1d516..5ba02b8 100644 --- a/__tests__/schema.test.ts +++ b/__tests__/schema.test.ts @@ -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);