Skip to content

Commit

Permalink
fix: Make address mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
fzavalia committed Oct 4, 2023
1 parent 695fc1c commit bd1414e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/modules/worlds/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('when creating the failure action to fetch worlds stats for a wallet',
})

it('should return the failure action to fetch worlds stats for a wallet', () => {
expect(fetchWorldsWalletStatsFailure(error, address)).toEqual({
expect(fetchWorldsWalletStatsFailure(address, error)).toEqual({
type: FETCH_WORLDS_WALLET_STATS_FAILURE,
payload: {
address,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/worlds/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export const FETCH_WORLDS_WALLET_STATS_REQUEST = '[Request] Fetch Worlds Wallet
export const FETCH_WORLDS_WALLET_STATS_SUCCESS = '[Success] Fetch Worlds Wallet Stats'
export const FETCH_WORLDS_WALLET_STATS_FAILURE = '[Failure] Fetch Worlds Wallet Stats'

export const fetchWorldsWalletStatsRequest = (address?: string) => action(FETCH_WORLDS_WALLET_STATS_REQUEST, { address })
export const fetchWorldsWalletStatsRequest = (address: string) => action(FETCH_WORLDS_WALLET_STATS_REQUEST, { address })
export const fetchWorldsWalletStatsSuccess = (address: string, stats: WorldsWalletStats) =>
action(FETCH_WORLDS_WALLET_STATS_SUCCESS, { address, stats })
export const fetchWorldsWalletStatsFailure = (error: string, address?: string) =>
export const fetchWorldsWalletStatsFailure = (address: string, error: string) =>
action(FETCH_WORLDS_WALLET_STATS_FAILURE, { address, error })

export type FetchWalletWorldsStatsRequestAction = ReturnType<typeof fetchWorldsWalletStatsRequest>
Expand Down
2 changes: 1 addition & 1 deletion src/modules/worlds/reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('when handling the action to fetch worlds stats for a wallet', () => {
})

it('should remove the action from loading and set the error', () => {
const action = fetchWorldsWalletStatsFailure(error, address)
const action = fetchWorldsWalletStatsFailure(address, error)

expect(worldsReducer(state, action)).toEqual({
...state,
Expand Down
134 changes: 38 additions & 96 deletions src/modules/worlds/sagas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,128 +4,70 @@ import { throwError } from 'redux-saga-test-plan/providers'
import { connectWalletSuccess } from 'decentraland-dapps/dist/modules/wallet/actions'
import { Wallet } from 'decentraland-dapps/dist/modules/wallet/types'
import { WorldsWalletStats, content } from 'lib/api/worlds'
import { getAddressOrWaitConnection } from 'modules/wallet/utils'
import { fetchWorldsWalletStatsFailure, fetchWorldsWalletStatsRequest, fetchWorldsWalletStatsSuccess } from './actions'
import { worldsSaga } from './sagas'

let address: string | undefined
let address: string

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

describe('when handling the request action to fetch worlds stats for a wallet', () => {
let stats: WorldsWalletStats
describe('when the worlds api request throws an error', () => {
let error: Error

describe('when the address is provided in the action', () => {
beforeEach(() => {
address = '0x123'
})

describe('when the worlds api request throws an error', () => {
let error: Error

beforeEach(() => {
error = new Error('some error')
})

it('should put the failure action with the request action address and the error message', () => {
return expectSaga(worldsSaga)
.provide([[call([content, content.fetchWalletStats], address!), throwError(error)]])
.put(fetchWorldsWalletStatsFailure(error.message, address))
.dispatch(fetchWorldsWalletStatsRequest(address))
.silentRun()
})
error = new Error('some error')
})

describe('when the worlds api request returns null', () => {
it('should put the failure action with the request action address and the error message', () => {
return expectSaga(worldsSaga)
.provide([[call([content, content.fetchWalletStats], address!), null]])
.put(fetchWorldsWalletStatsFailure('Could not fetch wallet stats', address))
.dispatch(fetchWorldsWalletStatsRequest(address))
.silentRun()
})
it('should put the failure action with the request action address and the error message', () => {
return expectSaga(worldsSaga)
.provide([[call([content, content.fetchWalletStats], address), throwError(error)]])
.put(fetchWorldsWalletStatsFailure(address, error.message))
.dispatch(fetchWorldsWalletStatsRequest(address))
.silentRun()
})
})

describe('when the worlds api request returns the stats', () => {
beforeEach(() => {
stats = {
dclNames: [],
ensNames: [],
maxAllowedSpace: '',
usedSpace: '',
wallet: address!
}
})

it('should put the success action with the request action address and the stats', () => {
return expectSaga(worldsSaga)
.provide([[call([content, content.fetchWalletStats], address!), stats]])
.put(fetchWorldsWalletStatsSuccess(address!, stats))
.dispatch(fetchWorldsWalletStatsRequest(address))
.silentRun()
})
describe('when the worlds api request returns null', () => {
it('should put the failure action with the request action address and the error message', () => {
return expectSaga(worldsSaga)
.provide([[call([content, content.fetchWalletStats], address), null]])
.put(fetchWorldsWalletStatsFailure(address, 'Could not fetch wallet stats'))
.dispatch(fetchWorldsWalletStatsRequest(address))
.silentRun()
})
})

describe('when the address is not provided in the action', () => {
let getAddressOrWaitConnectionResult: string | undefined
describe('when the worlds api request returns the stats', () => {
let stats: WorldsWalletStats

beforeEach(() => {
address = undefined
stats = {
dclNames: [],
ensNames: [],
maxAllowedSpace: '',
usedSpace: '',
wallet: address
}
})

describe('and the get address or wait for connection function returns an address', () => {
beforeEach(() => {
getAddressOrWaitConnectionResult = '0x123'
})

describe('and the request to fetch wallet stats responds with the wallet stats', () => {
beforeEach(() => {
stats = {
dclNames: [],
ensNames: [],
maxAllowedSpace: '',
usedSpace: '',
wallet: getAddressOrWaitConnectionResult!
}
})

it('should put the success action with the address from the get address or wait for connection function and the stats', () => {
return expectSaga(worldsSaga)
.provide([
[call(getAddressOrWaitConnection), getAddressOrWaitConnectionResult],
[call([content, content.fetchWalletStats], getAddressOrWaitConnectionResult!), stats]
])
.put(fetchWorldsWalletStatsSuccess(getAddressOrWaitConnectionResult!, stats))
.dispatch(fetchWorldsWalletStatsRequest(address))
.silentRun()
})
})
})

describe('and the get address or wait for connection function does not return an address', () => {
beforeEach(() => {
getAddressOrWaitConnectionResult = undefined
})

it('should put a failure action with an undefined address and the error message', () => {
return expectSaga(worldsSaga)
.provide([[call(getAddressOrWaitConnection), getAddressOrWaitConnectionResult]])
.put(fetchWorldsWalletStatsFailure('An address is required', getAddressOrWaitConnectionResult))
.dispatch(fetchWorldsWalletStatsRequest(address))
.silentRun()
})
it('should put the success action with the request action address and the stats', () => {
return expectSaga(worldsSaga)
.provide([[call([content, content.fetchWalletStats], address), stats]])
.put(fetchWorldsWalletStatsSuccess(address, stats))
.dispatch(fetchWorldsWalletStatsRequest(address))
.silentRun()
})
})
})

describe('when the wallet connects', () => {
beforeEach(() => {
address = '0x123'
})

it('should put the action to fetch worlds stats for the connected wallet address', () => {
return expectSaga(worldsSaga)
.put(fetchWorldsWalletStatsRequest(address))
.dispatch(connectWalletSuccess({ address: address! } as Wallet))
.dispatch(connectWalletSuccess({ address } as Wallet))
.silentRun()
})
})
9 changes: 2 additions & 7 deletions src/modules/worlds/sagas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { call, put, takeEvery } from 'redux-saga/effects'
import { CONNECT_WALLET_SUCCESS, ConnectWalletSuccessAction } from 'decentraland-dapps/dist/modules/wallet/actions'
import { getAddressOrWaitConnection } from 'modules/wallet/utils'
import { WorldsWalletStats, content as WorldsAPIContent } from 'lib/api/worlds'
import {
FETCH_WORLDS_WALLET_STATS_REQUEST,
Expand All @@ -16,13 +15,9 @@ export function* worldsSaga() {
}

function* handlefetchWorldsWalletStatsRequest(action: FetchWalletWorldsStatsRequestAction) {
const address = action.payload.address ?? (yield call(getAddressOrWaitConnection))
const { address } = action.payload

try {
if (!address) {
throw new Error('An address is required')
}

const stats: WorldsWalletStats | null = yield call([WorldsAPIContent, WorldsAPIContent.fetchWalletStats], address)

if (!stats) {
Expand All @@ -31,7 +26,7 @@ function* handlefetchWorldsWalletStatsRequest(action: FetchWalletWorldsStatsRequ

yield put(fetchWorldsWalletStatsSuccess(address, stats))
} catch (e) {
yield put(fetchWorldsWalletStatsFailure(e.message, address))
yield put(fetchWorldsWalletStatsFailure(address, e.message))
}
}

Expand Down

0 comments on commit bd1414e

Please sign in to comment.