Skip to content

Commit

Permalink
refactor: remove unnecessary safe or
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinszuchet committed Nov 15, 2024
1 parent 14ffb74 commit 8dcd203
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion processor/src/adapters/badge-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function createBadgeContext({

return (attempt: number): Promise<T> => {
if (attempt > 1 && catalystServers.length > 0) {
const [catalystServerUrl] = catalystServers.splice(attempt % catalystServers.length, 1) || []
const [catalystServerUrl] = catalystServers.splice(attempt % catalystServers.length, 1)
contentClientToUse = getContentClientOrDefault(catalystServerUrl)
}

Expand Down
54 changes: 54 additions & 0 deletions processor/test/unit/utils/array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { shuffleArray } from '../../../src/utils/array'

describe('shuffleArray', () => {
it('should return an array with the same elements in a different order', () => {
const originalArray = [1, 2, 3, 4, 5]
const shuffledArray = shuffleArray([...originalArray])

expect(shuffledArray).toHaveLength(originalArray.length)
expect(shuffledArray).not.toEqual(originalArray)
expect(shuffledArray.sort()).toEqual(originalArray.sort())
})

it('should shuffle elements randomly (mocked Math.random)', () => {
const originalArray = [1, 2, 3, 4, 5]

// to control the shuffle
jest
.spyOn(global.Math, 'random')
.mockReturnValueOnce(0.1)
.mockReturnValueOnce(0.3)
.mockReturnValueOnce(0.7)
.mockReturnValueOnce(0.9)
.mockReturnValueOnce(0.5)

const shuffledArray = shuffleArray([...originalArray])

expect(shuffledArray).not.toEqual(originalArray)

jest.restoreAllMocks()
})

it('should not modify the original array', () => {
const originalArray = [1, 2, 3, 4, 5]
const arrayCopy = [...originalArray]

shuffleArray(arrayCopy)

expect(originalArray).toEqual([1, 2, 3, 4, 5])
})

it('should handle an empty array gracefully', () => {
const emptyArray: number[] = []
const shuffledArray = shuffleArray(emptyArray)

expect(shuffledArray).toEqual([])
})

it('should handle an array with one element gracefully', () => {
const singleElementArray = [1]
const shuffledArray = shuffleArray(singleElementArray)

expect(shuffledArray).toEqual([1])
})
})

0 comments on commit 8dcd203

Please sign in to comment.