You've already forked tf2wikipricing
test: update tests for new pricing service
This commit is contained in:
@@ -1,22 +1,21 @@
|
|||||||
import { describe, expect, test, jest, mock, beforeEach } from "bun:test";
|
import { describe, expect, test, jest, mock, beforeEach } from "bun:test";
|
||||||
import { ItemPriceData, fetchPrice, fetchKeyPrice } from '../src/content/priceService'
|
import { ItemPriceData, fetchPrice, fetchKeyPrice } from '../src/content/priceService'
|
||||||
import { PricesResponse, priceUsingPricesTF } from '../src/content/pricing/pricestf'
|
import { PricesResponse, priceUsingPricedb } from '../src/content/pricing/pricedb'
|
||||||
import { getStorageValue, setStorageValue } from '../src/content/storage'
|
import { getStorageValue, setStorageValue } from '../src/content/storage'
|
||||||
import { defindex_key } from "../src/content/config";
|
import { defindex_key } from "../src/content/config";
|
||||||
|
|
||||||
// Mock the storage module
|
// Mock storage module
|
||||||
mock.module('../src/content/storage', () => ({
|
mock.module('../src/content/storage', () => ({
|
||||||
getStorageValue: jest.fn(),
|
getStorageValue: jest.fn(),
|
||||||
setStorageValue: jest.fn()
|
setStorageValue: jest.fn()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// Mock the pricing module
|
// Mock pricing module
|
||||||
mock.module('../src/content/pricing/pricestf', () => ({
|
mock.module('../src/content/pricing/pricedb', () => ({
|
||||||
priceUsingPricesTF: jest.fn()
|
priceUsingPricedb: jest.fn()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
describe('Price Service', () => {
|
describe('Price Service', () => {
|
||||||
const mockToken = 'test-token'
|
|
||||||
const mockDefIndex = 105 // Brigade Helm
|
const mockDefIndex = 105 // Brigade Helm
|
||||||
const mockQuality = 11 // Strange
|
const mockQuality = 11 // Strange
|
||||||
const mockSku = `${mockDefIndex};${mockQuality}`
|
const mockSku = `${mockDefIndex};${mockQuality}`
|
||||||
@@ -49,7 +48,7 @@ describe('Price Service', () => {
|
|||||||
test('fetchPrice returns cached data if available and not expired', async () => {
|
test('fetchPrice returns cached data if available and not expired', async () => {
|
||||||
(getStorageValue as jest.Mock).mockResolvedValue(mockCachedData)
|
(getStorageValue as jest.Mock).mockResolvedValue(mockCachedData)
|
||||||
|
|
||||||
const result = await fetchPrice(mockToken, mockDefIndex + ";" + mockQuality)
|
const result = await fetchPrice(mockDefIndex + ";" + mockQuality)
|
||||||
|
|
||||||
expect(getStorageValue).toHaveBeenCalledWith(expect.stringContaining(mockSku), null)
|
expect(getStorageValue).toHaveBeenCalledWith(expect.stringContaining(mockSku), null)
|
||||||
expect(result).toEqual(mockCachedData)
|
expect(result).toEqual(mockCachedData)
|
||||||
@@ -58,34 +57,30 @@ describe('Price Service', () => {
|
|||||||
test('fetchPrice fetches new data when cache is expired', async () => {
|
test('fetchPrice fetches new data when cache is expired', async () => {
|
||||||
const expiredCache: ItemPriceData = { ...mockCachedData, update: new Date(Date.now() - 2 * mockTtl).getTime() };
|
const expiredCache: ItemPriceData = { ...mockCachedData, update: new Date(Date.now() - 2 * mockTtl).getTime() };
|
||||||
(getStorageValue as jest.Mock).mockResolvedValue(expiredCache);
|
(getStorageValue as jest.Mock).mockResolvedValue(expiredCache);
|
||||||
(priceUsingPricesTF as jest.Mock).mockResolvedValue(mockPriceResponse);
|
(priceUsingPricedb as jest.Mock).mockResolvedValue(mockPriceResponse);
|
||||||
(chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => mockPriceResponse);
|
(chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => mockPriceResponse);
|
||||||
|
|
||||||
const result = await fetchPrice(mockToken, mockDefIndex + ";" + mockQuality)
|
const result = await fetchPrice(mockDefIndex + ";" + mockQuality)
|
||||||
|
|
||||||
expect(setStorageValue).toHaveBeenCalled()
|
expect(setStorageValue).toHaveBeenCalled()
|
||||||
expect(result.metal).not.toBe(mockCachedData.metal)
|
expect(result.metal).not.toBe(mockCachedData.metal)
|
||||||
expect(result.metal).toBe(mockPriceResponse.metal)
|
expect(result.metal).toBe(mockPriceResponse.metal)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('fetchPrice rejects with 401 when no token provided', async () => {
|
|
||||||
await expect(fetchPrice('', mockDefIndex + ";" + mockQuality)).rejects.toThrow()
|
|
||||||
})
|
|
||||||
|
|
||||||
test('fetchPrice handles pricing API errors', async () => {
|
test('fetchPrice handles pricing API errors', async () => {
|
||||||
(chrome.runtime.sendMessage as jest.Fn).mockResolvedValue(null);
|
(chrome.runtime.sendMessage as jest.Fn).mockResolvedValue(null);
|
||||||
(priceUsingPricesTF as jest.Mock).mockImplementation(() => Promise.reject(new Error('500 Internal Server Error')));
|
(priceUsingPricedb as jest.Mock).mockImplementation(() => Promise.reject(new Error('500 Internal Server Error')));
|
||||||
(getStorageValue as jest.Mock).mockResolvedValue(null)
|
(getStorageValue as jest.Mock).mockResolvedValue(null)
|
||||||
|
|
||||||
await expect(fetchPrice(mockToken, mockDefIndex + ";" + mockQuality)).rejects.toThrow()
|
await expect(fetchPrice(mockDefIndex + ";" + mockQuality)).rejects.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('fetchKeyPrice uses correct parameters', async () => {
|
test('fetchKeyPrice uses correct parameters', async () => {
|
||||||
(getStorageValue as jest.Mock).mockResolvedValue(null);
|
(getStorageValue as jest.Mock).mockResolvedValue(null);
|
||||||
(priceUsingPricesTF as jest.Mock).mockResolvedValue(mockKeyPriceResponse);
|
(priceUsingPricedb as jest.Mock).mockResolvedValue(mockKeyPriceResponse);
|
||||||
(chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => mockKeyPriceResponse);
|
(chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => mockKeyPriceResponse);
|
||||||
|
|
||||||
const result = await fetchKeyPrice(mockToken)
|
const result = await fetchKeyPrice()
|
||||||
|
|
||||||
expect(result.keys).toBe(0) // A key cannot cost a key :P
|
expect(result.keys).toBe(0) // A key cannot cost a key :P
|
||||||
expect(result.metal).toBe(mockKeyPriceResponse.metal)
|
expect(result.metal).toBe(mockKeyPriceResponse.metal)
|
||||||
|
|||||||
Reference in New Issue
Block a user