diff --git a/src/common-components/textbox-question/textbox-question.spec.js b/src/common-components/textbox-question/textbox-question.spec.js index d5d1de4e6..d8a29d9cd 100644 --- a/src/common-components/textbox-question/textbox-question.spec.js +++ b/src/common-components/textbox-question/textbox-question.spec.js @@ -197,4 +197,137 @@ describe("textboxQuestion.vue", () => { // Assert expect(wrapper.vm.handleChange).toHaveBeenCalled; }); + + it("Should call this.handleChange when image is submitted with valid value.", async () => { + // Arrange + const inputId = "test"; + const responseValue = "testResponse"; + + const storeMixin = { + methods: { + dispatchStoreAction: jest.fn().mockImplementation(() => { + return new Promise((resolve, reject) => + resolve({ + data: [responseValue], + }) + ); + }), + }, + }; + + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + }, + }, + propsData: { + modelValue: "", + includeCameraIcon: true, + inputId: inputId, + }, + mixins: [mockMixin, storeMixin], + attachTo: document.body, + }); + + wrapper.vm.handleChange = jest.fn().mockImplementation(() => {}); + + // Act + const imageUploadField = wrapper.find('[data-test="image-upload"]'); + + await imageUploadField.trigger("change"); + + await wrapper.vm.$nextTick(); + + // Assert + + expect(wrapper.vm.handleChange).toHaveBeenCalled(); + expect(wrapper.emitted()).toHaveProperty("update:modelValue"); + }); + + it("Should emit image-lookup-error when image is submitted with invalid value", async () => { + // Arrange + const inputId = "test"; + + const storeMixin = { + methods: { + dispatchStoreAction: jest.fn().mockImplementation(() => { + return new Promise((resolve, reject) => + resolve({ + data: [], + }) + ); + }), + }, + }; + + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + }, + }, + propsData: { + modelValue: "", + includeCameraIcon: true, + inputId: inputId, + }, + mixins: [mockMixin, storeMixin], + attachTo: document.body, + }); + + wrapper.vm.handleChange = jest.fn().mockImplementation(() => {}); + + // Act + const imageUploadField = wrapper.find('[data-test="image-upload"]'); + + await imageUploadField.trigger("change"); + + await wrapper.vm.$nextTick(); + + // Assert + expect(wrapper.vm.handleChange).not.toHaveBeenCalled(); + expect(wrapper.emitted()).toHaveProperty("imageLookupError"); + }); + + it("Should emit image-lookup-error when image endpoint responds with an error", async () => { + // Arrange + const inputId = "test"; + + const storeMixin = { + methods: { + dispatchStoreAction: jest.fn().mockImplementation(() => { + return new Promise((resolve, reject) => reject()); + }), + }, + }; + + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + }, + }, + propsData: { + modelValue: "", + includeCameraIcon: true, + inputId: inputId, + }, + mixins: [mockMixin, storeMixin], + attachTo: document.body, + }); + + wrapper.vm.handleChange = jest.fn().mockImplementation(() => {}); + + // Act + const imageUploadField = wrapper.find('[data-test="image-upload"]'); + + await imageUploadField.trigger("change"); + + await wrapper.vm.$nextTick(); + + // Assert + expect(wrapper.vm.handleChange).not.toHaveBeenCalled(); + expect(wrapper.emitted()).toHaveProperty("imageLookupError"); + }); }); diff --git a/src/common-components/textbox-question/textbox-question.vue b/src/common-components/textbox-question/textbox-question.vue index 957f59f36..41fc3a5c2 100644 --- a/src/common-components/textbox-question/textbox-question.vue +++ b/src/common-components/textbox-question/textbox-question.vue @@ -6,7 +6,12 @@ class="form-label" :class="[questionAlignment === 'center' ? 'text-center w-100 mb-5' : '']" v-html="labelText"> -