Skip to content

Commit

Permalink
test: add tests to immer related modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Dec 6, 2024
1 parent 719c02d commit e110246
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OneTimeDataRequestBuilder } from './builders'
import { DataRequestBuilder, OneTimeDataRequestBuilder } from './builders'
import { accounts } from './builders/accounts'
import { persona } from './builders/persona'
import { personaData } from './builders/persona-data'
Expand All @@ -18,6 +18,55 @@ describe('DataRequestStateModule', () => {
})
})

describe('patchState then removeState', () => {
it('should add state then remove state', () => {
dataRequest.patchState(
DataRequestBuilder.persona().withProof(),
DataRequestBuilder.personaData().fullName(),
)

expect(dataRequest.getState()).toEqual({
accounts: {
numberOfAccounts: {
quantifier: 'atLeast',
quantity: 1,
},
reset: false,
withProof: false,
},
persona: {
withProof: true,
},
personaData: {
fullName: true,
},
})
dataRequest.removeState('persona', 'accounts')
expect(dataRequest.getState()).toEqual({
personaData: {
fullName: true,
},
})
})
})

describe('reset', () => {
it('should set initial state', () => {
dataRequest.patchState(DataRequestBuilder.persona().withProof())
dataRequest.reset()
expect(dataRequest.getState()).toEqual({
accounts: {
numberOfAccounts: {
quantifier: 'atLeast',
quantity: 1,
},
reset: false,
withProof: false,
},
})
})
})

describe('toDataRequestState', () => {
it('should consume one time data request builder proofOfOwnership', () => {
const state = dataRequest.toDataRequestState(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { describe, expect, it } from 'vitest'
import { toWalletRequest } from './to-wallet-request'

describe('toWalletRequest', () => {
it('should transform data request to wallet request', () => {
const testCases: [any, any][] = [
[
{
isConnect: true,
oneTime: false,
dataRequestState: {},
walletData: {
accounts: [],
personaData: [],
proofs: [],
},
},
{
auth: {
discriminator: 'loginWithoutChallenge',
},
discriminator: 'authorizedRequest',
reset: {
accounts: false,
personaData: false,
},
},
],
[
{
isConnect: false,
oneTime: true,
challenge: 'abc',
dataRequestState: {
proofOfOwnership: {},
accounts: {},
},
walletData: {
persona: {},
},
},
{
auth: {
discriminator: 'loginWithoutChallenge',
},
discriminator: 'authorizedRequest',
oneTimeAccounts: {
challenge: undefined,
numberOfAccounts: {
quantifier: 'atLeast',
quantity: 1,
},
},
proofOfOwnership: {
challenge: 'abc',
},
reset: {
accounts: false,
personaData: false,
},
},
],
[
{
isConnect: true,
oneTime: false,
challenge: 'abc',
dataRequestState: {
accounts: {
withProof: true,
},
personaData: {
reset: true,
},
persona: {
withProof: true,
},
},
walletData: {
persona: {},
},
},
{
auth: {
challenge: 'abc',
discriminator: 'loginWithChallenge',
},
discriminator: 'authorizedRequest',
ongoingAccounts: {
challenge: 'abc',
numberOfAccounts: {
quantifier: 'atLeast',
quantity: 1,
},
},
ongoingPersonaData: {
isRequestingName: undefined,
numberOfRequestedEmailAddresses: undefined,
numberOfRequestedPhoneNumbers: undefined,
},
reset: {
accounts: false,
personaData: false,
},
},
],
]

testCases.forEach(([input, expected]) => {
const result = toWalletRequest(input)
expect(result.isOk() && result.value).toEqual(expected)
})
})
})

0 comments on commit e110246

Please sign in to comment.