-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
355 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"projects": ["<rootDir>/packages/*"], | ||
"watchPathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/bin/"], | ||
"testPathIgnorePatterns": [ | ||
"<rootDir>/node_modules/", | ||
"<rootDir>/bin/", | ||
"<rootDir>/**/node_modules/", | ||
"<rootDir>/**/bin/", | ||
"<rootDir>/**/__mocks__/", | ||
"<rootDir>/**/coverage/" | ||
], | ||
"transform": { | ||
"^.+\\.(ts|tsx)$": "ts-jest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
{ | ||
"preset": "ts-jest", | ||
"testEnvironment": "node", | ||
"modulePathIgnorePatterns": ["bin", "node_modules"] | ||
"transform": { | ||
"^.+\\.(ts|tsx)$": "ts-jest" | ||
}, | ||
"testMatch": ["<rootDir>/**/*.test.(ts|tsx)"], | ||
"moduleFileExtensions": ["ts", "tsx", "js", "jsx"], | ||
"moduleDirectories": ["node_modules", "<rootDir>/packages"], | ||
"modulePathIgnorePatterns": ["bin", "node_modules", "coverage", "__mocks__"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { mockSessionStorage } from './mock-storage'; | ||
|
||
export const defineGlobalMock = () => { | ||
Object.defineProperty(global, 'sessionStorage', { | ||
value: mockSessionStorage, | ||
}); | ||
}; | ||
|
||
export const clearGlobalMock = () => { | ||
Object.defineProperty(global, 'sessionStorage', {}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const mockSessionStorage = (() => { | ||
let store: Record<string, string> = {}; | ||
|
||
return { | ||
getItem(key: string) { | ||
return store[key] || null; | ||
}, | ||
setItem(key: string, value: string) { | ||
store[key] = value.toString(); | ||
}, | ||
removeItem(key: string) { | ||
delete store[key]; | ||
}, | ||
clear() { | ||
store = {}; | ||
}, | ||
}; | ||
})(); |
164 changes: 164 additions & 0 deletions
164
packages/sdk/src/core/__mocks__/mock-wallet-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import { WalletProvider } from '../providers'; | ||
import { WalletResponseFailedType, WalletResponseStatus, WalletResponseSuccessType } from '../types'; | ||
|
||
export const mockWalletProvider: jest.Mocked<WalletProvider> = { | ||
isConnected: jest.fn().mockResolvedValue({ | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.CONNECTION_SUCCESS, | ||
message: 'Wallet is connected', | ||
data: null, | ||
}), | ||
addEstablish: jest.fn().mockResolvedValue({ | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.CONNECTION_SUCCESS, | ||
message: 'Connection established', | ||
data: null, | ||
}), | ||
getAccount: jest.fn().mockResolvedValue({ | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.GET_ACCOUNT_SUCCESS, | ||
message: 'Account retrieved', | ||
data: { address: 'mock-address', connected: true }, | ||
}), | ||
switchNetwork: jest.fn().mockResolvedValue({ | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.SWITCH_NETWORK_SUCCESS, | ||
message: 'Network switched', | ||
data: null, | ||
}), | ||
addNetwork: jest.fn().mockResolvedValue({ | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.ADD_NETWORK_SUCCESS, | ||
message: 'Network added', | ||
data: null, | ||
}), | ||
signTransaction: jest.fn().mockResolvedValue({ | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.SIGN_SUCCESS, | ||
message: 'Transaction signed', | ||
data: 'mock-signed-transaction', | ||
}), | ||
broadcastTransaction: jest.fn().mockResolvedValue({ | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.TRANSACTION_SUCCESS, | ||
message: 'Transaction broadcasted', | ||
data: { result: 'mock-result' }, | ||
}), | ||
onChangeAccount: jest.fn().mockImplementation(() => { | ||
return { | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.CONNECTION_SUCCESS, | ||
message: 'Account change listener added', | ||
data: null, | ||
}; | ||
}), | ||
onChangeNetwork: jest.fn().mockImplementation(() => { | ||
return { | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.SWITCH_NETWORK_SUCCESS, | ||
message: 'Network change listener added', | ||
data: null, | ||
}; | ||
}), | ||
}; | ||
|
||
jest.mock('../providers', () => ({ | ||
WalletProvider: jest.fn(() => mockWalletProvider), | ||
})); | ||
|
||
export const isConnectedSuccessMock = { | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.CONNECTION_SUCCESS, | ||
message: '', | ||
data: true, | ||
}; | ||
|
||
export const isConnectedFailureMock = { | ||
code: 4000, | ||
status: WalletResponseStatus.FAILURE, | ||
type: WalletResponseFailedType.ALREADY_CONNECTED, | ||
message: '', | ||
data: false, | ||
}; | ||
|
||
export const addEstablishSuccessMock = { | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.CONNECTION_SUCCESS, | ||
message: '', | ||
data: null, | ||
}; | ||
|
||
export const addEstablishFailureMock = { | ||
code: 4000, | ||
status: WalletResponseStatus.FAILURE, | ||
type: WalletResponseFailedType.ALREADY_CONNECTED, | ||
message: '', | ||
data: null, | ||
}; | ||
|
||
export const getAccountSuccessMock = { | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.GET_ACCOUNT_SUCCESS, | ||
message: '', | ||
data: { address: '' }, | ||
}; | ||
|
||
export const switchNetworkSuccessMock = { | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.SWITCH_NETWORK_SUCCESS, | ||
message: '', | ||
data: null, | ||
}; | ||
|
||
export const addNetworkSuccessMock = { | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.ADD_NETWORK_SUCCESS, | ||
message: '', | ||
data: null, | ||
}; | ||
|
||
export const signTransactionSuccessMock = { | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.SIGN_SUCCESS, | ||
message: '', | ||
data: '', | ||
}; | ||
|
||
export const broadcastTransactionSuccessMock = { | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.TRANSACTION_SUCCESS, | ||
message: '', | ||
data: { result: '' }, | ||
}; | ||
|
||
export const onChangeAccountSuccessMock = { | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.CONNECTION_SUCCESS, | ||
message: '', | ||
data: null, | ||
}; | ||
|
||
export const onChangeNetworkSuccessMock = { | ||
code: 0, | ||
status: WalletResponseStatus.SUCCESS, | ||
type: WalletResponseSuccessType.SWITCH_NETWORK_SUCCESS, | ||
message: '', | ||
data: null, | ||
}; |
78 changes: 78 additions & 0 deletions
78
packages/sdk/src/core/__tests__/connection/connection-manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { clearGlobalMock, defineGlobalMock } from '../../__mocks__/mock-global'; | ||
import { | ||
addEstablishFailureMock, | ||
addEstablishSuccessMock, | ||
isConnectedFailureMock, | ||
isConnectedSuccessMock, | ||
mockWalletProvider, | ||
} from '../../__mocks__/mock-wallet-provider'; | ||
import { ConnectionManager, ConnectionState } from '../../connection'; | ||
import { SDKConnectionConfigure } from '../../types/config.types'; | ||
|
||
describe('ConnectionManager', () => { | ||
let connectionManager: ConnectionManager; | ||
let config: SDKConnectionConfigure; | ||
|
||
beforeEach(() => { | ||
defineGlobalMock(); | ||
config = { isSession: true }; | ||
|
||
// Initialize ConnectionManager with a real instance of ConnectionStateManager | ||
connectionManager = new ConnectionManager(mockWalletProvider, config); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
clearGlobalMock(); | ||
}); | ||
|
||
test('should initialize and load state if isSession is true', () => { | ||
// Initially, loadState should set the state to CONNECTED if it was not saved | ||
connectionManager = new ConnectionManager(mockWalletProvider, config); | ||
expect(connectionManager.getConnectionState()).toBe(ConnectionState.CONNECTED); | ||
}); | ||
|
||
test('should set state to CONNECTING when connecting wallet', async () => { | ||
mockWalletProvider.isConnected.mockResolvedValue(isConnectedSuccessMock); | ||
mockWalletProvider.addEstablish.mockResolvedValue(addEstablishSuccessMock); | ||
|
||
await connectionManager.connectWallet(); | ||
|
||
expect(connectionManager.getConnectionState()).toBe(ConnectionState.CONNECTED); | ||
}); | ||
|
||
test('should connect wallet if already connected', async () => { | ||
mockWalletProvider.isConnected.mockResolvedValue(isConnectedSuccessMock); | ||
|
||
await connectionManager.connectWallet(); | ||
|
||
expect(connectionManager.getConnectionState()).toBe(ConnectionState.CONNECTED); | ||
}); | ||
|
||
test('should set state to DISCONNECTED if connection fails', async () => { | ||
connectionManager = new ConnectionManager(mockWalletProvider, { isSession: false }); | ||
mockWalletProvider.isConnected.mockResolvedValue(isConnectedFailureMock); | ||
mockWalletProvider.addEstablish.mockResolvedValue(addEstablishFailureMock); | ||
|
||
await connectionManager.connectWallet(); | ||
|
||
expect(connectionManager.getConnectionState()).toBe(ConnectionState.DISCONNECTED); | ||
}); | ||
|
||
test('should set state to ERROR if an exception is thrown', async () => { | ||
mockWalletProvider.isConnected.mockRejectedValueOnce(new Error('connection error')); | ||
|
||
await expect(connectionManager.connectWallet()).rejects.toThrow('connection error'); | ||
expect(connectionManager.getConnectionState()).toBe(ConnectionState.ERROR); | ||
}); | ||
|
||
test('should trigger connection event when connected', () => { | ||
const listener = jest.fn(); | ||
connectionManager.on(listener); | ||
|
||
connectionManager['connect'](); | ||
|
||
expect(connectionManager.getConnectionState()).toBe(ConnectionState.CONNECTED); | ||
expect(listener).toHaveBeenCalledWith('connect'); | ||
}); | ||
}); |
54 changes: 54 additions & 0 deletions
54
packages/sdk/src/core/__tests__/connection/connection-state-manger.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { clearGlobalMock, defineGlobalMock } from '../../__mocks__/mock-global'; | ||
import { ConnectionState, ConnectionStateManager } from '../../connection'; | ||
import { getSessionStorageItem, setSessionStorageItem } from '../../utils/storage.utils'; | ||
|
||
jest.mock('../../utils/storage.utils', () => ({ | ||
getSessionStorageItem: jest.fn(), | ||
setSessionStorageItem: jest.fn(), | ||
})); | ||
|
||
describe('ConnectionStateManager', () => { | ||
let manager: ConnectionStateManager; | ||
|
||
beforeEach(() => { | ||
defineGlobalMock(); | ||
manager = new ConnectionStateManager(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
clearGlobalMock(); | ||
}); | ||
|
||
it('should initialize with DISCONNECTED state', () => { | ||
expect(manager.getState()).toBe(ConnectionState.DISCONNECTED); | ||
}); | ||
|
||
it('should save state to session storage when setState is called', () => { | ||
manager.setState(ConnectionState.CONNECTED); | ||
|
||
expect(setSessionStorageItem).toHaveBeenCalledWith( | ||
'adena-sdk-connection-state', | ||
ConnectionState.CONNECTED.toString() | ||
); | ||
expect(manager.getState()).toBe(ConnectionState.CONNECTED); | ||
}); | ||
|
||
it('should load state from session storage when loadState is called', () => { | ||
(getSessionStorageItem as jest.Mock).mockReturnValue(ConnectionState.CONNECTED.toString()); | ||
|
||
manager.loadState(); | ||
|
||
expect(getSessionStorageItem).toHaveBeenCalledWith('adena-sdk-connection-state'); | ||
expect(manager.getState()).toBe(ConnectionState.CONNECTED); | ||
}); | ||
|
||
it('should not change state if the loaded state is not CONNECTED', () => { | ||
(getSessionStorageItem as jest.Mock).mockReturnValue(ConnectionState.DISCONNECTED.toString()); | ||
|
||
manager.loadState(); | ||
|
||
expect(getSessionStorageItem).toHaveBeenCalledWith('adena-sdk-connection-state'); | ||
expect(manager.getState()).toBe(ConnectionState.DISCONNECTED); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.