diff --git a/src/js/random-quote.js b/src/js/random-quote.js deleted file mode 100644 index ed01ab8..0000000 --- a/src/js/random-quote.js +++ /dev/null @@ -1,25 +0,0 @@ -let quoteCache = null; - -/** - * Fetch a random quote from our API. - * @returns {Promise<*>} A promise that resolves with a random quote - */ -async function fetchRandomQuote() { - if (!quoteCache) { - const response = await fetch('https://app.lizardbyte.dev/uno/random-quotes/games.json'); - quoteCache = await response.json(); - } - return quoteCache[Math.floor(Math.random() * quoteCache.length)]; -} - -/** - * Reset the quote cache. - */ -function resetQuoteCache() { - quoteCache = null; -} - -module.exports = { - fetchRandomQuote, - resetQuoteCache, -}; diff --git a/tests/random-quote.test.js b/tests/random-quote.test.js deleted file mode 100644 index 8dd9c4c..0000000 --- a/tests/random-quote.test.js +++ /dev/null @@ -1,77 +0,0 @@ -import { - afterEach, - beforeEach, - describe, - expect, - it, - jest, -} from '@jest/globals'; - -const { fetchRandomQuote, resetQuoteCache } = require('../src/js/random-quote'); - -describe('fetchRandomQuote', () => { - let originalFetch; - - beforeEach(() => { - // Save the original fetch function - originalFetch = globalThis.fetch; - // Clear the quote cache before each test - jest.resetModules(); - // Reset the quotesCache - resetQuoteCache(); - }); - - afterEach(() => { - // Restore the original fetch function - globalThis.fetch = originalFetch; - }); - - it('should fetch and return a random quote', async () => { - const mockQuotes = [ - { quote: 'Quote 1' }, - { quote: 'Quote 2' }, - { quote: 'Quote 3' } - ]; - - globalThis.fetch = jest.fn(() => - Promise.resolve({ - json: () => Promise.resolve(mockQuotes) - }) - ); - - const quote = await fetchRandomQuote(); - expect(mockQuotes).toContainEqual(quote); - expect(globalThis.fetch).toHaveBeenCalledTimes(1); - }); - - it('should return a cached quote if already fetched', async () => { - const mockQuotes = [ - { quote: 'Quote 1' }, - { quote: 'Quote 2' }, - { quote: 'Quote 3' } - ]; - - globalThis.fetch = jest.fn(() => - Promise.resolve({ - json: () => Promise.resolve(mockQuotes) - }) - ); - - // First call to populate the cache - await fetchRandomQuote(); - // Second call should use the cache - const quote = await fetchRandomQuote(); - - expect(mockQuotes).toContainEqual(quote); - expect(globalThis.fetch).toHaveBeenCalledTimes(1); - }); - - it('should handle fetch errors gracefully', async () => { - globalThis.fetch = jest.fn(() => - Promise.reject(new Error('Failed to fetch')) - ); - - await expect(fetchRandomQuote()).rejects.toThrow('Failed to fetch'); - expect(globalThis.fetch).toHaveBeenCalledTimes(1); - }); -}); diff --git a/webpack.config.js b/webpack.config.js index 8b5397c..c78f312 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -15,7 +15,6 @@ let config = { 'levenshtein-distance': './src/js/levenshtein-distance', 'lizardbyte-css': './src/js/lizardbyte-css', 'load-script': './src/js/load-script', - 'random-quote': './src/js/random-quote', 'ranking-sorter': './src/js/ranking-sorter', 'sleep': './src/js/sleep', },