Skip to content

Commit

Permalink
feat: added tests to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pentreathm committed Oct 17, 2024
1 parent 56a2182 commit 7ead75a
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions test/scene-fetcher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { createSceneFetcherComponent } from '../src/adapters/scene-fetcher'
import { Permissions } from '../src/types'
import { LRUCache } from 'lru-cache'

describe('SceneFetcherComponent', () => {
const mockConfig = {
requireString: jest.fn(),
getString: jest.fn(),
getNumber: jest.fn(),
requireNumber: jest.fn()
}
const mockFetch = {
fetch: jest.fn()
}
const mockLogs = {
getLogger: jest.fn().mockReturnValue({
log: jest.fn(),
warn: jest.fn()
})
}

beforeEach(() => {
jest.clearAllMocks()
})


it('should return undefined if the world sceneId is not found', async () => {
mockFetch.fetch.mockResolvedValueOnce({ json: async () => ({ configurations: { scenesUrn: [] } }) })
const sceneFetcher = await createSceneFetcherComponent({ config: mockConfig, fetch: mockFetch, logs: mockLogs })
const permissions = await sceneFetcher.fetchWorldPermissions('missing-world')

expect(permissions).toBeUndefined()
})


it('should return default permissions on fetch error', async () => {
mockFetch.fetch.mockRejectedValueOnce(new Error('Network error'))
const sceneFetcher = await createSceneFetcherComponent({ config: mockConfig, fetch: mockFetch, logs: mockLogs })
const permissions = await sceneFetcher.fetchScenePermissions('scene-123')
expect(permissions).toEqual({ cast: [], mute: [] })
expect(mockLogs.getLogger().warn).toHaveBeenCalled()
})

it('should use LRU cache for fetching scene IDs', async () => {
const cacheSpy = jest.spyOn(LRUCache.prototype, 'fetch')

// Mock responses for cache logic
mockFetch.fetch.mockResolvedValueOnce({
json: async () => ({
configurations: { scenesUrn: ['urn:decentraland:scene:world:cached-scene'] }
})
})

const sceneFetcher = await createSceneFetcherComponent({ config: mockConfig, fetch: mockFetch, logs: mockLogs })
await sceneFetcher.fetchWorldPermissions('cached-world')

expect(cacheSpy).toHaveBeenCalledWith('cached-world')
})
})

0 comments on commit 7ead75a

Please sign in to comment.