diff --git a/ui-vue3/src/views/traffic/routingRule/tabs/addByFormView.spec.ts b/ui-vue3/src/views/traffic/routingRule/tabs/addByFormView.spec.ts new file mode 100644 index 000000000..d4b3c75bd --- /dev/null +++ b/ui-vue3/src/views/traffic/routingRule/tabs/addByFormView.spec.ts @@ -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') + }) +}) diff --git a/ui-vue3/src/views/traffic/routingRule/tabs/addByFormView.vue b/ui-vue3/src/views/traffic/routingRule/tabs/addByFormView.vue index a2f1f5a85..97657285d 100644 --- a/ui-vue3/src/views/traffic/routingRule/tabs/addByFormView.vue +++ b/ui-vue3/src/views/traffic/routingRule/tabs/addByFormView.vue @@ -225,7 +225,7 @@ const baseInfo = reactive({ enable: true, faultTolerantProtection: false, runtime: true, - priority: null, + priority: null as number | null, group: '' }) @@ -294,6 +294,7 @@ const addRoutingRule = async () => { enable, faultTolerantProtection, runtime, + priority, group } = baseInfo @@ -310,6 +311,7 @@ const addRoutingRule = async () => { enabled: enable, force: faultTolerantProtection, runtime, + ...(isNil(priority) ? {} : { priority }), conditions: mergeConditions() } const res = await addConditionRuleAPI(ruleName, data)