@getclave/lifi-sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
104 lines (103 loc) • 5.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const constants_1 = require("../../errors/constants");
const errors_1 = require("../../errors/errors");
const fixtures_1 = require("../../tests/fixtures");
const switchChain_1 = require("./switchChain");
let client;
let step;
let statusManager;
let hooks;
let requestMock;
let switchChainHookMock;
let findOrCreateProcessMock;
let updateExecutionMock;
let updateProcessMock;
(0, vitest_1.describe)('switchChain', () => {
(0, vitest_1.beforeEach)(() => {
switchChainHookMock = vitest_1.vi.fn();
hooks = {
switchChainHook: switchChainHookMock,
};
step = (0, fixtures_1.buildStepObject)({ includingExecution: false });
requestMock = vitest_1.vi.fn(() => 1);
client = {
request: requestMock,
};
findOrCreateProcessMock = vitest_1.vi.fn();
updateExecutionMock = vitest_1.vi.fn();
updateProcessMock = vitest_1.vi.fn();
statusManager = {
initExecutionObject: vitest_1.vi.fn(),
findOrCreateProcess: findOrCreateProcessMock,
removeProcess: vitest_1.vi.fn(),
updateExecution: updateExecutionMock,
updateProcess: updateProcessMock,
};
});
(0, vitest_1.describe)('when the chain is already correct', () => {
(0, vitest_1.beforeEach)(() => {
requestMock.mockResolvedValue(step.action.fromChainId);
});
(0, vitest_1.it)('should return the connector client and do nothing else', async () => {
const updatedClient = await (0, switchChain_1.switchChain)(client, statusManager, step, true, hooks.switchChainHook);
(0, vitest_1.expect)(updatedClient).toEqual(client);
(0, vitest_1.expect)(statusManager.initExecutionObject).not.toHaveBeenCalled();
(0, vitest_1.expect)(hooks.switchChainHook).not.toHaveBeenCalled();
});
});
(0, vitest_1.describe)('when the chain is not correct', () => {
(0, vitest_1.beforeEach)(() => {
requestMock.mockResolvedValueOnce(1);
findOrCreateProcessMock.mockReturnValue({ type: 'SWITCH_CHAIN' });
});
(0, vitest_1.describe)('when allowUserInteraction is false', () => {
(0, vitest_1.it)('should initialize the status manager and abort', async () => {
const updatedClient = await (0, switchChain_1.switchChain)(client, statusManager, step, false, hooks.switchChainHook);
(0, vitest_1.expect)(updatedClient).toBeUndefined();
(0, vitest_1.expect)(statusManager.initExecutionObject).toHaveBeenCalled();
(0, vitest_1.expect)(statusManager.findOrCreateProcess).toHaveBeenCalled();
(0, vitest_1.expect)(hooks.switchChainHook).not.toHaveBeenCalled();
});
});
(0, vitest_1.describe)('when allowUserInteraction is true', () => {
(0, vitest_1.describe)('when the switchChainHook fails', () => {
(0, vitest_1.beforeEach)(() => {
switchChainHookMock.mockRejectedValue(new Error('something went wrong'));
});
(0, vitest_1.it)('should throw the error', async () => {
await (0, vitest_1.expect)((0, switchChain_1.switchChain)(client, statusManager, step, true, hooks.switchChainHook)).rejects.toThrowError(new Error('something went wrong'));
(0, vitest_1.expect)(switchChainHookMock).toHaveBeenCalledWith(step.action.fromChainId);
(0, vitest_1.expect)(updateExecutionMock).toHaveBeenCalledWith(step, 'FAILED');
});
});
(0, vitest_1.describe)("when the switchChainHook doesn't change the chain", () => {
(0, vitest_1.beforeEach)(() => {
switchChainHookMock.mockResolvedValue(client);
});
(0, vitest_1.it)('should throw the error', async () => {
await (0, vitest_1.expect)((0, switchChain_1.switchChain)(client, statusManager, step, true, hooks.switchChainHook)).rejects.toThrowError(new errors_1.ProviderError(constants_1.LiFiErrorCode.ChainSwitchError, 'Chain switch required.'));
(0, vitest_1.expect)(switchChainHookMock).toHaveBeenCalledWith(step.action.fromChainId);
(0, vitest_1.expect)(updateExecutionMock).toHaveBeenCalledWith(step, 'FAILED');
});
});
(0, vitest_1.describe)('when the switchChainHook changes the chain', () => {
let newClient;
(0, vitest_1.beforeEach)(() => {
newClient = {
request: () => Promise.resolve(step.action.fromChainId),
};
switchChainHookMock.mockResolvedValue(newClient);
});
(0, vitest_1.it)('should return the updated connector client', async () => {
const updatedClient = await (0, switchChain_1.switchChain)(client, statusManager, step, true, hooks.switchChainHook);
(0, vitest_1.expect)(switchChainHookMock).toHaveBeenCalledWith(step.action.fromChainId);
(0, vitest_1.expect)(updatedClient).toEqual(newClient);
(0, vitest_1.expect)(statusManager.updateProcess).toHaveBeenCalledWith(step, 'SWITCH_CHAIN', 'DONE');
(0, vitest_1.expect)(statusManager.updateExecution).toHaveBeenCalledWith(step, 'PENDING');
});
});
});
});
});