Skip to content

Commit

Permalink
fix: Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
fzavalia committed Oct 4, 2023
1 parent cca1011 commit 4d4ea17
Showing 1 changed file with 67 additions and 63 deletions.
130 changes: 67 additions & 63 deletions src/modules/ens/selectors.spec.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,111 @@
import { WalletState } from 'decentraland-dapps/dist/modules/wallet/reducer'
import { RootState } from 'modules/common/types'
import { getExternalNames, getExternalNamesForConnectedWallet } from './selectors'
import { getExternalNames, getExternalNamesForConnectedWallet, getExternalNamesForWallet } from './selectors'
import { ENSState } from './reducer'

let state: RootState
let wallet: string
let wallet1: string
let wallet2: string
let externalNames: ENSState['externalNames']

beforeEach(() => {
state = {} as RootState
wallet = '0x123'
wallet1 = '0x123'
wallet2 = '0x456'
externalNames = {
'name1.eth': {
domain: 'name1.eth',
nftOwnerAddress: wallet
nftOwnerAddress: wallet1
},
'name2.eth': {
domain: 'name2.eth',
nftOwnerAddress: wallet
nftOwnerAddress: wallet1
},
'name3.eth': {
domain: 'name2.eth',
nftOwnerAddress: '0xOtherWallet'
domain: 'name3.eth',
nftOwnerAddress: wallet2
}
} as unknown as ENSState['externalNames']

state = {
ens: {
externalNames
},
wallet: {
data: {
address: wallet1
}
}
} as RootState
})

describe('when getting the external names', () => {
beforeEach(() => {
state = {
ens: {
externalNames
} as ENSState
} as RootState
})

it('should return a record of external names by domain', () => {
expect(getExternalNames(state)).toEqual(externalNames)
})
})

describe('when getting the external names by wallet', () => {
describe('when the external names has an entry for the wallet', () => {
beforeEach(() => {
state = {
ens: {
externalNames
} as ENSState,
wallet: {
data: {
address: wallet
describe('when getting the external names for the connected wallet', () => {
describe('when there is a connected wallet', () => {
describe('when the external names has an entry for the connected wallet', () => {
it('should return the names of the wallet', () => {
expect(getExternalNamesForConnectedWallet(state)).toEqual([
{
domain: 'name1.eth',
nftOwnerAddress: wallet1
},
{
domain: 'name2.eth',
nftOwnerAddress: wallet1
}
} as WalletState
} as RootState
])
})
})

it('should return the names of the wallet', () => {
expect(getExternalNamesForConnectedWallet(state)).toEqual([
{
domain: 'name1.eth',
nftOwnerAddress: wallet
},
{
domain: 'name2.eth',
nftOwnerAddress: wallet
}
])
describe('when the external names does not have an entry for the connected wallet', () => {
beforeEach(() => {
state.ens.externalNames = {}
})

it('should return an empty array', () => {
expect(getExternalNamesForConnectedWallet(state)).toEqual([])
})
})
})

describe('when the external names does not have an entry for the wallet', () => {
describe('when there is no connected wallet', () => {
beforeEach(() => {
state = {
ens: {
externalNames: {}
} as ENSState,
wallet: {
data: {
address: wallet
}
} as WalletState
} as RootState
state.wallet.data && (state.wallet.data = null)
})

it('should return an empty array', () => {
expect(getExternalNamesForConnectedWallet(state)).toEqual([])
})
})
})

describe('when there is no wallet', () => {
beforeEach(() => {
state = {
ens: {
externalNames
} as ENSState,
wallet: {
data: null
} as WalletState
} as RootState
describe('when getting the external names for a provided wallet', () => {
describe('when getting the names for wallet1', () => {
it('should return the external names of wallet1', () => {
expect(getExternalNamesForWallet(wallet1)(state)).toEqual([
{
domain: 'name1.eth',
nftOwnerAddress: wallet1
},
{
domain: 'name2.eth',
nftOwnerAddress: wallet1
}
])
})
})

it('should return an empty array', () => {
expect(getExternalNamesForConnectedWallet(state)).toEqual([])
describe('when getting the names for wallet2', () => {
it('should return the external names of wallet2', () => {
expect(getExternalNamesForWallet(wallet2)(state)).toEqual([
{
domain: 'name3.eth',
nftOwnerAddress: wallet2
}
])
})
})
})

0 comments on commit 4d4ea17

Please sign in to comment.