Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test for w3m-connect-walletconnect-widget #3657

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
61df3ef
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Nov 25, 2024
2cb1a1b
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Nov 26, 2024
d2bf2bc
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Nov 27, 2024
0bd4276
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Nov 28, 2024
a4c24da
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Nov 28, 2024
5eecce4
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Nov 29, 2024
b3d2efe
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Nov 29, 2024
0a0aeca
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 2, 2024
cf37108
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 5, 2024
34ee496
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 6, 2024
7df0378
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 10, 2024
9f1174d
wip
svenvoskamp Dec 10, 2024
c95f623
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 10, 2024
26c8ad4
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 11, 2024
ab675ab
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 12, 2024
67129c9
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 12, 2024
f33b34b
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 19, 2024
a7d921b
wip
svenvoskamp Dec 19, 2024
39b475f
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 20, 2024
c749e97
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 23, 2024
e2dacf5
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 23, 2024
38da726
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Dec 31, 2024
ca675a7
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 3, 2025
fec0681
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 6, 2025
feb309a
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 7, 2025
912ed0d
wip
svenvoskamp Jan 7, 2025
21994b1
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 9, 2025
045b5d9
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 13, 2025
1a55494
wip
svenvoskamp Jan 13, 2025
d894f7c
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 14, 2025
20c0417
wip
svenvoskamp Jan 14, 2025
aef71ea
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 14, 2025
3d1da87
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 14, 2025
2fb4235
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 15, 2025
9b81c31
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 15, 2025
02d2e7b
add test for w3m-connect-injected-widget
svenvoskamp Jan 15, 2025
64b6b19
wip
svenvoskamp Jan 15, 2025
f20577e
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 15, 2025
11c9819
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 15, 2025
11f8596
Merge branch 'main' of github.com:reown-com/appkit
svenvoskamp Jan 15, 2025
eeb1648
add test for w3m-connect-walletconnect-widget
svenvoskamp Jan 15, 2025
50ce545
format
svenvoskamp Jan 16, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { elementUpdated, fixture } from '@open-wc/testing'
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'

import { html } from 'lit'

import { ConstantsUtil as CommonConstantsUtil } from '@reown/appkit-common'
import type { ConnectorType, ConnectorWithProviders } from '@reown/appkit-core'
import {
AssetController,
ConnectorController,
CoreHelperUtil,
RouterController
} from '@reown/appkit-core'
import { ConstantsUtil } from '@reown/appkit-utils'

import { W3mConnectWalletConnectWidget } from '../../src/partials/w3m-connect-walletconnect-widget'
import { HelpersUtil } from '../utils/HelpersUtil'

// --- Constants ---------------------------------------------------- //
const WALLET_CONNECT_CONNECTOR: ConnectorWithProviders = {
id: 'walletConnect',
name: 'WalletConnect',
type: ConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT as ConnectorType,
provider: undefined,
chain: CommonConstantsUtil.CHAIN.EVM
}

const OTHER_CONNECTOR: ConnectorWithProviders = {
id: 'otherConnector',
name: 'Other Wallet',
type: ConstantsUtil.CONNECTOR_TYPE_INJECTED as ConnectorType,
provider: undefined,
chain: CommonConstantsUtil.CHAIN.EVM
}

