From afe5a31fa998e8ef4ea57249e4fe06c0d1acf11c Mon Sep 17 00:00:00 2001 From: Kevin Szuchet Date: Wed, 9 Aug 2023 10:45:23 +0200 Subject: [PATCH] test: World Servers utils --- src/utils/worldServers.ts | 9 -- src/utils/worldServers/index.ts | 1 + src/utils/worldServers/worldServers.spec.ts | 108 ++++++++++++++++++++ src/utils/worldServers/worldServers.ts | 11 ++ 4 files changed, 120 insertions(+), 9 deletions(-) delete mode 100644 src/utils/worldServers.ts create mode 100644 src/utils/worldServers/index.ts create mode 100644 src/utils/worldServers/worldServers.spec.ts create mode 100644 src/utils/worldServers/worldServers.ts diff --git a/src/utils/worldServers.ts b/src/utils/worldServers.ts deleted file mode 100644 index cfa686f..0000000 --- a/src/utils/worldServers.ts +++ /dev/null @@ -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(',')) -} diff --git a/src/utils/worldServers/index.ts b/src/utils/worldServers/index.ts new file mode 100644 index 0000000..1688028 --- /dev/null +++ b/src/utils/worldServers/index.ts @@ -0,0 +1 @@ +export * from './worldServers' diff --git a/src/utils/worldServers/worldServers.spec.ts b/src/utils/worldServers/worldServers.spec.ts new file mode 100644 index 0000000..d1a853a --- /dev/null +++ b/src/utils/worldServers/worldServers.spec.ts @@ -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(',')) + }) + }) +}) diff --git a/src/utils/worldServers/worldServers.ts b/src/utils/worldServers/worldServers.ts new file mode 100644 index 0000000..e00b152 --- /dev/null +++ b/src/utils/worldServers/worldServers.ts @@ -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(',')) +}