Skip to content
Open
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
122 changes: 122 additions & 0 deletions ui-vue3/src/views/traffic/routingRule/tabs/addByFormView.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { flushPromises, mount } from '@vue/test-utils'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { defineComponent, h } from 'vue'
import { createI18n } from 'vue-i18n'
import { PROVIDE_INJECT_KEY } from '@/base/enums/ProvideInject'
import { addConditionRuleAPI } from '@/api/service/traffic'
import AddByFormView from './addByFormView.vue'

vi.mock('@/api/service/traffic', () => ({
addConditionRuleAPI: vi.fn()
}))

vi.mock('vue-router', () => ({
useRouter: vi.fn()
}))

vi.mock('vue-clipboard3', () => ({
default: () => ({ toClipboard: vi.fn() })
}))

const passthrough = defineComponent({
setup(_props, { slots }) {
return () => h('div', slots.default?.())
}
})

const priorityInput = defineComponent({
props: { value: Number },
emits: ['update:value'],
setup(props, { emit }) {
return () =>
h('input', {
type: 'number',
value: props.value,
onInput: (event: Event) => {
emit('update:value', (event.target as HTMLInputElement).valueAsNumber)
}
})
}
})

function mountForm() {
return mount(AddByFormView, {
global: {
plugins: [createI18n({ legacy: false, missingWarn: false, fallbackWarn: false })],
provide: {
[PROVIDE_INJECT_KEY.TAB_LAYOUT_STATE]: {
conditionRule: {
scope: 'application',
key: 'demo-server',
conditions: ['method=sayHello => host=10.0.0.1']
}
}
},
stubs: {
RoutingRuleList: true,
'a-flex': passthrough,
'a-col': passthrough,
'a-card': passthrough,
'a-space': passthrough,
'a-row': passthrough,
'a-form': passthrough,
'a-form-item': passthrough,
'a-descriptions': true,
'a-descriptions-item': true,
'a-select': true,
'a-input': true,
'a-switch': true,
'a-input-number': priorityInput,
'a-button': passthrough
}
}
})
}

describe('condition route creation form', () => {
beforeEach(() => {
vi.clearAllMocks()
})

it('submits the entered priority', async () => {
const wrapper = mountForm()
await wrapper.get('input[type=number]').setValue('12')
await wrapper.get('[type=primary]').trigger('click')
await flushPromises()

expect(addConditionRuleAPI).toHaveBeenCalledWith(
'demo-server.condition-router',
expect.objectContaining({
conditions: ['method=sayHello => host=10.0.0.1'],
priority: 12
})
)
})

it('omits priority when the input is empty', async () => {
const wrapper = mountForm()
await wrapper.get('[type=primary]').trigger('click')
await flushPromises()

expect(addConditionRuleAPI).toHaveBeenCalledOnce()
const [, payload] = vi.mocked(addConditionRuleAPI).mock.calls[0]
expect(payload).not.toHaveProperty('priority')
})
})
4 changes: 3 additions & 1 deletion ui-vue3/src/views/traffic/routingRule/tabs/addByFormView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ const baseInfo = reactive({
enable: true,
faultTolerantProtection: false,
runtime: true,
priority: null,
priority: null as number | null,
group: ''
})

Expand Down Expand Up @@ -294,6 +294,7 @@ const addRoutingRule = async () => {
enable,
faultTolerantProtection,
runtime,
priority,
group
} = baseInfo

Expand All @@ -310,6 +311,7 @@ const addRoutingRule = async () => {
enabled: enable,
force: faultTolerantProtection,
runtime,
...(isNil(priority) ? {} : { priority }),
conditions: mergeConditions()
}
const res = await addConditionRuleAPI(ruleName, data)
Expand Down
Loading