Skip to content

Commit

Permalink
fix: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fzavalia committed Oct 4, 2023
1 parent f5c6498 commit ba26d90
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
111 changes: 111 additions & 0 deletions src/components/Modals/WorldsYourStorageModal/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { AccountHoldings, fetchAccountHoldings, getMbsFromAccountHoldings } from './utils'

describe('when fetching the account holdings', () => {
let account: string

beforeEach(() => {
account = '0x123'
})

describe('when the request fails', () => {
beforeEach(() => {
jest.spyOn(global, 'fetch').mockRejectedValueOnce(new Error('some error'))
})

it('should return null', async () => {
expect(await fetchAccountHoldings(account)).toBeNull()
})
})

describe('when the request does not fail', () => {
describe('when the response is not ok', () => {
beforeEach(() => {
jest.spyOn(global, 'fetch').mockResolvedValueOnce({
ok: false
} as Response)
})

it('should return null', async () => {
expect(await fetchAccountHoldings(account)).toBeNull()
})
})

describe('when the response is ok', () => {
let accountHoldings: AccountHoldings

beforeEach(() => {
accountHoldings = {} as AccountHoldings

jest.spyOn(global, 'fetch').mockResolvedValueOnce({
ok: true,
json: () =>
Promise.resolve({
data: accountHoldings
})
} as Response)
})

it('should return null', async () => {
expect(await fetchAccountHoldings(account)).toEqual(accountHoldings)
})
})
})
})

describe('when gettings mbs from account holdings', () => {
let accountHoldings: AccountHoldings

describe('when account holdings has 10000 MANA, 100 LANDs and 100 NAMEs', () => {
beforeEach(() => {
accountHoldings = {
ownedMana: 10000,
ownedLands: 100,
ownedNames: 100
} as AccountHoldings
})

it('should return an object with 500 manaMbs, 10000 landMbs and 10000 nameMbs', () => {
expect(getMbsFromAccountHoldings(accountHoldings)).toEqual({
manaMbs: 500,
landMbs: 10000,
nameMbs: 10000
})
})
})

describe('when account holdings has 0 MANA, 0 LANDs and 0 NAMEs', () => {
beforeEach(() => {
accountHoldings = {
ownedMana: 0,
ownedLands: 0,
ownedNames: 0
} as AccountHoldings
})

it('should return an object with 0 manaMbs, 0 landMbs and 0 nameMbs', () => {
expect(getMbsFromAccountHoldings(accountHoldings)).toEqual({
manaMbs: 0,
landMbs: 0,
nameMbs: 0
})
})
})

describe('when account holdings has 1999 MANA, 0 LANDs and 0 NAMEs', () => {
beforeEach(() => {
accountHoldings = {
ownedMana: 1999,
ownedLands: 0,
ownedNames: 0
} as AccountHoldings
})

it('should return an object with 0 manaMbs, 0 landMbs and 0 nameMbs', () => {
expect(getMbsFromAccountHoldings(accountHoldings)).toEqual({
manaMbs: 0,
landMbs: 0,
nameMbs: 0
})
})
})
})
2 changes: 0 additions & 2 deletions src/components/Modals/WorldsYourStorageModal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ export const fetchAccountHoldings = async (account: string) => {
method: 'POST'
})
} catch (e) {
console.warn(`Error fetching account holdings for ${account}`, e.message)
return null
}

if (!response.ok) {
console.warn(`Error fetching account holdings for ${account}`, response.status)
return null
}

Expand Down

0 comments on commit ba26d90

Please sign in to comment.