test: World Servers utils

This commit is contained in:
Kevin Szuchet 2023-08-09 10:45:23 +02:00
parent 30c23dc32e
commit afe5a31fa9
No known key found for this signature in database
GPG Key ID: 1E083BC33700E594
4 changed files with 120 additions and 9 deletions

View File

@ -1,9 +0,0 @@
const KEY = 'previously-loaded-servers'
export const getPreviouslyLoadedServers = () => Array.from(new Set(localStorage.getItem(KEY)?.split(','))) || null
export const addServerToPreviouslyLoaded = (server: string) => {
const previouslyLoadedServers = getPreviouslyLoadedServers() || []
previouslyLoadedServers.push(server)
localStorage.setItem(KEY, previouslyLoadedServers.join(','))
}

View File

@ -0,0 +1 @@
export * from './worldServers'

View File

@ -0,0 +1,108 @@
import { PREVIOUSLY_LOADED_SERVERS_KEY, addServerToPreviouslyLoaded, getPreviouslyLoadedServers } from './worldServers'
describe('when getting the previously loaded servers from the local storage', () => {
describe('and the key is not yet set', () => {
beforeEach(() => {
jest.spyOn(Storage.prototype, 'getItem').mockReturnValueOnce(null)
})
it('should return an empty array', () => {
expect(getPreviouslyLoadedServers()).toStrictEqual([])
})
})
describe('and the value is empty', () => {
beforeEach(() => {
jest.spyOn(Storage.prototype, 'getItem').mockReturnValueOnce('')
})
it('should return an empty array', () => {
expect(getPreviouslyLoadedServers()).toStrictEqual([])
})
})
describe('and the value has one previously loaded server', () => {
let previouslyLoadedServer: string
beforeEach(() => {
previouslyLoadedServer = 'previous-server.dcl.eth'
jest.spyOn(Storage.prototype, 'getItem').mockReturnValueOnce(previouslyLoadedServer)
})
it('should return an array with the previously loaded server', () => {
expect(getPreviouslyLoadedServers()).toStrictEqual([previouslyLoadedServer])
})
})
describe('and the value has multiple previously loaded server', () => {
let previouslyLoadedServers: string[]
describe('and all the servers are different', () => {
beforeEach(() => {
previouslyLoadedServers = ['previous-server.dcl.eth', 'another-server.dcl.eth', 'and-another-server.dcl.eth']
jest.spyOn(Storage.prototype, 'getItem').mockReturnValueOnce(previouslyLoadedServers.join(','))
})
it('should return an array with all those different servers', () => {
expect(getPreviouslyLoadedServers()).toStrictEqual(previouslyLoadedServers)
})
})
describe('and there are some duplicates in the local storage value', () => {
beforeEach(() => {
previouslyLoadedServers = [
'previous-server.dcl.eth',
'another-server.dcl.eth',
'another-server.dcl.eth',
'and-another-server.dcl.eth',
'and-another-server.dcl.eth',
'and-another-server.dcl.eth'
]
jest.spyOn(Storage.prototype, 'getItem').mockReturnValueOnce(previouslyLoadedServers.join(','))
})
it('should return an array with only one occurrence of each server', () => {
expect(getPreviouslyLoadedServers()).toStrictEqual([
'previous-server.dcl.eth',
'another-server.dcl.eth',
'and-another-server.dcl.eth'
])
})
})
})
})
describe('when adding a new server to the same key in the local storage', () => {
let newServer: string
beforeEach(() => {
newServer = 'new-server.dcl.eth'
jest.spyOn(Storage.prototype, 'setItem').mockImplementationOnce(jest.fn())
})
describe('and there was not previous loaded servers in the local storage', () => {
beforeEach(() => {
jest.spyOn(Storage.prototype, 'getItem').mockReturnValueOnce(null)
addServerToPreviouslyLoaded(newServer)
})
it('should set in the local storage only the new server', () => {
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(localStorage.setItem).toBeCalledWith(PREVIOUSLY_LOADED_SERVERS_KEY, newServer)
})
})
describe('and there were some previous loaded servers in the local storage', () => {
const previouslyLoadedServers = ['previous-server.dcl.eth', 'another-server.dcl.eth', 'and-another-server.dcl.eth']
beforeEach(() => {
jest.spyOn(Storage.prototype, 'getItem').mockReturnValueOnce(previouslyLoadedServers.join(','))
addServerToPreviouslyLoaded(newServer)
})
it('should set in the local storage only the new server', () => {
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(localStorage.setItem).toBeCalledWith(PREVIOUSLY_LOADED_SERVERS_KEY, [...previouslyLoadedServers, newServer].join(','))
})
})
})

View File

@ -0,0 +1,11 @@
export const PREVIOUSLY_LOADED_SERVERS_KEY = 'previously-loaded-servers'
export const getPreviouslyLoadedServers = () => {
return Array.from(new Set(localStorage.getItem(PREVIOUSLY_LOADED_SERVERS_KEY)?.split(',').filter(Boolean)))
}
export const addServerToPreviouslyLoaded = (server: string) => {
const previouslyLoadedServers = getPreviouslyLoadedServers() || []
previouslyLoadedServers.push(server)
localStorage.setItem(PREVIOUSLY_LOADED_SERVERS_KEY, previouslyLoadedServers.join(','))
}