Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions packages/sdk-multichain/src/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import { createMetamaskSDK as createMetamaskSDKWeb } from './index.browser';
import { createMetamaskSDK as createMetamaskSDKRN } from './index.native';
import { createMetamaskSDK as createMetamaskSDKNode } from './index.node';
import { Store } from './store';
import * as nodeStorage from './store/adapters/node';
import * as rnStorage from './store/adapters/rn';
import * as webStorage from './store/adapters/web';

function testSuite<T extends MultiChainFNOptions>({ platform, createSDK, options: sdkOptions, ...options }: TestSuiteOptions<T>) {
const { beforeEach, afterEach } = options;
Expand All @@ -36,7 +33,18 @@ function testSuite<T extends MultiChainFNOptions>({ platform, createSDK, options
enabled: platform !== 'node',
integrationType: 'test',
},
storage: new Store(mockedData.nativeStorageStub),
storage: new Store({
platform: platform as 'web' | 'rn' | 'node',
get(key) {
return Promise.resolve(mockedData.nativeStorageStub.getItem(key));
},
set(key, value) {
return Promise.resolve(mockedData.nativeStorageStub.setItem(key, value));
},
delete(key) {
return Promise.resolve(mockedData.nativeStorageStub.removeItem(key));
},
}),
};
});

