mirror of
https://github.com/fatedier/frp.git
synced 2026-07-29 02:29:18 +08:00
test(web): add frontend unit test baseline (#5451)
This commit is contained in:
73
web/frpc/test/config-field.test.ts
Normal file
73
web/frpc/test/config-field.test.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { defineComponent } from 'vue'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import ConfigField from '../src/components/ConfigField.vue'
|
||||
|
||||
const InputStub = defineComponent({
|
||||
props: ['modelValue', 'disabled', 'placeholder', 'type'],
|
||||
emits: ['update:modelValue'],
|
||||
template:
|
||||
'<input :value="modelValue ?? \'\'" :disabled="disabled" :placeholder="placeholder" :type="type === \'password\' ? \'password\' : \'text\'" @input="$emit(\'update:modelValue\', $event.target.value)" />',
|
||||
})
|
||||
|
||||
const FormItemStub = defineComponent({
|
||||
template: '<div class="form-item-stub"><slot /></div>',
|
||||
})
|
||||
|
||||
const stubs = {
|
||||
'el-form-item': FormItemStub,
|
||||
'el-input': InputStub,
|
||||
'el-switch': defineComponent({
|
||||
props: ['modelValue', 'disabled'],
|
||||
emits: ['update:modelValue'],
|
||||
template: '<button :disabled="disabled" @click="$emit(\'update:modelValue\', !modelValue)">switch</button>',
|
||||
}),
|
||||
KeyValueEditor: defineComponent({ template: '<div class="key-value-stub" />' }),
|
||||
StringListEditor: defineComponent({ template: '<div class="string-list-stub" />' }),
|
||||
PopoverMenu: defineComponent({ template: '<div class="popover-stub"><slot /></div>' }),
|
||||
PopoverMenuItem: defineComponent({ template: '<div><slot /></div>' }),
|
||||
}
|
||||
|
||||
describe('ConfigField', () => {
|
||||
it('clamps numeric input and emits the public model update', async () => {
|
||||
const wrapper = mount(ConfigField, {
|
||||
props: { label: 'Port', type: 'number', modelValue: 100, min: 1, max: 65535 },
|
||||
global: { stubs },
|
||||
})
|
||||
|
||||
const input = wrapper.find('input')
|
||||
await input.setValue('70000')
|
||||
|
||||
expect(wrapper.emitted('update:modelValue')).toContainEqual([65535])
|
||||
expect((input.element as HTMLInputElement).value).toBe('65535')
|
||||
})
|
||||
|
||||
it('keeps a leading sign as an editable draft until it becomes numeric', async () => {
|
||||
const wrapper = mount(ConfigField, {
|
||||
props: { label: 'Port', type: 'number', modelValue: 100 },
|
||||
global: { stubs },
|
||||
})
|
||||
|
||||
const input = wrapper.find('input')
|
||||
await wrapper.setProps({ modelValue: 200 })
|
||||
expect((input.element as HTMLInputElement).value).toBe('200')
|
||||
|
||||
await input.setValue('-')
|
||||
|
||||
expect(wrapper.emitted('update:modelValue')).toContainEqual([undefined])
|
||||
await wrapper.setProps({ modelValue: undefined })
|
||||
expect((input.element as HTMLInputElement).value).toBe('-')
|
||||
})
|
||||
|
||||
it('renders an empty readonly field as a disabled placeholder', () => {
|
||||
const wrapper = mount(ConfigField, {
|
||||
props: { label: 'Secret', readonly: true, modelValue: '' },
|
||||
global: { stubs },
|
||||
})
|
||||
|
||||
const input = wrapper.find('input').element as HTMLInputElement
|
||||
expect(wrapper.find('.config-field-label').text()).toBe('Secret')
|
||||
expect(input.disabled).toBe(true)
|
||||
expect(input.value).toBe('—')
|
||||
})
|
||||
})
|
||||
96
web/frpc/test/proxy-converters.test.ts
Normal file
96
web/frpc/test/proxy-converters.test.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
createDefaultProxyForm,
|
||||
createDefaultVisitorForm,
|
||||
} from '../src/types/proxy-form'
|
||||
import {
|
||||
formToStoreProxy,
|
||||
formToStoreVisitor,
|
||||
storeProxyToForm,
|
||||
} from '../src/types/proxy-converters'
|
||||
|
||||
describe('proxy converters', () => {
|
||||
it('serializes only configured proxy fields into the typed store block', () => {
|
||||
const form = createDefaultProxyForm()
|
||||
Object.assign(form, {
|
||||
name: 'web',
|
||||
type: 'http',
|
||||
enabled: false,
|
||||
localIP: '10.0.0.8',
|
||||
localPort: 8080,
|
||||
useEncryption: true,
|
||||
customDomains: ['example.com', ''],
|
||||
locations: ['/api', ''],
|
||||
metadatas: [{ key: 'team', value: 'edge' }],
|
||||
})
|
||||
|
||||
expect(formToStoreProxy(form)).toEqual({
|
||||
name: 'web',
|
||||
type: 'http',
|
||||
http: {
|
||||
enabled: false,
|
||||
localIP: '10.0.0.8',
|
||||
localPort: 8080,
|
||||
transport: { useEncryption: true },
|
||||
metadatas: { team: 'edge' },
|
||||
customDomains: ['example.com'],
|
||||
locations: ['/api'],
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it('round-trips store values while applying form defaults', () => {
|
||||
const form = storeProxyToForm({
|
||||
name: 'tcp-proxy',
|
||||
type: 'tcp',
|
||||
tcp: {
|
||||
localPort: 9000,
|
||||
customDomains: 'unused-for-tcp',
|
||||
enabled: false,
|
||||
metadatas: { owner: 'platform' },
|
||||
},
|
||||
})
|
||||
|
||||
expect(form).toMatchObject({
|
||||
name: 'tcp-proxy',
|
||||
type: 'tcp',
|
||||
enabled: false,
|
||||
localIP: '127.0.0.1',
|
||||
localPort: 9000,
|
||||
metadatas: [{ key: 'owner', value: 'platform' }],
|
||||
multiplexer: 'httpconnect',
|
||||
})
|
||||
})
|
||||
|
||||
it.each(['stcp', 'xtcp'] as const)(
|
||||
'serializes virtual_net for %s visitors',
|
||||
(type) => {
|
||||
const form = createDefaultVisitorForm()
|
||||
Object.assign(form, {
|
||||
name: 'visitor',
|
||||
type,
|
||||
pluginType: 'virtual_net',
|
||||
pluginDestinationIP: ' 192.0.2.10 ',
|
||||
bindPort: 6000,
|
||||
})
|
||||
|
||||
expect(formToStoreVisitor(form)[type]).toMatchObject({
|
||||
plugin: { type: 'virtual_net', destinationIP: '192.0.2.10' },
|
||||
bindPort: 6000,
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
it('does not serialize virtual_net for SUDP visitors', () => {
|
||||
const form = createDefaultVisitorForm()
|
||||
Object.assign(form, {
|
||||
name: 'visitor',
|
||||
type: 'sudp',
|
||||
pluginType: 'virtual_net',
|
||||
pluginDestinationIP: '192.0.2.10',
|
||||
bindPort: 6000,
|
||||
})
|
||||
|
||||
expect(formToStoreVisitor(form).sudp).toEqual({ bindPort: 6000 })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user