diff --git a/src/utils/worldServers/worldServers.spec.ts b/src/utils/worldServers/worldServers.spec.ts index d1a853a..9af85a9 100644 --- a/src/utils/worldServers/worldServers.spec.ts +++ b/src/utils/worldServers/worldServers.spec.ts @@ -80,6 +80,18 @@ describe('when adding a new server to the same key in the local storage', () => jest.spyOn(Storage.prototype, 'setItem').mockImplementationOnce(jest.fn()) }) + describe('and the new server is already in the array of previously loaded servers', () => { + beforeEach(() => { + jest.spyOn(Storage.prototype, 'getItem').mockReturnValueOnce([newServer].join(',')) + addServerToPreviouslyLoaded(newServer) + }) + + it('should not call the local storage set method to add the new server', () => { + // eslint-disable-next-line @typescript-eslint/unbound-method + expect(localStorage.setItem).not.toBeCalled() + }) + }) + describe('and there was not previous loaded servers in the local storage', () => { beforeEach(() => { jest.spyOn(Storage.prototype, 'getItem').mockReturnValueOnce(null) diff --git a/src/utils/worldServers/worldServers.ts b/src/utils/worldServers/worldServers.ts index 76d4bc8..22d07b1 100644 --- a/src/utils/worldServers/worldServers.ts +++ b/src/utils/worldServers/worldServers.ts @@ -5,6 +5,9 @@ export const getPreviouslyLoadedServers = () => export const addServerToPreviouslyLoaded = (server: string) => { const previouslyLoadedServers = getPreviouslyLoadedServers() || [] + + if (previouslyLoadedServers.includes(server)) return + previouslyLoadedServers.push(server) localStorage.setItem(PREVIOUSLY_LOADED_SERVERS_KEY, previouslyLoadedServers.join(',')) }