From e1102460f6ade3ff129a3a87394b65e956ffe75d Mon Sep 17 00:00:00 2001 From: Dawid Sowa Date: Wed, 4 Dec 2024 19:15:43 +0100 Subject: [PATCH] test: add tests to `immer` related modules --- .../data-request/data-request-state.spec.ts | 51 +++++++- .../helpers/to-wallet-request.spec.ts | 114 ++++++++++++++++++ 2 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 packages/dapp-toolkit/src/modules/wallet-request/data-request/helpers/to-wallet-request.spec.ts diff --git a/packages/dapp-toolkit/src/modules/wallet-request/data-request/data-request-state.spec.ts b/packages/dapp-toolkit/src/modules/wallet-request/data-request/data-request-state.spec.ts index 6877dbaa..1e6854a9 100644 --- a/packages/dapp-toolkit/src/modules/wallet-request/data-request/data-request-state.spec.ts +++ b/packages/dapp-toolkit/src/modules/wallet-request/data-request/data-request-state.spec.ts @@ -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' @@ -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( diff --git a/packages/dapp-toolkit/src/modules/wallet-request/data-request/helpers/to-wallet-request.spec.ts b/packages/dapp-toolkit/src/modules/wallet-request/data-request/helpers/to-wallet-request.spec.ts new file mode 100644 index 00000000..9edf9747 --- /dev/null +++ b/packages/dapp-toolkit/src/modules/wallet-request/data-request/helpers/to-wallet-request.spec.ts @@ -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) + }) + }) +})