From 33275b82d22e2913eaaf436932c5f4e9bd78f74a Mon Sep 17 00:00:00 2001 From: ly5156 <361112237@qq.com> Date: Wed, 8 Jan 2025 12:10:49 +0800 Subject: [PATCH] test(Networking): add hostAliases config unit test case --- shell/components/form/Networking.vue | 1 + .../form/__tests__/Networking.test.ts | 120 ++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 shell/components/form/__tests__/Networking.test.ts diff --git a/shell/components/form/Networking.vue b/shell/components/form/Networking.vue index a272ef0c4b9..db973c9622b 100644 --- a/shell/components/form/Networking.vue +++ b/shell/components/form/Networking.vue @@ -252,6 +252,7 @@ export default {
jest.fn((fn) => fn)); + +describe('component: Networking', () => { + it.each([ + { + ip: '10.1.1.1', + hostnames: ['host1'] + }, + { + ip: '10.1.1.2', + hostnames: ['host1', 'host2'] + }, + { + ip: '10.1.1.3', + hostnames: [] + } + ])('should render host aliases form if the value contains host aliases config: [%j]', (hostAliasesConfig) => { + const wrapper = mount(Networking, { + props: { + value: { hostAliases: [hostAliasesConfig] }, + mode: 'create', + }, + global: { mocks: { $store: { getters: { 'i18n/t': jest.fn() } } } } + }); + + const hostAliases = wrapper.findComponent('[data-test="hostAliases"]'); + const keyInput = hostAliases.find('[data-testid="input-kv-item-key-0"]'); + const valueInput = hostAliases.find('[data-testid="kv-item-value-0"]'); + + expect(keyInput.element.value).toBe(hostAliasesConfig.ip); + expect(valueInput.find('[data-testid="value-multiline"]').element.value).toBe(hostAliasesConfig.hostnames.join(', ')); + }); + + it('should render host aliases form correctly if click add host aliases button', async() => { + const wrapper = mount(Networking, { + props: { + value: {}, + 'onUpdate:value': (e) => wrapper.setProps({ value: e }), + mode: 'create', + }, + global: { mocks: { $store: { getters: { 'i18n/t': jest.fn() } } } } + }); + + const hostAliases = wrapper.findComponent('[data-test="hostAliases"]'); + const addButton = hostAliases.find('[data-testid="add_link_button"]'); + let keyInput = hostAliases.find('[data-testid="input-kv-item-key-0"]'); + let valueInput = hostAliases.find('[data-testid="kv-item-value-0"]'); + + expect(keyInput.exists()).toBe(false); + expect(valueInput.exists()).toBe(false); + expect(wrapper.props('value')).toStrictEqual({}); + addButton.trigger('click'); + await nextTick(); + + keyInput = hostAliases.find('[data-testid="input-kv-item-key-0"]'); + valueInput = hostAliases.find('[data-testid="kv-item-value-0"]'); + + expect(keyInput.exists()).toBe(true); + expect(valueInput.exists()).toBe(true); + await nextTick(); + + const v = wrapper.props('value'); + + expect(v.hostAliases).toHaveLength(1); + expect(v.hostAliases[0].ip).toBe(''); + expect(v.hostAliases[0].hostnames).toStrictEqual([]); + + keyInput = hostAliases.find('[data-testid="input-kv-item-key-0"]'); + valueInput = hostAliases.find('[data-testid="kv-item-value-0"]'); + + expect(keyInput.exists()).toBe(true); + expect(valueInput.exists()).toBe(true); + }); + + it('should render host aliases form correctly if modify the form values', async() => { + const wrapper = mount(Networking, { + props: { + value: {}, + 'onUpdate:value': (e) => wrapper.setProps({ value: e }), + mode: 'create', + }, + global: { mocks: { $store: { getters: { 'i18n/t': jest.fn() } } } } + }); + + const hostAliases = wrapper.findComponent('[data-test="hostAliases"]'); + const addButton = hostAliases.find('[data-testid="add_link_button"]'); + + addButton.trigger('click'); + await nextTick(); + let v = wrapper.props('value'); + + expect(v.hostAliases).toHaveLength(1); + expect(v.hostAliases[0].ip).toBe(''); + expect(v.hostAliases[0].hostnames).toStrictEqual([]); + + let keyInput = hostAliases.find('[data-testid="input-kv-item-key-0"]'); + let valueInput = hostAliases.find('[data-testid="kv-item-value-0"]'); + + expect(keyInput.exists()).toBe(true); + expect(valueInput.exists()).toBe(true); + + keyInput.setValue('10.1.1.1'); + valueInput.find('[data-testid="value-multiline"]').setValue('test1, test2'); + await nextTick(); + v = wrapper.props('value'); + + expect(v.hostAliases).toHaveLength(1); + expect(v.hostAliases[0].ip).toBe('10.1.1.1'); + expect(v.hostAliases[0].hostnames).toStrictEqual(['test1', 'test2']); + + keyInput = hostAliases.find('[data-testid="input-kv-item-key-0"]'); + valueInput = hostAliases.find('[data-testid="kv-item-value-0"]'); + expect(keyInput.exists()).toBe(true); + expect(valueInput.exists()).toBe(true); + }); +});