You've already forked tf2wikipricing
test: fix unit tests for WebExtension build
This commit is contained in:
@@ -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;
|
||||
});
|
||||
(GM_fetch as jest.Mock).mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
const mockResponse = {
|
||||
rates: mockRates,
|
||||
time_next_update_utc: new Date(Date.now() + 100000).toISOString()
|
||||
})
|
||||
};
|
||||
(GM_fetch as jest.Mock).mockResolvedValue({
|
||||
ok: true,
|
||||
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();
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user