-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add frontend test around custom inputs.
- Loading branch information
1 parent
528b106
commit 22737ca
Showing
7 changed files
with
322 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
training-front-end/src/components/__tests__/ValidatedDatepicker.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { mount } from '@vue/test-utils'; | ||
import ValidatedDatepicker from '../ValidatedDatepicker.vue'; | ||
|
||
describe('ValidatedDatepicker', () => { | ||
it('renders properly', async () => { | ||
const wrapper = mount(ValidatedDatepicker, { | ||
props: { | ||
modelValue: '', | ||
validator: { | ||
$error: false, | ||
$errors: [] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label' | ||
} | ||
}); | ||
|
||
expect(wrapper.html()).toContain('Month'); | ||
expect(wrapper.html()).toContain('Day'); | ||
expect(wrapper.html()).toContain('Year'); | ||
}); | ||
|
||
it('displays errors when validator has errors', async () => { | ||
const wrapper = mount(ValidatedDatepicker, { | ||
props: { | ||
modelValue: '', | ||
validator: { | ||
$error: true, | ||
$errors: [{ $property: 'name', $message: 'Name is required' }] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label' | ||
} | ||
}); | ||
|
||
expect(wrapper.find('.usa-error-message').text()).toBe('Name is required'); | ||
}); | ||
|
||
it('emits update:modelValue event on input', async () => { | ||
const wrapper = mount(ValidatedDatepicker, { | ||
props: { | ||
modelValue: '', | ||
validator: { | ||
$error: false, | ||
$errors: [] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label' | ||
} | ||
}); | ||
|
||
const monthInput = wrapper.get('#testName-month'); | ||
const dayInput = wrapper.get('#testName-day'); | ||
const yearInput = wrapper.get('#testName-year'); | ||
|
||
await monthInput.setValue('1'); //zero index 1 = february | ||
await dayInput.setValue('15'); | ||
await yearInput.setValue('2023'); | ||
|
||
expect(wrapper.emitted()).toHaveProperty('update:modelValue'); | ||
expect(wrapper.emitted()['update:modelValue'][0]).toEqual([new Date('2023-02-15')]); | ||
}); | ||
|
||
it('updates user_input when modelValue changes', async () => { | ||
const wrapper = mount(ValidatedDatepicker, { | ||
props: { | ||
modelValue: '', | ||
validator: { | ||
$error: false, | ||
$errors: [] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label' | ||
} | ||
}); | ||
|
||
await wrapper.vm.$nextTick(); | ||
|
||
// Simulate user input | ||
const monthInput = wrapper.get('#testName-month'); | ||
const dayInput = wrapper.get('#testName-day'); | ||
const yearInput = wrapper.get('#testName-year'); | ||
|
||
// Simulate modelValue change | ||
await wrapper.setProps({ | ||
modelValue: new Date('2024-02-20') | ||
}); | ||
|
||
await wrapper.vm.$nextTick(); | ||
|
||
// Ensure user_input is updated | ||
expect(monthInput.element.value).toBe('1'); | ||
expect(dayInput.element.value).toBe('20'); | ||
expect(yearInput.element.value).toBe('2024'); | ||
|
||
// Simulate modelValue change to undefined | ||
await wrapper.setProps({ | ||
modelValue: undefined | ||
}); | ||
|
||
await wrapper.vm.$nextTick(); | ||
|
||
// Ensure user_input is cleared | ||
expect(monthInput.element.value).toBe(''); | ||
expect(dayInput.element.value).toBe(''); | ||
expect(yearInput.element.value).toBe(''); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
training-front-end/src/components/__tests__/ValidatedInput.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { mount } from '@vue/test-utils'; | ||
import ValidatedInput from '../ValidatedInput.vue'; | ||
|
||
describe('ValidatedInput', () => { | ||
it('renders properly', () => { | ||
const wrapper = mount(ValidatedInput, { | ||
props: { | ||
modelValue: '', | ||
validator: { | ||
$error: false, | ||
$errors: [] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label' | ||
} | ||
}) | ||
|
||
expect(wrapper.find('.usa-form-group').exists()).toBe(true) | ||
expect(wrapper.find('.usa-label').text()).toBe('Test Label (*Required)') | ||
expect(wrapper.find('input').exists()).toBe(true) | ||
}) | ||
|
||
it('displays errors when validator has errors', async () => { | ||
const wrapper = mount(ValidatedInput, { | ||
props: { | ||
modelValue: '', | ||
validator: { | ||
$error: true, | ||
$errors: [{ $property: 'name', $message: 'Name is required' }] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label' | ||
} | ||
}) | ||
|
||
expect(wrapper.find('.usa-input--error').exists()).toBe(true) | ||
expect(wrapper.find('.usa-error-message').text()).toBe('Name is required') | ||
}) | ||
|
||
it('emits update:modelValue event on input', async () => { | ||
const wrapper = mount(ValidatedInput, { | ||
props: { | ||
modelValue: '', | ||
validator: { | ||
$error: false, | ||
$errors: [] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label' | ||
} | ||
}) | ||
|
||
const textarea = wrapper.find('input') | ||
await textarea.setValue('new value') | ||
|
||
expect(wrapper.emitted('update:modelValue')).toBeTruthy() | ||
expect(wrapper.emitted('update:modelValue')[0]).toEqual(['new value']) | ||
}) | ||
}) |
75 changes: 75 additions & 0 deletions
75
training-front-end/src/components/__tests__/ValidatedSelect.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { mount } from '@vue/test-utils'; | ||
import ValidatedSelect from '../ValidatedSelect.vue'; | ||
|
||
describe('ValidatedSelect', () => { | ||
it('renders properly', () => { | ||
const wrapper = mount(ValidatedSelect, { | ||
props: { | ||
modelValue: '', | ||
validator: { | ||
$error: false, | ||
$errors: [] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label', | ||
options: [ | ||
{ id: 1, name: 'Option 1' }, | ||
{ id: 2, name: 'Option 2' }, | ||
{ id: 3, name: 'Option 3' } | ||
] | ||
} | ||
}) | ||
|
||
expect(wrapper.find('.usa-form-group').exists()).toBe(true) | ||
expect(wrapper.find('.usa-label').text()).toBe('Test Label (*Required)') | ||
expect(wrapper.findAll('option').length).toBe(4) // 3 options + default option | ||
}) | ||
|
||
it('displays errors when validator has errors', async () => { | ||
const wrapper = mount(ValidatedSelect, { | ||
props: { | ||
modelValue: '', | ||
validator: { | ||
$error: true, | ||
$errors: [{ $property: 'name', $message: 'Name is required' }] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label', | ||
options: [ | ||
{ id: 1, name: 'Option 1' }, | ||
{ id: 2, name: 'Option 2' }, | ||
{ id: 3, name: 'Option 3' } | ||
] | ||
} | ||
}) | ||
|
||
expect(wrapper.find('.usa-form-group--error').exists()).toBe(true) | ||
expect(wrapper.find('.usa-error-message').text()).toBe('Name is required') | ||
}) | ||
|
||
it('emits update:modelValue event on input', async () => { | ||
const wrapper = mount(ValidatedSelect, { | ||
props: { | ||
modelValue: '', | ||
validator: { | ||
$error: false, | ||
$errors: [] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label', | ||
options: [ | ||
{ id: 1, name: 'Option 1' }, | ||
{ id: 2, name: 'Option 2' }, | ||
{ id: 3, name: 'Option 3' } | ||
] | ||
} | ||
}) | ||
|
||
const select = wrapper.find('select') | ||
await select.setValue('1') | ||
|
||
expect(wrapper.emitted('update:modelValue')).toBeTruthy() | ||
expect(wrapper.emitted('update:modelValue')[0]).toEqual(['1']) | ||
}) | ||
}) |
60 changes: 60 additions & 0 deletions
60
training-front-end/src/components/__tests__/ValidatedTextArea.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { mount } from "@vue/test-utils"; | ||
import ValidatedTextArea from "../ValidatedTextArea.vue"; | ||
|
||
describe('ValidatedTextArea', () => { | ||
it('renders properly', () => { | ||
const wrapper = mount(ValidatedTextArea, { | ||
props: { | ||
modelValue: 'initial value', | ||
validator: { | ||
$error: false, | ||
$errors: [] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label' | ||
} | ||
}) | ||
|
||
expect(wrapper.find('.usa-form-group').exists()).toBe(true) | ||
expect(wrapper.find('.usa-label').text()).toBe('Test Label (*Required)') | ||
expect(wrapper.find('textarea').exists()).toBe(true) | ||
}) | ||
|
||
it('displays errors when validator has errors', async () => { | ||
const wrapper = mount(ValidatedTextArea, { | ||
props: { | ||
modelValue: 'initial value', | ||
validator: { | ||
$error: true, | ||
$errors: [{ $property: 'name', $message: 'Name is required' }] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label' | ||
} | ||
}) | ||
|
||
expect(wrapper.find('.usa-input--error').exists()).toBe(true) | ||
expect(wrapper.find('.usa-error-message').text()).toBe('Name is required') | ||
}) | ||
|
||
it('emits update:modelValue event on input', async () => { | ||
const wrapper = mount(ValidatedTextArea, { | ||
props: { | ||
modelValue: 'initial value', | ||
validator: { | ||
$error: false, | ||
$errors: [] | ||
}, | ||
name: 'testName', | ||
label: 'Test Label' | ||
} | ||
}) | ||
|
||
const textarea = wrapper.find('textarea') | ||
await textarea.setValue('new value') | ||
|
||
expect(wrapper.emitted('update:modelValue')).toBeTruthy() | ||
expect(wrapper.emitted('update:modelValue')[0]).toEqual(['new value']) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const setup = () => { | ||
process.env.TZ = 'UTC' | ||
} |