fix: Avoid adding a repeated server

This commit is contained in:
Kevin Szuchet 2023-08-09 14:23:35 +02:00
parent 8ba22fa750
commit 71156337ee
No known key found for this signature in database
GPG Key ID: 1E083BC33700E594
2 changed files with 15 additions and 0 deletions

View File

@ -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)

View File

@ -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(','))
}