describe('W3mConnectWalletConnectWidget', () => {
beforeAll(() => {
vi.spyOn(CoreHelperUtil, 'isMobile').mockReturnValue(false)
})

beforeEach(() => {
vi.spyOn(AssetController, 'state', 'get').mockReturnValue({
...AssetController.state,
connectorImages: {}
})
})

afterEach(() => {
vi.resetAllMocks()
})

it('should not render anything on mobile', async () => {
vi.spyOn(CoreHelperUtil, 'isMobile').mockReturnValue(true)
vi.spyOn(ConnectorController, 'state', 'get').mockReturnValue({
...ConnectorController.state,
connectors: [WALLET_CONNECT_CONNECTOR]
})

const element: W3mConnectWalletConnectWidget = await fixture(
html`<w3m-connect-walletconnect-widget></w3m-connect-walletconnect-widget>`
)

expect(element.style.display).toBe('none')
})

it('should not render if WalletConnect connector is not available', async () => {
vi.spyOn(ConnectorController, 'state', 'get').mockReturnValue({
...ConnectorController.state,
connectors: [OTHER_CONNECTOR]
})

const element: W3mConnectWalletConnectWidget = await fixture(
html`<w3m-connect-walletconnect-widget></w3m-connect-walletconnect-widget>`
)

expect(element.style.display).toBe('none')
})

it('should render WalletConnect connector with default image', async () => {
vi.spyOn(ConnectorController, 'state', 'get').mockReturnValue({
...ConnectorController.state,
connectors: [WALLET_CONNECT_CONNECTOR]
})

const element: W3mConnectWalletConnectWidget = await fixture(
html`<w3m-connect-walletconnect-widget></w3m-connect-walletconnect-widget>`
)

element.requestUpdate()
await elementUpdated(element)

const walletSelector = HelpersUtil.getByTestId(element, 'wallet-selector-walletconnect')
expect(walletSelector).not.toBeNull()
expect(walletSelector.getAttribute('name')).toBe(WALLET_CONNECT_CONNECTOR.name)
expect(walletSelector.getAttribute('tagLabel')).toBe('qr code')
expect(walletSelector.getAttribute('tagVariant')).toBe('main')
})

it('should render WalletConnect connector with custom image URL', async () => {
const connectorWithImage = {
...WALLET_CONNECT_CONNECTOR,
imageUrl: 'https://example.com/image.png'
}
vi.spyOn(ConnectorController, 'state', 'get').mockReturnValue({
...ConnectorController.state,
connectors: [connectorWithImage]
})

const element: W3mConnectWalletConnectWidget = await fixture(
html`<w3m-connect-walletconnect-widget></w3m-connect-walletconnect-widget>`
)

element.requestUpdate()
await elementUpdated(element)

const walletSelector = HelpersUtil.getByTestId(element, 'wallet-selector-walletconnect')
expect(walletSelector.getAttribute('imageSrc')).toBe(connectorWithImage.imageUrl)
})

it('should handle connector image from AssetController', async () => {
const imageId = 'walletconnect-image'
const imageUrl = 'https://example.com/asset-image.png'
const connectorWithImageId = {
...WALLET_CONNECT_CONNECTOR,
imageId
}

vi.spyOn(ConnectorController, 'state', 'get').mockReturnValue({
...ConnectorController.state,
connectors: [connectorWithImageId]
})

vi.spyOn(AssetController, 'state', 'get').mockReturnValue({
...AssetController.state,
connectorImages: { [imageId]: imageUrl }
})

const element: W3mConnectWalletConnectWidget = await fixture(
html`<w3m-connect-walletconnect-widget></w3m-connect-walletconnect-widget>`
)

element.requestUpdate()
await elementUpdated(element)

const walletSelector = HelpersUtil.getByTestId(element, 'wallet-selector-walletconnect')
expect(walletSelector.getAttribute('imageSrc')).toBe(imageUrl)
})

it('should route to ConnectingWalletConnect on click', async () => {
vi.spyOn(ConnectorController, 'state', 'get').mockReturnValue({
...ConnectorController.state,
connectors: [WALLET_CONNECT_CONNECTOR]
})
const setActiveConnectorSpy = vi.spyOn(ConnectorController, 'setActiveConnector')
const pushSpy = vi.spyOn(RouterController, 'push')

const element: W3mConnectWalletConnectWidget = await fixture(
html`<w3m-connect-walletconnect-widget></w3m-connect-walletconnect-widget>`
)

const walletSelector = HelpersUtil.getByTestId(element, 'wallet-selector-walletconnect')
walletSelector.click()

expect(setActiveConnectorSpy).toHaveBeenCalledWith(WALLET_CONNECT_CONNECTOR)
expect(pushSpy).toHaveBeenCalledWith('ConnectingWalletConnect')
})

it('should handle unknown wallet name', async () => {
const unknownConnector: ConnectorWithProviders = {
...WALLET_CONNECT_CONNECTOR,
name: undefined
} as unknown as ConnectorWithProviders

vi.spyOn(ConnectorController, 'state', 'get').mockReturnValue({
...ConnectorController.state,
connectors: [unknownConnector]
})

const element: W3mConnectWalletConnectWidget = await fixture(
html`<w3m-connect-walletconnect-widget></w3m-connect-walletconnect-widget>`
)

element.requestUpdate()
await elementUpdated(element)

const walletSelector = HelpersUtil.getByTestId(element, 'wallet-selector-walletconnect')
expect(walletSelector.getAttribute('name')).toBe('Unknown')
})
})
Loading