diff --git a/tests/unit/components/Form/CheckboxGroup.CheckboxItem.integration.spec.js b/tests/integration/CheckboxGroup.CheckboxItem.integration.spec.js similarity index 93% rename from tests/unit/components/Form/CheckboxGroup.CheckboxItem.integration.spec.js rename to tests/integration/CheckboxGroup.CheckboxItem.integration.spec.js index 5339f7992..a6b15419f 100644 --- a/tests/unit/components/Form/CheckboxGroup.CheckboxItem.integration.spec.js +++ b/tests/integration/CheckboxGroup.CheckboxItem.integration.spec.js @@ -32,8 +32,8 @@ xdescribe('components/Form/CheckboxGroup and components/Form/CheckboxItem integr checkboxes = subject.findAll('input[type=checkbox]'); }); - xdescribe('binding to `inputValue`', () => { - xdescribe('when `inputValue` changes', () => { + describe('binding to `inputValue`', () => { + describe('when `inputValue` changes', () => { it('the selected checkbox inputs change to match', () => { const [optionOne, optionTwo] = checkboxes.wrappers.map(wrapper => wrapper.element); @@ -59,7 +59,7 @@ xdescribe('components/Form/CheckboxGroup and components/Form/CheckboxItem integr }); }); - xdescribe('when a checkbox input is checked', () => { + describe('when a checkbox input is checked', () => { it('it emits an `input` event with an array containing the values of the checked inputs', () => { subject.setProps({ value: [] }); const [optionOne, optionTwo] = checkboxes.wrappers; @@ -80,7 +80,7 @@ xdescribe('components/Form/CheckboxGroup and components/Form/CheckboxItem integr }); }); - xdescribe('CheckboxItem element IDs', () => { + describe('CheckboxItem element IDs', () => { it('checkbox input IDs are unique', () => { const [checkboxOne, checkboxTwo] = checkboxes.wrappers; expect(checkboxOne.attributes('id')).not.toEqual(checkboxTwo.attributes('id')); diff --git a/tests/unit/components/Form/RadioGroup.RadioItem.integration.spec.js b/tests/integration/RadioGroup.RadioItem.integration.spec.js similarity index 92% rename from tests/unit/components/Form/RadioGroup.RadioItem.integration.spec.js rename to tests/integration/RadioGroup.RadioItem.integration.spec.js index 3edc18ef5..f2331aeea 100644 --- a/tests/unit/components/Form/RadioGroup.RadioItem.integration.spec.js +++ b/tests/integration/RadioGroup.RadioItem.integration.spec.js @@ -32,8 +32,8 @@ xdescribe('components/Form/RadioGroup and components/Form/RadioItem integration' radios = subject.findAll('input[type=radio]'); }); - xdescribe('binding to `inputValue`', () => { - xdescribe('when `inputValue` changes', () => { + describe('binding to `inputValue`', () => { + describe('when `inputValue` changes', () => { it('the selected radio input changes to match', () => { const [optionOne, optionTwo] = radios.wrappers.map(wrapper => wrapper.element); @@ -51,7 +51,7 @@ xdescribe('components/Form/RadioGroup and components/Form/RadioItem integration' }); }); - xdescribe('when a radio input is checked', () => { + describe('when a radio input is checked', () => { it('it emits an `input` event with the value of the checked radio', () => { const [optionOne, optionTwo] = radios.wrappers; @@ -69,7 +69,7 @@ xdescribe('components/Form/RadioGroup and components/Form/RadioItem integration' }); }); - xdescribe('RadioItem element IDs', () => { + describe('RadioItem element IDs', () => { it('radio input IDs are unique', () => { const [radioOne, radioTwo] = radios.wrappers; expect(radioOne.attributes('id')).not.toEqual(radioTwo.attributes('id')); @@ -81,7 +81,7 @@ xdescribe('components/Form/RadioGroup and components/Form/RadioItem integration' }); }); - xdescribe('radio inputs `name` attribute', () => { + describe('radio inputs `name` attribute', () => { let nameOne, nameTwo; beforeEach(() => { [nameOne, nameTwo] = radios.wrappers.map(radio => radio.attributes('name')); diff --git a/tests/unit/components/DownloadLink.spec.js b/tests/unit/components/DownloadLink.spec.js new file mode 100644 index 000000000..74376b912 --- /dev/null +++ b/tests/unit/components/DownloadLink.spec.js @@ -0,0 +1,180 @@ +import { createTestSubject } from '../helpers'; + +const mockGetDownloadURL = jest.fn() + .mockName('getDownloadURL'); +const mockRef = jest.fn() + .mockName('ref') + .mockReturnValue({ + getDownloadURL: mockGetDownloadURL, + }); + +jest.mock('@firebase/app', () => ({ + __esModule: true, + default: { + apps: [], + initializeApp: () => {}, + auth: jest.fn(), + storage: jest.fn() + .mockImplementation(() => ({ + ref: mockRef, + })), + }, + }) +); + +jest.mock('@firebase/storage', () => ({ + __esModule: true, + default: { + registerService: jest.fn(), // required by firebase/app + }, +})); + +import '@firebase/app'; +import '@firebase/storage'; + +import DownloadLink from '@/components/DownloadLink'; + +describe('components/DownloadLink', () => { + let wrapper; + const mockProps = { + exerciseId: 'mock_id', + title: 'mock title', + fileName: 'mock_name', + }; + + beforeEach(() => { + wrapper = createTestSubject(DownloadLink, { + mocks: {}, + stubs: [], + propsData: mockProps, + }); + }); + + it('renders the component', () => { + expect(wrapper.exists()).toBe(true); + }); + + describe('lifecycle hooks', () => { + xdescribe('mounted', () => { + // const localVue = createLocalVue(); + const mockGetDownloadURL = jest.fn() + .mockName('getDownloadURL'); + + const mockHref = 'mock href'; + + it('should call .getDownloadURL()', () => { + createTestSubject(DownloadLink, { + propsData: mockProps, + stubs: [], + mocks: { + getDownloadURL: mockGetDownloadURL, + }, + }); + + expect(mockGetDownloadURL).toHaveBeenCalled(); + }); + + it('should set linkHref if .getDownloadURL() returned download url', async () => { + expect.assertions(1); + + const wrapper = createTestSubject(DownloadLink, { + mocks: { + getDownloadURL: mockGetDownloadURL + .mockReturnValue(mockHref), + }, + stubs: [], + propsData: mockProps, + }); + + await wrapper.vm.$nextTick(); + expect(wrapper.vm.linkHref).toEqual(mockHref); + }); + + it('should not set linkHref if .getDownloadURL() failed', async () => { + expect.assertions(1); + + const wrapper = createTestSubject(DownloadLink, { + mocks: { + getDownloadURL: mockGetDownloadURL + .mockReturnValue(false), + }, + stubs: [], + propsData: mockProps, + }); + + await wrapper.vm.$nextTick(); + expect(wrapper.vm.linkHref).toBeEmpty(); + }); + }); + }); + + describe('methods', () => { + + xdescribe('getDownloadURL()', () => { + it('returns false if filename not set', async () => { + expect.assertions(1); + + const result = await wrapper.vm.getDownloadURL(); + + expect(result).toBe(false); + }); + + describe('with filename', () => { + const mockFileName = 'mock file name'; + const mockHref = 'mock href'; + + beforeEach(() => { + wrapper.setProps({ + fileName: mockFileName, + }); + }); + + it('calls storage().ref()', async () => { + expect.assertions(1); + + await wrapper.vm.getDownloadURL(); + + expect(mockRef).toHaveBeenCalled(); + }); + + it('calls fileRef.getDownloadURL()', async () => { + expect.assertions(1); + + await wrapper.vm.getDownloadURL(); + + expect(mockGetDownloadURL).toHaveBeenCalled(); + }); + + it('returns download url if fileRef.getDownloadURL() returned a valid response', async () => { + expect.assertions(1); + + mockGetDownloadURL.mockReturnValue(mockHref); + + const result = await wrapper.vm.getDownloadURL(); + + expect(result).toBe(mockHref); + }); + + it('returns false if fileRef.getDownloadURL() returned invalid response', async () => { + expect.assertions(1); + + mockGetDownloadURL.mockReturnValue(null); + + const result = await wrapper.vm.getDownloadURL(); + + expect(result).toBe(false); + }); + + it('returns false if fileRef.getDownloadURL() threw', async () => { + expect.assertions(1); + mockGetDownloadURL.mockImplementation(() => { + throw new Error('Error'); + }); + const result = await wrapper.vm.getDownloadURL(); + expect(result).toBe(false); + }); + }); + }); + }); +}); + diff --git a/tests/unit/components/Form/CheckboxGroup.spec.js b/tests/unit/components/Form/CheckboxGroup.spec.js index 7368b0d62..90d6ff392 100644 --- a/tests/unit/components/Form/CheckboxGroup.spec.js +++ b/tests/unit/components/Form/CheckboxGroup.spec.js @@ -1,77 +1,15 @@ -import { shallowMount } from '@vue/test-utils'; +import { createTestSubject } from '../../helpers'; import CheckboxGroup from '@/components/Form/CheckboxGroup'; -const createTestSubject = (propsData) => { - return shallowMount(CheckboxGroup, { - propsData: { - label: 'Example question', - id: 'example', - value: ['selected-checkbox-value'], - ...propsData, - }, - slots: { - default: ['CheckboxItem components'], - }, - }); -}; +describe('components/Form/CheckboxGroup', () => { -xdescribe('components/Form/CheckboxGroup', () => { it('component name is "CheckboxGroup"', () => { expect(CheckboxGroup.name).toBe('CheckboxGroup'); }); - xdescribe('properties', () => { + describe('props', () => { let prop; - /* - ** @todo these are now part of `FormField`, which this component extends. - ** Therefore we could test here that this component extends FormField - ** and move these prop tests to FormField.spec.js instead - */ - // xdescribe('label', () => { - // beforeEach(() => { - // prop = CheckboxGroup.props.label; - // }); - - // it('is optional', () => { - // expect(prop.required).not.toBe(true); - // expect(prop.default).toBe(''); - // }); - - // it('must be a String', () => { - // expect(prop.type).toBe(String); - // }); - // }); - - // xdescribe('hint', () => { - // beforeEach(() => { - // prop = CheckboxGroup.props.hint; - // }); - - // it('is optional', () => { - // expect(prop.required).not.toBe(true); - // expect(prop.default).toBe(''); - // }); - - // it('must be a String', () => { - // expect(prop.type).toBe(String); - // }); - // }); - - // xdescribe('id', () => { - // beforeEach(() => { - // prop = CheckboxGroup.props.id; - // }); - - // it('is required', () => { - // expect(prop.required).toBe(true); - // }); - - // it('must be a String', () => { - // expect(prop.type).toBe(String); - // }); - // }); - - xdescribe('value', () => { + describe('value', () => { beforeEach(() => { prop = CheckboxGroup.props.value; }); @@ -107,31 +45,48 @@ xdescribe('components/Form/CheckboxGroup', () => { expect(valid).toBe(false); }); }); + }); - xdescribe('`v-model` interface', () => { - let subject; + describe('component functions', () => { + let wrapper; + const inputMock = jest.fn(); beforeEach(() => { - subject = createTestSubject(); - }); + wrapper = createTestSubject(CheckboxGroup, { + mocks: { + emitted: { + input: inputMock, + }, + }, + stubs: [], + propsData: { + value: ['selected-checkbox-value'], + id: '', + }, + }); + }); + + describe('`v-model` interface', () => { + it('renders successfully', () => { + expect(wrapper.exists()).toBeTrue(); + }); - xdescribe('when the `value` property changes', () => { + describe('when the `value` property changes', () => { it('updates computed property `inputValue`', () => { - expect(subject.vm.inputValue).toEqual(['selected-checkbox-value']); - subject.setProps({ + wrapper.setProps({ value: ['some-other-value'], }); - expect(subject.vm.inputValue).toEqual(['some-other-value']); + expect(wrapper.vm.inputValue).toEqual(['some-other-value']); }); }); - xdescribe('when computed property `inputValue` changes', () => { + describe('when computed property `inputValue` changes', () => { it('emits an `input` event', () => { - subject.setData({ + wrapper.setData({ inputValue: ['some-new-value'], }); - const emitted = subject.emitted().input; + const emitted = wrapper.emitted().input; expect(emitted).toBeArrayOfSize(1); expect(emitted[0][0]).toEqual(['some-new-value']); @@ -139,148 +94,23 @@ xdescribe('components/Form/CheckboxGroup', () => { }); }); - xdescribe('template', () => { - let subject; - - it('the root element has the `id` attribute which was passed in as prop `id`', () => { - subject = createTestSubject(); - expect(subject.is('#example')).toBe(true); - }); - - xdescribe(' element', () => { - xdescribe('when the `label` prop is set', () => { - it('displays the label in a element', () => { - subject = createTestSubject({ - label: 'Which cakes do you like?', - }); - const legend = subject.find('legend'); - expect(legend.exists()).toBe(true); - expect(legend.text()).toBe('Which cakes do you like?'); - expect(legend.is('.govuk-fieldset__legend')).toBe(true); + describe('lifecycle hooks', () => { + describe('created', () => { + describe('if value is an array', () => { + xit('does not call emit', ()=> { + expect(inputMock).toHaveBeenCalledWith('??'); }); }); - - xdescribe('when the `label` prop is empty', () => { - it('does not render a ', () => { - subject = createTestSubject({ - label: '', - }); - const legend = subject.find('legend'); - expect(legend.exists()).toBe(false); + describe('if value is not an array', () => { + xit('emits the initial empty array value', ()=> { + wrapper.setProps({ value: undefined }); + expect(inputMock).toHaveBeenCalledWith('??'); + // expect(wrapper.emitted()).toBe('??'); }); }); - - it('is wrapped in a
', () => { - subject = createTestSubject(); - const fieldset = subject.find('fieldset'); - expect(fieldset.exists()).toBe(true); - expect(fieldset.is('.govuk-fieldset')).toBe(true); - const legend = fieldset.find('legend'); - expect(legend.exists()).toBe(true); - }); - }); - - xdescribe('hint text', () => { - xdescribe('when the `hint` prop is set', () => { - let hint; - beforeEach(() => { - subject = createTestSubject({ - label: 'Which cakes do you like?', - hint: 'Choose all the cakes you like', - id: 'liked-cakes', - }); - hint = subject.find('span.govuk-hint'); - }); - - it('displays the hint', () => { - expect(hint.exists()).toBe(true); - expect(hint.text()).toBe('Choose all the cakes you like'); - }); - - it('gives the hint element an `id` based on the main component `id`', () => { - expect(hint.attributes('id')).toBe('liked-cakes__hint'); - }); - - it('sets attribute `aria-describedby` on the
to reference the hint element `id`', () => { - const fieldset = subject.find('fieldset'); - expect(fieldset.attributes('aria-describedby')).toBe('liked-cakes__hint'); - }); - }); - - xdescribe('when the `hint` prop is not set', () => { - let hint; - beforeEach(() => { - subject = createTestSubject({ - label: 'Which cakes do you like?', - hint: undefined, - }); - hint = subject.find('span.govuk-hint'); - }); - - it('does not render the hint element', () => { - expect(hint.exists()).toBe(false); - }); - }); - }); - - xdescribe('`.govuk-checkboxes` slot container', () => { - xdescribe('if value is an array ', () => { - let slotContainer; - beforeEach(() => { - subject = createTestSubject(); - slotContainer = subject.find('.govuk-checkboxes'); - }); - - it('exists', () => { - expect(slotContainer.exists()).toBe(true); - }); - - it('renders default slot content', () => { - expect(slotContainer.text()).toBe('CheckboxItem components'); - }); - - it('is inside the
', () => { - const fieldset = subject.find('fieldset'); - expect(fieldset.find('.govuk-checkboxes').exists()).toBe(true); - }); - - }); - - xdescribe('if value is not an array ', () => { - let slotContainer; - - beforeEach(() => { - subject = createTestSubject({ value: null }); - slotContainer = subject.find('.govuk-checkboxes'); - }); - - it('exists', () => { - expect(slotContainer.exists()).toBe(true); - }); - - it('does not render slot content', () => { - expect(slotContainer.text()).toBe(''); - }); - - }); }); }); - xdescribe('lifecycle hooks', () => { - xdescribe('created', () => { - xdescribe('if value is an array', () => { - it('does not call emit', ()=> { - const array = []; - const wrapper = createTestSubject({ value: array }); - expect(wrapper.emitted().input).not.toBeTruthy(); - }); - }); - xdescribe('if value is not an array', () => { - it('emits the initial empty array value', ()=> { - const wrapper = createTestSubject({ value: undefined }); - expect(wrapper.emitted().input).toBeTruthy(); - }); - }); - }); }); + }); diff --git a/tests/unit/components/Form/CheckboxItem.spec.js b/tests/unit/components/Form/CheckboxItem.spec.js index 35df1e91e..92fd5c99d 100644 --- a/tests/unit/components/Form/CheckboxItem.spec.js +++ b/tests/unit/components/Form/CheckboxItem.spec.js @@ -1,83 +1,30 @@ -import { shallowMount } from '@vue/test-utils'; -import CheckboxGroup from '@/components/Form/CheckboxGroup'; +import { createTestSubject } from '../../helpers'; import CheckboxItem from '@/components/Form/CheckboxItem'; -const checkboxItemTemplate = ({ label, value, hint, content }) => { - let template = `${content}`; - } - else { - template += '/>'; - } - - return template; -}; - -const createTestSubject = (options) => { - if (!options) { - options = { - label: 'Example checkbox item', - value: 'example-value', - hint: 'Something', - content: 'Conditional content', - }; - } - - const checkboxItem = checkboxItemTemplate(options); - - // CheckboxItem depends on the parent component being CheckboxGroup - // So we're mocking CheckboxGroup with a CheckboxItem inside, and will return the CheckboxItem - const checkboxes = shallowMount(CheckboxGroup, { - propsData: { - label: 'Example question', - id: 'example', - value: ['selected-checkbox-value'], - }, - stubs: { - CheckboxItem, - }, - slots: { - default: [checkboxItem], - }, - }); - - return checkboxes.find(CheckboxItem); -}; - -xdescribe('components/Form/CheckboxItem', () => { +describe('components/Form/CheckboxItem', () => { it('component name is "CheckboxItem"', () => { expect(CheckboxItem.name).toBe('CheckboxItem'); }); it('throws an error if the parent component is not "CheckboxGroup"', () => { - /* eslint-disable no-console */ - // Mock console.error because Vue catches errors thrown by components and logs them to console.error - const originalConsoleError = console.error; - console.error = jest.fn(); - - const createWithBadParent = () => { - shallowMount(CheckboxItem, { + const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {}); + const BadParent = () => { + return createTestSubject(CheckboxItem, { + stubs: [], propsData: { label: 'Example checkbox item', value: 'example-value', }, }); }; - - expect(createWithBadParent).toThrow('CheckboxItem component can only be used inside a CheckboxGroup component'); - console.error = originalConsoleError; - /* eslint-enable no-console */ + expect(BadParent).toThrow('CheckboxItem component can only be used inside a CheckboxGroup component'); + expect(consoleError).toHaveBeenCalled(); + consoleError.mockRestore(); }); - xdescribe('properties', () => { + describe('props', () => { let prop; - - xdescribe('label', () => { + describe('label', () => { beforeEach(() => { prop = CheckboxItem.props.label; }); @@ -87,11 +34,11 @@ xdescribe('components/Form/CheckboxItem', () => { }); it('must be a String', () => { - expect(prop.type).toBe(String); + expect(prop.type()).toBeString(); }); }); - xdescribe('value', () => { + describe('value', () => { beforeEach(() => { prop = CheckboxItem.props.value; }); @@ -119,7 +66,7 @@ xdescribe('components/Form/CheckboxItem', () => { }); }); - xdescribe('hint', () => { + describe('hint', () => { beforeEach(() => { prop = CheckboxItem.props.hint; }); @@ -129,49 +76,46 @@ xdescribe('components/Form/CheckboxItem', () => { }); it('must be a String', () => { - expect(prop.type).toBe(String); + expect(prop.type()).toBeString(); }); }); }); - xdescribe('data', () => { - let subject; - - xdescribe('hasConditionalContent', () => { - it('is true when slot content was supplied', () => { - subject = createTestSubject({ + xdescribe('component instance', () => { + let wrapper; + beforeEach(() => { + wrapper = createTestSubject(CheckboxItem,{ + propsData: { label: 'Example checkbox item', value: ['example-value'], content: 'Conditional content', - }); - expect(subject.vm.hasConditionalContent).toBe(true); + }, + stubs: [], + }); + }); + + describe('data', () => { + describe('hasConditionalContent', () => { + it('is true when slot content was supplied', () => { + expect(wrapper.vm.hasConditionalContent).toBe(true); }); it('is false when slot content was not supplied', () => { - subject = createTestSubject({ - label: 'Example checkbox item', - value: ['example-value'], - }); - expect(subject.vm.hasConditionalContent).toBe(false); + expect(wrapper.vm.hasConditionalContent).toBe(false); }); }); }); - xdescribe('template', () => { - let subject; - beforeEach(() => { - subject = createTestSubject(); - }); - + describe('template', () => { it('renders a `.govuk-checkboxes__item` element', () => { - const item = subject.find('.govuk-checkboxes__item'); + const item = wrapper.find('.govuk-checkboxes__item'); expect(item.exists()).toBe(true); }); - xdescribe('checkbox input', () => { + describe('checkbox input', () => { let input; beforeEach(() => { - input = subject.find('input[type=checkbox]'); + input = wrapper.find('input[type=checkbox]'); }); it('renders a checkbox input', () => { @@ -183,10 +127,10 @@ xdescribe('components/Form/CheckboxItem', () => { }); }); - xdescribe('label', () => { + describe('label', () => { let label; beforeEach(() => { - label = subject.find('label'); + label = wrapper.find('label'); }); it('renders a label', () => { @@ -203,22 +147,22 @@ xdescribe('components/Form/CheckboxItem', () => { }); it('label `for` and input `id` attributes match', () => { - subject = createTestSubject(); - const input = subject.find('input[type=checkbox]'); - const label = subject.find('label'); + wrapper = createTestSubject(); + const input = wrapper.find('input[type=checkbox]'); + const label = wrapper.find('label'); expect(label.attributes('for')).toBe(input.attributes('id')); }); - xdescribe('hint', () => { + describe('hint', () => { let hint; - xdescribe('when the `hint` prop is set', () => { + describe('when the `hint` prop is set', () => { beforeEach(() => { - subject = createTestSubject({ + wrapper = createTestSubject({ label: 'My label', value: ['my-value'], hint: 'Label hint text', }); - hint = subject.find('.govuk-checkboxes__hint'); + hint = wrapper.find('.govuk-checkboxes__hint'); }); it('renders a `.govuk-checkboxes__hint` element', () => { @@ -230,18 +174,18 @@ xdescribe('components/Form/CheckboxItem', () => { }); it('sets attribute `aria-describedby` on the input to reference the hint element', () => { - const input = subject.find('input[type=checkbox]'); + const input = wrapper.find('input[type=checkbox]'); expect(input.attributes('aria-describedby')).toBe(hint.attributes('id')); }); }); - xdescribe('when the `hint` prop is not set', () => { + describe('when the `hint` prop is not set', () => { beforeEach(() => { - subject = createTestSubject({ + wrapper = createTestSubject({ label: 'My label', value: ['my-value'], }); - hint = subject.find('.govuk-checkboxes__hint'); + hint = wrapper.find('.govuk-checkboxes__hint'); }); it('does not render', () => { @@ -249,46 +193,48 @@ xdescribe('components/Form/CheckboxItem', () => { }); it('does not set attribute `aria-describedby` on the input', () => { - const input = subject.find('input[type=checkbox]'); + const input = wrapper.find('input[type=checkbox]'); expect(input.attributes()).not.toContainKey('aria-describedby'); }); }); }); - xdescribe('conditional content', () => { - xdescribe('when the checkbox is selected', () => { - xdescribe('and conditional content was given', () => { + describe('conditional content', () => { + describe('when the checkbox is selected', () => { + describe('and conditional content was given', () => { it('renders conditional content', () => { - subject = createTestSubject({ + wrapper = createTestSubject({ value: ['selected-checkbox-value'], content: 'Conditional content here', }); - const conditional = subject.find('.govuk-checkboxes__conditional'); + const conditional = wrapper.find('.govuk-checkboxes__conditional'); expect(conditional.exists()).toBe(true); }); }); - xdescribe('and conditional content was not given', () => { + describe('and conditional content was not given', () => { it('does not render conditional content', () => { - subject = createTestSubject({ + wrapper = createTestSubject({ value: ['selected-checkbox-value'], }); - const conditional = subject.find('.govuk-checkboxes__conditional'); + const conditional = wrapper.find('.govuk-checkboxes__conditional'); expect(conditional.exists()).toBe(false); }); }); }); - xdescribe('when the checkbox value is not selected', () => { + describe('when the checkbox value is not selected', () => { it('does not render conditional content', () => { - subject = createTestSubject({ + wrapper = createTestSubject({ value: ['not-selected-value'], content: 'Conditional content here', }); - const conditional = subject.find('.govuk-checkboxes__conditional'); + const conditional = wrapper.find('.govuk-checkboxes__conditional'); expect(conditional.exists()).toBe(false); }); }); }); }); + }); + }); diff --git a/tests/unit/components/Form/Currency.spec.js b/tests/unit/components/Form/Currency.spec.js index 75d81fa7f..82f3cd92a 100644 --- a/tests/unit/components/Form/Currency.spec.js +++ b/tests/unit/components/Form/Currency.spec.js @@ -1,33 +1,79 @@ -import { shallowMount } from '@vue/test-utils'; +import { createTestSubject } from '../../helpers'; import Currency from '@/components/Form/Currency'; -xdescribe('components/Form/Currency', () => { +describe('components/Form/Currency', () => { + describe('props', () => { + describe('value', () => { + let prop; + beforeEach(() => { + prop = Currency.props.value; + }); + it('is not required', () => { + expect(prop.required).toBeFalsy(); + }); + it('defaults as null', () => { + expect(prop.default).toBe(null); + }); + it('is a string', () => { + expect(prop.type).toBeString; + }); + }); + }); + + describe('component instance', () => { let wrapper; + const mockProps = { + id: 'mockId', + }; beforeEach(() => { - wrapper = shallowMount(Currency); + wrapper = createTestSubject(Currency, { + propsData: mockProps, + stubs: ['FormFieldError'], + }); }); - + it('renders the component', () => { expect(wrapper.exists()).toBe(true); }); - xdescribe('template', () => { - xdescribe('label', () => { - it('sets the label to the value of the `label` prop', () => { - wrapper.setProps({ label: 'My Form Label' }); - expect(wrapper.find('label').text()).toBe('My Form Label'); + describe('template', () => { + + describe('containing div', () => { + describe('with errorMessage', () => { + it('has gov-uk-form-group--error class', () => { + wrapper.setData({ errorMessage: 'test error' }); + expect(wrapper.find('div').attributes('class')).toContain('govuk-form-group--error'); + }); + }); + describe('without errorMessage', () => { + it('doesn\'t have gov-uk-form-group--error class', () => { + wrapper.setData({ errorMessage: '' }); + expect(wrapper.find('div').attributes('class')).not.toContain('govuk-form-group--error'); + }); }); }); - }); - xdescribe('hint', () => { - let hint; - xdescribe('when the prop is set', () => { + describe('label', () => { + it('sets the label to the value of the `label` prop', () => { + wrapper.setProps({ label: 'My Form Label' }); + expect(wrapper.find('label').text()).toBe('My Form Label'); + }); + it('id sets `for` attribute', () => { + wrapper.setProps({ id: 'my_unique_key' }); + expect(wrapper.find('label').attributes().for).toBe('my_unique_key'); + }); + it('has govuk-heading-m govuk-!-margin-bottom-2 classes', () => { + expect(wrapper.find('label').attributes('class')).toBe('govuk-heading-m govuk-!-margin-bottom-2'); + }); + }); + + describe('hint span', () => { + let hint; + describe('when the prop is set', () => { beforeEach(() => { wrapper.setProps({ hint: 'my_hint' }); hint = wrapper.find('.govuk-hint'); }); - it('shows a hint', () => { expect(hint.exists()).toBe(true); }); @@ -35,43 +81,86 @@ xdescribe('components/Form/Currency', () => { expect(hint.text()).toBe('my_hint'); }); }); - - xdescribe('when the prop is not set', () => { + describe('when the prop is not set', () => { beforeEach(() => { hint = wrapper.find('.govuk-hint'); }); - it('does not show hint', () => { expect(hint.exists()).toBe(false); }); }); }); - xdescribe('id', () => { - it('sets