|
| 1 | +import { createLocalVue, shallowMount } from '@vue/test-utils'; |
| 2 | + |
| 3 | +import { formService } from '@/services'; |
| 4 | +import Form from '@/views/Form.vue'; |
| 5 | + |
| 6 | +const localVue = createLocalVue(); |
| 7 | + |
| 8 | +describe('Form.vue', () => { |
| 9 | + const mockConsoleError = jest.spyOn(console, 'error'); |
| 10 | + const readFormSpy = jest.spyOn(formService, 'readForm'); |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + mockConsoleError.mockReset(); |
| 14 | + readFormSpy.mockReset(); |
| 15 | + }); |
| 16 | + |
| 17 | + afterAll(() => { |
| 18 | + mockConsoleError.mockRestore(); |
| 19 | + readFormSpy.mockRestore(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('renders without formId', () => { |
| 23 | + const wrapper = shallowMount(Form, { |
| 24 | + localVue, |
| 25 | + stubs: ['router-view'], |
| 26 | + }); |
| 27 | + |
| 28 | + expect(wrapper.html()).toMatch('router-view'); |
| 29 | + expect(readFormSpy).toHaveBeenCalledTimes(0); |
| 30 | + }); |
| 31 | + |
| 32 | + it('renders with formId correctly', async () => { |
| 33 | + const name = 'test'; |
| 34 | + readFormSpy.mockImplementation(() => ({ data: { name: name } })); |
| 35 | + const wrapper = shallowMount(Form, { |
| 36 | + localVue, |
| 37 | + propsData: { formId: '123' }, |
| 38 | + stubs: ['router-view'], |
| 39 | + }); |
| 40 | + await localVue.nextTick(); |
| 41 | + |
| 42 | + expect(wrapper.html()).toMatch('router-view'); |
| 43 | + expect(readFormSpy).toHaveBeenCalledTimes(1); |
| 44 | + expect(wrapper.vm.formName).toMatch(name); |
| 45 | + }); |
| 46 | + |
| 47 | + it('renders with formId and logs error', async () => { |
| 48 | + readFormSpy.mockImplementation(() => { |
| 49 | + throw new Error('error'); |
| 50 | + }); |
| 51 | + const wrapper = shallowMount(Form, { |
| 52 | + localVue, |
| 53 | + propsData: { formId: '123' }, |
| 54 | + stubs: ['router-view'], |
| 55 | + }); |
| 56 | + await localVue.nextTick(); |
| 57 | + |
| 58 | + expect(wrapper.html()).toMatch('router-view'); |
| 59 | + expect(readFormSpy).toHaveBeenCalledTimes(1); |
| 60 | + expect(wrapper.vm.formName).toMatch(''); |
| 61 | + }); |
| 62 | +}); |
0 commit comments