mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-11 13:02:22 +08:00
remove useless test
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
import { useSyncExternalStore } from 'react'
|
||||
import { afterEach, expect, it, vi } from 'vitest'
|
||||
|
||||
vi.mock('dayjs', () => ({ default: () => ({ from: () => '' }) }))
|
||||
vi.mock('react', () => ({
|
||||
memo: (component: unknown) => component,
|
||||
useSyncExternalStore: vi.fn((_subscribe, getSnapshot) => getSnapshot()),
|
||||
}))
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('keeps at most one interval while visible and none while hidden or unsubscribed', async () => {
|
||||
vi.useFakeTimers()
|
||||
vi.stubGlobal('window', globalThis)
|
||||
const visibility = Object.assign(new EventTarget(), { hidden: true })
|
||||
vi.stubGlobal('document', visibility)
|
||||
const { RelativeTime } = await import('./connection-relative-time')
|
||||
RelativeTime({ start: '' })
|
||||
const [subscribe] = vi.mocked(useSyncExternalStore).mock.calls[0]
|
||||
const unsubscribeFirst = subscribe(() => {})
|
||||
const unsubscribeSecond = subscribe(() => {})
|
||||
expect(vi.getTimerCount()).toBe(0)
|
||||
visibility.hidden = false
|
||||
visibility.dispatchEvent(new Event('visibilitychange'))
|
||||
expect(vi.getTimerCount()).toBe(1)
|
||||
visibility.hidden = true
|
||||
visibility.dispatchEvent(new Event('visibilitychange'))
|
||||
expect(vi.getTimerCount()).toBe(0)
|
||||
visibility.hidden = false
|
||||
visibility.dispatchEvent(new Event('visibilitychange'))
|
||||
expect(vi.getTimerCount()).toBe(1)
|
||||
unsubscribeFirst()
|
||||
unsubscribeSecond()
|
||||
expect(vi.getTimerCount()).toBe(0)
|
||||
visibility.dispatchEvent(new Event('visibilitychange'))
|
||||
expect(vi.getTimerCount()).toBe(0)
|
||||
})
|
||||
@@ -1,75 +0,0 @@
|
||||
import { createElement } from 'react'
|
||||
import { renderToStaticMarkup } from 'react-dom/server'
|
||||
import { expect, it, vi } from 'vitest'
|
||||
|
||||
import { formatConnectionTraffic } from './connection-row-view'
|
||||
import { ConnectionTable } from './connection-table'
|
||||
|
||||
const sorting = vi.hoisted(() => ({ id: 'download', desc: false }))
|
||||
|
||||
vi.mock('react', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('react')>()),
|
||||
useState: (initial: unknown) => [
|
||||
initial === null ? sorting : initial,
|
||||
vi.fn(),
|
||||
],
|
||||
}))
|
||||
vi.mock('foxact/use-local-storage', () => ({
|
||||
useLocalStorage: (_key: string, initial: unknown) => [initial, vi.fn()],
|
||||
}))
|
||||
vi.mock('react-i18next', () => ({
|
||||
useTranslation: () => ({ t: (key: string) => key }),
|
||||
}))
|
||||
vi.mock('./connection-column-manager', () => ({
|
||||
ConnectionColumnManager: () => null,
|
||||
}))
|
||||
vi.mock('./connection-relative-time', () => ({
|
||||
RelativeTime: () => null,
|
||||
}))
|
||||
vi.mock('./connection-row-view', async (importOriginal) => {
|
||||
const original =
|
||||
await importOriginal<typeof import('./connection-row-view')>()
|
||||
return {
|
||||
...original,
|
||||
formatConnectionTraffic: vi.fn(original.formatConnectionTraffic),
|
||||
}
|
||||
})
|
||||
|
||||
it('formats only virtual rows when sorting numeric columns above the cache bound', () => {
|
||||
const connections: IConnectionsItem[] = Array.from(
|
||||
{ length: 2_101 },
|
||||
(_, i) => ({
|
||||
id: `${i}`,
|
||||
metadata: {
|
||||
network: 'tcp',
|
||||
type: 'HTTP',
|
||||
host: `host-${i}`,
|
||||
sourceIP: '',
|
||||
sourcePort: '',
|
||||
destinationPort: '443',
|
||||
},
|
||||
chains: [],
|
||||
rule: '',
|
||||
rulePayload: '',
|
||||
start: '',
|
||||
upload: (i * 3) % 2_101,
|
||||
download: (i * 5) % 2_101,
|
||||
curUpload: (i * 7) % 2_101,
|
||||
curDownload: (i * 11) % 2_101,
|
||||
}),
|
||||
)
|
||||
|
||||
for (const column of ['download', 'upload', 'dlSpeed', 'ulSpeed']) {
|
||||
sorting.id = column
|
||||
vi.mocked(formatConnectionTraffic).mockClear()
|
||||
renderToStaticMarkup(
|
||||
createElement(ConnectionTable, {
|
||||
connections,
|
||||
onShowDetail: vi.fn(),
|
||||
columnManagerOpen: false,
|
||||
onCloseColumnManager: vi.fn(),
|
||||
}),
|
||||
)
|
||||
expect(formatConnectionTraffic).toHaveBeenCalledTimes(24)
|
||||
}
|
||||
})
|
||||
@@ -1,79 +0,0 @@
|
||||
import { useSyncExternalStore } from 'react'
|
||||
import { afterEach, expect, it, vi } from 'vitest'
|
||||
|
||||
const socket = vi.hoisted(() => ({
|
||||
addListener:
|
||||
vi.fn<
|
||||
(listener: (message: { type: string; data: string }) => void) => void
|
||||
>(),
|
||||
close: async () => {},
|
||||
}))
|
||||
|
||||
vi.mock('react', () => ({
|
||||
useCallback: (callback: unknown) => callback,
|
||||
useMemo: (factory: () => unknown) => factory(),
|
||||
useSyncExternalStore: vi.fn((_subscribe, getSnapshot) => getSnapshot()),
|
||||
}))
|
||||
vi.mock('tauri-plugin-mihomo-api', () => ({
|
||||
MihomoWebSocket: { connect_connections: async () => socket },
|
||||
}))
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('reuses unchanged snapshot and list references without notifying subscribers', async () => {
|
||||
vi.useFakeTimers()
|
||||
vi.setSystemTime(10_000)
|
||||
vi.stubGlobal('window', globalThis)
|
||||
const { useConnectionData: readConnectionData } = await import(
|
||||
'./use-connection-data'
|
||||
)
|
||||
readConnectionData()
|
||||
const [subscribe, read] = vi
|
||||
.mocked(
|
||||
useSyncExternalStore<
|
||||
ReturnType<typeof readConnectionData>['response']['data']
|
||||
>,
|
||||
)
|
||||
.mock.calls.at(-1)!
|
||||
const listener = vi.fn()
|
||||
const unsubscribe = subscribe(listener)
|
||||
await Promise.resolve()
|
||||
const onMessage = socket.addListener.mock.calls[0][0]
|
||||
const connections: IConnectionsItem[] = [
|
||||
{
|
||||
id: 'a',
|
||||
metadata: {
|
||||
network: 'tcp',
|
||||
type: 'HTTP',
|
||||
host: 'example.com',
|
||||
sourceIP: '',
|
||||
sourcePort: '',
|
||||
destinationPort: '443',
|
||||
},
|
||||
chains: [],
|
||||
rule: '',
|
||||
rulePayload: '',
|
||||
start: '',
|
||||
upload: 0,
|
||||
download: 0,
|
||||
},
|
||||
]
|
||||
const send = (uploadTotal = 0) => {
|
||||
vi.advanceTimersByTime(500)
|
||||
onMessage({
|
||||
type: 'Text',
|
||||
data: JSON.stringify({ connections, uploadTotal, downloadTotal: 0 }),
|
||||
})
|
||||
return read()
|
||||
}
|
||||
const first = send()
|
||||
expect(send()).toBe(first)
|
||||
expect(listener).toHaveBeenCalledTimes(1)
|
||||
const totalsOnly = send(100)
|
||||
expect(totalsOnly.activeConnections).toBe(first.activeConnections)
|
||||
expect(totalsOnly.closedConnections).toBe(first.closedConnections)
|
||||
unsubscribe()
|
||||
})
|
||||
Reference in New Issue
Block a user