From 2aa4f373459c83602cd90aca07a24848759088ba Mon Sep 17 00:00:00 2001 From: Kulbhushan Kaushik Date: Thu, 20 Oct 2022 08:08:11 -0400 Subject: [PATCH] commit --- .../textbox-question/textbox-question.spec.js | 170 ++++++++++++++++ .../textbox-question/textbox-question.vue | 189 ++++++++++++++++++ 2 files changed, 359 insertions(+) create mode 100644 src/common-components/textbox-question/textbox-question.spec.js create mode 100644 src/common-components/textbox-question/textbox-question.vue diff --git a/src/common-components/textbox-question/textbox-question.spec.js b/src/common-components/textbox-question/textbox-question.spec.js new file mode 100644 index 00000000..b830b978 --- /dev/null +++ b/src/common-components/textbox-question/textbox-question.spec.js @@ -0,0 +1,170 @@ +import { shallowMount } from "@vue/test-utils"; +import textboxQuestion from "./textbox-question"; + +// Mock CMS content +const questionText = "Question Text"; +const mockMixin = { + methods: { + getCmsContent: jest.fn().mockImplementation(()=> { + return questionText; + }) + } +} +const maska = jest.fn(); + +describe("textboxQuestion.vue", () => { + + it("Should render a text input", async () => { + // Arrange + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + } + }, + mixins: [mockMixin] + }); + + wrapper.getCmsContent = jest.fn(); + + // Act + const input = wrapper.find("input"); + + // Assert + expect(input.exists()).toBe(true); + + }); + + it("Should render the 'questionText' data value as the label text when disableAutoFill is false.", async () => { + // Arrange + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + } + }, + mixins: [mockMixin] + }); + + // Act + const label = wrapper.find("label"); + + // Assert + expect(label.text()).toContain(questionText); + + }); + + it("Should return input id as the id of the input field", async () => { + // Arrange + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + } + }, + propsData: { + inputId: "input ID", + }, + mixins: [mockMixin] + }); + + // Act + const input = wrapper.find("input"); + + // Assert + expect(input.attributes().id).toEqual("input ID"); + + }); + + it("Should render the 'questionText' data value as the aria-label attribute.", async () => { + + // Arrange + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + } + }, + mixins: [mockMixin] + }); + + // Act + const label = wrapper.find("label"); + + // Assert + expect(label.attributes("aria-label")).toContain(questionText); + + }); + + it("Should return aria-disabled state as disabled", async () => { + // Arrange + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + } + }, + propsData: { + isDisabled: true, + }, + mixins: [mockMixin] + }); + + // Assert + const input = wrapper.find("input"); + + // Expect + expect(input.attributes("aria-disabled")).toEqual("true"); + + }); + + it("Should emit new value when modelValue is changed", async () => { + // Act + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + } + }, + propsData: { + modelValue: "val", + }, + mixins: [mockMixin] + }); + + await wrapper.find("input").setValue("val2"); + + // Assert + expect(wrapper.emitted()).toHaveProperty('change') + + }); + + it("Should call this.handleChange with new value when the value is changed and the new value is valid", async () => { + // Arrange + const wrapper = shallowMount(textboxQuestion, { + global: { + directives: { + maska: maska, + } + }, + propsData: { + options: {}, + modelValue: "foo", + }, + mixins: [mockMixin] + }); + + wrapper.vm.handleChange = jest.fn().mockImplementation(() => {}); + wrapper.vm.validate = jest.fn().mockImplementation(() => { + return true; + }); + + // Act + wrapper.vm.$options.watch.value.call(wrapper.vm, "bar"); + + // Assert + expect(wrapper.vm.handleChange).toHaveBeenCalled; + + }); + +}); diff --git a/src/common-components/textbox-question/textbox-question.vue b/src/common-components/textbox-question/textbox-question.vue new file mode 100644 index 00000000..f0a8bcc2 --- /dev/null +++ b/src/common-components/textbox-question/textbox-question.vue @@ -0,0 +1,189 @@ + + + + +