test: World Servers utils
This commit is contained in:
parent
30c23dc32e
commit
afe5a31fa9
@ -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(','))
|
||||
}
|
||||
1
src/utils/worldServers/index.ts
Normal file
1
src/utils/worldServers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './worldServers'
|
||||
108
src/utils/worldServers/worldServers.spec.ts
Normal file
108
src/utils/worldServers/worldServers.spec.ts
Normal 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(','))
|
||||
})
|
||||
})
|
||||
})
|
||||
11
src/utils/worldServers/worldServers.ts
Normal file
11
src/utils/worldServers/worldServers.ts
Normal 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(','))
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user