Modified reveal password wrapper to contain test data, added tests for icon default and change of icon upon click. Preliminary test for returning focus to input field.

This commit is contained in:
Brandon Tripp 2020-11-24 15:35:35 -07:00
parent 0b03bf2fcc
commit f71fb6b07b
2 changed files with 58 additions and 13 deletions

View File

@ -1,3 +1,4 @@
import Vue from 'vue'
import LoginForm from './LoginForm.vue' import LoginForm from './LoginForm.vue'
import Styleguide from '@human-connection/styleguide' import Styleguide from '@human-connection/styleguide'
import Vuex from 'vuex' import Vuex from 'vuex'
@ -38,7 +39,7 @@ describe('LoginForm', () => {
error: jest.fn(), error: jest.fn(),
}, },
} }
return mount(LoginForm, { mocks, localVue, propsData, store, data }) return mount(LoginForm, { mocks, localVue, propsData, store })
} }
describe('fill in email and password and submit', () => { describe('fill in email and password and submit', () => {
@ -58,12 +59,43 @@ describe('LoginForm', () => {
}) })
}) })
describe('Click event', () => { describe('Click on show password input field type change', () => {
it('clicking icon shows/hides password and changes icon', () => { const wrapper = Wrapper()
wrapper.find('a.click-wrapper').trigger('click') it('does not show the password by default', () => {
expect(wrapper.data.showPassword).toEqual(!showPassword) expect(wrapper.find('input[name ="password"]').attributes('type')).toEqual('password')
}) })
it('shows the password after click on show password', async () => {
wrapper.find('span.click-wrapper').trigger('click')
await Vue.nextTick()
await expect(wrapper.find('input[name = "password"]').attributes('type')).toEqual('text')
})
})
describe('Click on show password icon, icon change', () => {
const wrapper = Wrapper()
it('shows eye icon by default', () => {
expect(wrapper.find('span.icon-wrapper').attributes('data-test')).toEqual('eye')
})
it('shows the slash-eye icon after click', async () => {
wrapper.find('span.click-wrapper').trigger('click')
await Vue.nextTick()
await expect(wrapper.find('span.icon-wrapper').attributes('data-test')).toEqual('eye-slash')
}) })
}) })
describe('Focus returns to password input container after show password click', () =>{
const wrapper = Wrapper()
const componentToGetFocus = wrapper.find('input[name ="password"]')
it('Focus is on the password field container after click', async () => {
wrapper.find('span.click-wrapper').trigger('click', {
relateTarget: componentToGetFocus
})
await Vue.nextTick()
await expect(wrapper.emitted('focus')).toBeTruthy()
})
})
})
}) })

View File

@ -20,7 +20,7 @@
name="email" name="email"
icon="envelope" icon="envelope"
/> />
<div class="password-wrapper"> <div class="password-wrapper" >
<ds-input <ds-input
v-model="form.password" v-model="form.password"
:disabled="pending" :disabled="pending"
@ -28,11 +28,14 @@
icon="lock" icon="lock"
name="password" name="password"
class="password-field" class="password-field"
ref="password"
:type="showPassword ? 'text' : 'password'" :type="showPassword ? 'text' : 'password'"
/> />
<a class="click-wrapper" @click="toggleShowPassword"> <span class="click-wrapper" @click="toggleShowPassword">
<base-icon class="toggle-icon" :name="showPassword ? 'eye-slash' : 'eye'" /> <span class="icon-wrapper" :data-test="iconName">
</a> <base-icon class="toggle-icon" :name="iconName" />
</span>
</span>
</div> </div>
<nuxt-link to="/password-reset/request"> <nuxt-link to="/password-reset/request">
{{ $t('login.forgotPassword') }} {{ $t('login.forgotPassword') }}
@ -74,6 +77,9 @@ export default {
pending() { pending() {
return this.$store.getters['auth/pending'] return this.$store.getters['auth/pending']
}, },
iconName() {
return this.showPassword ? 'eye-slash' : 'eye'
}
}, },
methods: { methods: {
async onSubmit() { async onSubmit() {
@ -89,6 +95,10 @@ export default {
toggleShowPassword(event) { toggleShowPassword(event) {
this.showPassword = !this.showPassword this.showPassword = !this.showPassword
event.preventDefault() event.preventDefault()
this.$nextTick(() => {
this.$refs.password.$el.children[1].children[1].focus()
this.$emit('focus')
})
}, },
}, },
} }
@ -126,15 +136,17 @@ export default {
outline: none; outline: none;
transition: all $duration-short $ease-out; transition: all $duration-short $ease-out;
.click-wrapper { .icon-wrapper {
padding: 8px; padding: 8px;
margin: 4px; margin: 4px;
padding-left: 16px;
color: $text-color-disabled; color: $text-color-disabled;
} }
.click-wrapper:hover { .click-wrapper:hover {
cursor: pointer; cursor: pointer;
&:focus-within { &:focus-within {
background-color: $background-color-base; background-color: $background-color-base;
border: $input-border-size solid $border-color-active; border: $input-border-size solid $border-color-active;
@ -153,10 +165,11 @@ export default {
color: $text-color-base; color: $text-color-base;
} }
} }
.password-field { .password-field {
position: relative; position: relative;
padding-top: 16px; padding-top: 16px;
padding-right: 16px; padding-right: 8px;
border: none; border: none;
border-style: none; border-style: none;
appearance: none; appearance: none;