Expand Down Expand Up @@ -226,13 +234,16 @@ createTest({
...baseTestOptions,
platform: 'node',
createSDK: createMetamaskSDKNode,
setupMocks: (nativeStorageStub) => {
setupMocks: () => {
const memfs = new Map<string, any>();
t.vi.spyOn(fs, 'existsSync').mockImplementation((path) => memfs.has(path.toString()));
t.vi.spyOn(fs, 'writeFileSync').mockImplementation((path, data) => memfs.set(path.toString(), data));
t.vi.spyOn(fs, 'readFileSync').mockImplementation((path) => memfs.get(path.toString()));
t.vi.spyOn(nodeStorage, 'StoreAdapterNode').mockImplementation(() => {
return nativeStorageStub as any;
t.vi.spyOn(fs, 'existsSync').mockImplementation((path) => {
return memfs.has(path.toString());
});
t.vi.spyOn(fs, 'writeFileSync').mockImplementation((path, data) => {
return memfs.set(path.toString(), data);
});
t.vi.spyOn(fs, 'readFileSync').mockImplementation((path) => {
return memfs.get(path.toString());
});
},
});
Expand All @@ -242,11 +253,14 @@ createTest({
platform: 'rn',
createSDK: createMetamaskSDKRN,
setupMocks: (nativeStorageStub) => {
t.vi.spyOn(AsyncStorage, 'getItem').mockImplementation(async (key) => nativeStorageStub.getItem(key));
t.vi.spyOn(AsyncStorage, 'setItem').mockImplementation(async (key, value) => nativeStorageStub.setItem(key, value));
t.vi.spyOn(AsyncStorage, 'removeItem').mockImplementation(async (key) => nativeStorageStub.deleteItem(key));
t.vi.spyOn(rnStorage, 'StoreAdapterRN').mockImplementation(() => {
return nativeStorageStub as any;
t.vi.spyOn(AsyncStorage, 'getItem').mockImplementation(async (key) => {
return nativeStorageStub.getItem(key);
});
t.vi.spyOn(AsyncStorage, 'setItem').mockImplementation(async (key, value) => {
return nativeStorageStub.setItem(key, value);
});
t.vi.spyOn(AsyncStorage, 'removeItem').mockImplementation(async (key) => {
return nativeStorageStub.removeItem(key);
});
},
});
Expand All @@ -256,9 +270,7 @@ createTest({
platform: 'web',
createSDK: createMetamaskSDKWeb,
setupMocks: (nativeStorageStub) => {
const dom = new Page('<!DOCTYPE html><p>Hello world</p>', {
url: exampleDapp.url,
});
const dom = new Page('<!DOCTYPE html><p>Hello world</p>', { url: exampleDapp.url });
const globalStub = {
...dom.window,
addEventListener: t.vi.fn(),
Expand All @@ -278,8 +290,8 @@ createTest({
t.vi.stubGlobal('document', dom.window.document);
t.vi.stubGlobal('HTMLElement', dom.window.HTMLElement);
t.vi.stubGlobal('requestAnimationFrame', t.vi.fn());
t.vi.spyOn(webStorage, 'StoreAdapterWeb').mockImplementation(() => {
return nativeStorageStub as any;
});
// t.vi.spyOn(webStorage, 'StoreAdapterWeb').mockImplementation(() => {
// return nativeStorageStub as any;
// });
},
});
9 changes: 3 additions & 6 deletions packages/sdk-multichain/src/domain/store/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ export type StoreOptions = Record<string, any>;
export abstract class StoreAdapter {
abstract platform: 'web' | 'rn' | 'node';
constructor(public options?: StoreOptions) {}

abstract getItem(key: string): Promise<string | null>;

abstract setItem(key: string, value: string): Promise<void>;

abstract deleteItem(key: string): Promise<void>;
abstract get(key: string): Promise<string | null>;
abstract set(key: string, value: string): Promise<void>;
abstract delete(key: string): Promise<void>;
}
25 changes: 14 additions & 11 deletions packages/sdk-multichain/src/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ vi.mock('@metamask/multichain-api-client', () => {
};
});

type GetItem = (key: string) => string | null;
type SetItem = (key: string, value: string) => void;
type RemoveItem = (key: string) => void;
type Clear = () => void;

export type NativeStorageStub = {
platform: 'web' | 'rn' | 'node';
data: Map<string, string>;
getItem: t.Mock<(key: string) => Promise<string | null>>;
setItem: t.Mock<(key: string, value: string) => Promise<void>>;
deleteItem: t.Mock<(key: string) => Promise<void>>;
clear: t.Mock<() => void>;
getItem: t.Mock<GetItem>;
setItem: t.Mock<SetItem>;
removeItem: t.Mock<RemoveItem>;
clear: t.Mock<Clear>;
};

export type MockedData = {
Expand Down Expand Up @@ -161,18 +165,17 @@ export const createTest: CreateTestFN = ({ platform, options, createSDK, setupMo
mockMultichainClient.createSession.mockResolvedValue(mockSessionData);
mockMultichainClient.getSession.mockResolvedValue(mockSessionData);

// Create storage stub
// Create storage stub using the mocked class
nativeStorageStub = {
platform,
data: new Map<string, string>(),
getItem: vi.fn((key: string) => Promise.resolve(nativeStorageStub.data.get(key) || null)),
setItem: vi.fn((key: string, value: string) => {
getItem: t.vi.fn((key: string) => nativeStorageStub.data.get(key) || null),
setItem: t.vi.fn((key: string, value: string) => {
nativeStorageStub.data.set(key, value);
}),
deleteItem: vi.fn((key: string) => {
removeItem: t.vi.fn((key: string) => {
nativeStorageStub.data.delete(key);
}),
clear: vi.fn(() => {
clear: t.vi.fn(() => {
nativeStorageStub.data.clear();
}),
};
Expand Down
26 changes: 14 additions & 12 deletions packages/sdk-multichain/src/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ function testSuite<T extends MultiChainFNOptions>({ platform, createSDK, options
enabled: platform !== 'node',
integrationType: 'test',
},
storage: new Store(mockedData.nativeStorageStub),
storage: new Store({
platform: platform as 'web' | 'rn' | 'node',
get(key) {
return Promise.resolve(mockedData.nativeStorageStub.getItem(key));
},
set(key, value) {
return Promise.resolve(mockedData.nativeStorageStub.setItem(key, value));
},
delete(key) {
return Promise.resolve(mockedData.nativeStorageStub.removeItem(key));
},
}),
};
});

Expand Down Expand Up @@ -152,14 +163,11 @@ createTest({
...baseTestOptions,
platform: 'node',
createSDK: createMetamaskSDKNode,
setupMocks: (nativeStorageStub) => {
setupMocks: () => {
const memfs = new Map<string, any>();
t.vi.spyOn(fs, 'existsSync').mockImplementation((path) => memfs.has(path.toString()));
t.vi.spyOn(fs, 'writeFileSync').mockImplementation((path, data) => memfs.set(path.toString(), data));
t.vi.spyOn(fs, 'readFileSync').mockImplementation((path) => memfs.get(path.toString()));
t.vi.spyOn(nodeStorage, 'StoreAdapterNode').mockImplementation(() => {
return nativeStorageStub as any;
});
},
});

Expand All @@ -170,10 +178,7 @@ createTest({
setupMocks: (nativeStorageStub) => {
t.vi.spyOn(AsyncStorage, 'getItem').mockImplementation(async (key) => nativeStorageStub.getItem(key));
t.vi.spyOn(AsyncStorage, 'setItem').mockImplementation(async (key, value) => nativeStorageStub.setItem(key, value));
t.vi.spyOn(AsyncStorage, 'removeItem').mockImplementation(async (key) => nativeStorageStub.deleteItem(key));
t.vi.spyOn(rnStorage, 'StoreAdapterRN').mockImplementation(() => {
return nativeStorageStub as any;
});
t.vi.spyOn(AsyncStorage, 'removeItem').mockImplementation(async (key) => nativeStorageStub.removeItem(key));
},
});

Expand All @@ -198,8 +203,5 @@ createTest({
t.vi.stubGlobal('window', globalStub);
t.vi.stubGlobal('location', dom.window.location);
t.vi.stubGlobal('document', dom.window.document);
t.vi.spyOn(webStorage, 'StoreAdapterWeb').mockImplementation(() => {
return nativeStorageStub as any;
});
},
});
26 changes: 14 additions & 12 deletions packages/sdk-multichain/src/invoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ function testSuite<T extends MultiChainFNOptions>({ platform, createSDK, options
enabled: platform !== 'node',
integrationType: 'test',
},
storage: new Store(mockedData.nativeStorageStub),
storage: new Store({
platform: platform as 'web' | 'rn' | 'node',
get(key) {
return Promise.resolve(mockedData.nativeStorageStub.getItem(key));
},
set(key, value) {
return Promise.resolve(mockedData.nativeStorageStub.setItem(key, value));
},
delete(key) {
return Promise.resolve(mockedData.nativeStorageStub.removeItem(key));
},
}),
};
});

Expand Down Expand Up @@ -130,14 +141,11 @@ createTest({
...baseTestOptions,
platform: 'node',
createSDK: createMetamaskSDKNode,
setupMocks: (nativeStorageStub) => {
setupMocks: () => {
const memfs = new Map<string, any>();
t.vi.spyOn(fs, 'existsSync').mockImplementation((path) => memfs.has(path.toString()));
t.vi.spyOn(fs, 'writeFileSync').mockImplementation((path, data) => memfs.set(path.toString(), data));
t.vi.spyOn(fs, 'readFileSync').mockImplementation((path) => memfs.get(path.toString()));
t.vi.spyOn(nodeStorage, 'StoreAdapterNode').mockImplementation(() => {
return nativeStorageStub as any;
});
},
});

Expand All @@ -148,10 +156,7 @@ createTest({
setupMocks: (nativeStorageStub) => {
t.vi.spyOn(AsyncStorage, 'getItem').mockImplementation(async (key) => nativeStorageStub.getItem(key));
t.vi.spyOn(AsyncStorage, 'setItem').mockImplementation(async (key, value) => nativeStorageStub.setItem(key, value));
t.vi.spyOn(AsyncStorage, 'removeItem').mockImplementation(async (key) => nativeStorageStub.deleteItem(key));
t.vi.spyOn(rnStorage, 'StoreAdapterRN').mockImplementation(() => {
return nativeStorageStub as any;
});
t.vi.spyOn(AsyncStorage, 'removeItem').mockImplementation(async (key) => nativeStorageStub.removeItem(key));
},
});

Expand Down Expand Up @@ -182,8 +187,5 @@ createTest({
t.vi.stubGlobal('document', dom.window.document);
t.vi.stubGlobal('HTMLElement', dom.window.HTMLElement);
t.vi.stubGlobal('requestAnimationFrame', t.vi.fn());
t.vi.spyOn(webStorage, 'StoreAdapterWeb').mockImplementation(() => {
return nativeStorageStub as any;
});
},
});
26 changes: 14 additions & 12 deletions packages/sdk-multichain/src/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,18 @@ function testSuite<T extends MultiChainFNOptions>({ platform, createSDK, options
enabled: platform !== 'node',
integrationType: 'test',
},
storage: new Store(mockedData.nativeStorageStub),
storage: new Store({
platform: platform as 'web' | 'rn' | 'node',
get(key) {
return Promise.resolve(mockedData.nativeStorageStub.getItem(key));
},
set(key, value) {
return Promise.resolve(mockedData.nativeStorageStub.setItem(key, value));
},
delete(key) {
return Promise.resolve(mockedData.nativeStorageStub.removeItem(key));
},
}),
};
});

Expand Down Expand Up @@ -142,14 +153,11 @@ createTest({
...baseTestOptions,
platform: 'node',
createSDK: createMetamaskSDKNode,
setupMocks: (nativeStorageStub) => {
setupMocks: () => {
const memfs = new Map<string, any>();
t.vi.spyOn(fs, 'existsSync').mockImplementation((path) => memfs.has(path.toString()));
t.vi.spyOn(fs, 'writeFileSync').mockImplementation((path, data) => memfs.set(path.toString(), data));
t.vi.spyOn(fs, 'readFileSync').mockImplementation((path) => memfs.get(path.toString()));
t.vi.spyOn(nodeStorage, 'StoreAdapterNode').mockImplementation(() => {
return nativeStorageStub as any;
});
},
});

Expand All @@ -160,10 +168,7 @@ createTest({
setupMocks: (nativeStorageStub) => {
t.vi.spyOn(AsyncStorage, 'getItem').mockImplementation(async (key) => nativeStorageStub.getItem(key));
t.vi.spyOn(AsyncStorage, 'setItem').mockImplementation(async (key, value) => nativeStorageStub.setItem(key, value));
t.vi.spyOn(AsyncStorage, 'removeItem').mockImplementation(async (key) => nativeStorageStub.deleteItem(key));
t.vi.spyOn(rnStorage, 'StoreAdapterRN').mockImplementation(() => {
return nativeStorageStub as any;
});
t.vi.spyOn(AsyncStorage, 'removeItem').mockImplementation(async (key) => nativeStorageStub.removeItem(key));
},
});

Expand Down Expand Up @@ -194,8 +199,5 @@ createTest({
t.vi.stubGlobal('document', dom.window.document);
t.vi.stubGlobal('HTMLElement', dom.window.HTMLElement);
t.vi.stubGlobal('requestAnimationFrame', t.vi.fn());
t.vi.spyOn(webStorage, 'StoreAdapterWeb').mockImplementation(() => {
return nativeStorageStub as any;
});
},
});
6 changes: 3 additions & 3 deletions packages/sdk-multichain/src/store/adapters/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class StoreAdapterNode extends StoreAdapter {
}
}

async getItem(key: string): Promise<string | null> {
async get(key: string): Promise<string | null> {
if (!fs.existsSync(CONFIG_FILE)) {
return null;
}
Expand All @@ -27,7 +27,7 @@ export class StoreAdapterNode extends StoreAdapter {
return null;
}

async setItem(key: string, value: string): Promise<void> {
async set(key: string, value: string): Promise<void> {
if (!fs.existsSync(CONFIG_FILE)) {
fs.writeFileSync(CONFIG_FILE, '{}');
}
Expand All @@ -37,7 +37,7 @@ export class StoreAdapterNode extends StoreAdapter {
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
}

async deleteItem(key: string): Promise<void> {
async delete(key: string): Promise<void> {
if (!fs.existsSync(CONFIG_FILE)) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk-multichain/src/store/adapters/rn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { StoreAdapter } from '../../domain';

export class StoreAdapterRN extends StoreAdapter {
readonly platform = 'rn';
async getItem(key: string): Promise<string | null> {
async get(key: string): Promise<string | null> {
return AsyncStorage.getItem(key);
}

async setItem(key: string, value: string): Promise<void> {
async set(key: string, value: string): Promise<void> {
return AsyncStorage.setItem(key, value);
}

async deleteItem(key: string): Promise<void> {
async delete(key: string): Promise<void> {
return AsyncStorage.removeItem(key);
}
}
6 changes: 3 additions & 3 deletions packages/sdk-multichain/src/store/adapters/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export class StoreAdapterWeb extends StoreAdapter {
}
return window.localStorage;
}
async getItem(key: string): Promise<string | null> {
async get(key: string): Promise<string | null> {
return this.internal.getItem(key);
}

async setItem(key: string, value: string): Promise<void> {
async set(key: string, value: string): Promise<void> {
return this.internal.setItem(key, value);
}

async deleteItem(key: string): Promise<void> {
async delete(key: string): Promise<void> {
return this.internal.removeItem(key);
}
}
Loading
Loading