-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(bridge-ui): mobile issues (#13927)
- Loading branch information
1 parent
f4f77dc
commit 2cb5125
Showing
20 changed files
with
321 additions
and
276 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
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
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
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
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
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
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
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,88 @@ | ||
import { ethers } from 'ethers'; | ||
|
||
import { | ||
getInjectedSigner, | ||
hasInjectedProvider, | ||
rpcCall, | ||
} from './injectedProvider'; | ||
|
||
jest.mock('../constants/envVars'); | ||
|
||
jest.mock('ethers', () => { | ||
const Web3Provider = jest.fn(); | ||
Web3Provider.prototype = { | ||
getSigner: jest.fn(), | ||
send: jest.fn(), | ||
}; | ||
|
||
return { | ||
ethers: { | ||
providers: { | ||
Web3Provider, | ||
}, | ||
}, | ||
}; | ||
}); | ||
|
||
beforeEach(() => { | ||
globalThis.ethereum = { | ||
isMetaMask: true, | ||
request: jest.fn(), | ||
}; | ||
|
||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('injectedProvider - rpcCall', () => { | ||
it('should call rpc method', async () => { | ||
jest | ||
.mocked(ethers.providers.Web3Provider.prototype.send) | ||
.mockResolvedValueOnce('test value'); | ||
|
||
const result = await rpcCall('eth_requestAccounts'); | ||
|
||
expect(result).toEqual('test value'); | ||
expect(ethers.providers.Web3Provider.prototype.send).toHaveBeenCalledWith( | ||
'eth_requestAccounts', | ||
undefined, | ||
); | ||
}); | ||
|
||
it('should throw error if rpc method fails', async () => { | ||
jest | ||
.mocked(ethers.providers.Web3Provider.prototype.send) | ||
.mockRejectedValue(new Error('test error')); | ||
|
||
await expect(rpcCall('eth_requestAccounts')).rejects.toThrowError( | ||
'RPC call "eth_requestAccounts" failed', | ||
); | ||
}); | ||
}); | ||
|
||
describe('injectedProvider - getInjectedSigner', () => { | ||
it('should return signer', () => { | ||
const mockSigner = {} as ethers.providers.JsonRpcSigner; | ||
jest | ||
.mocked(ethers.providers.Web3Provider.prototype.getSigner) | ||
.mockReturnValue(mockSigner); | ||
|
||
expect(getInjectedSigner()).toEqual(mockSigner); | ||
|
||
expect(ethers.providers.Web3Provider).toHaveBeenCalledWith( | ||
globalThis.ethereum, | ||
'any', | ||
); | ||
}); | ||
}); | ||
|
||
describe('injectedProvider - hasInjectedProvider', () => { | ||
it('should return true if injected provider is available', () => { | ||
expect(hasInjectedProvider()).toBeTruthy(); | ||
}); | ||
|
||
it('should return false if injected provider is not available', () => { | ||
globalThis.ethereum = undefined; | ||
|
||
expect(hasInjectedProvider()).toBeFalsy(); | ||
}); | ||
}); |
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
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,25 @@ | ||
type MobileOS = 'Windows' | 'Android' | 'iOS' | null; | ||
|
||
function getMobileOS(): MobileOS { | ||
const { userAgent } = navigator; | ||
|
||
// Windows Phone must come first because its UA might contain "Android" | ||
if (/windows phone/i.test(userAgent)) { | ||
return 'Windows'; | ||
} | ||
|
||
if (/android/i.test(userAgent)) { | ||
return 'Android'; | ||
} | ||
|
||
if (/ipad|iphone|ipod/i.test(userAgent)) { | ||
return 'iOS'; | ||
} | ||
|
||
return null; // unknown or simply not a mobile | ||
} | ||
|
||
// This includes tablets | ||
export function isMobileDevice() { | ||
return ['Windows', 'Android', 'iOS'].includes(getMobileOS()); | ||
} |
Oops, something went wrong.