如何为 Vuetify 的 v-alert 创建单元测试?
How can I create an unit test to an v-alert from Vuetify?
问题
Jest 正在返回整个 Vuetify 组件,我无法只获取内容
问题
如何为 Vuetify 的 v-alert 创建单元测试?
我的目标
我想获取警报的内容文本并测试此警报文本是否正确
代码
测试
fit('should show error message if username or password are not present', () => {
const { getByTestId } = renderComponent()
const alert = getByTestId('alert')
const loginButton = getByTestId('login-button')
userEvent.click(loginButton)
// const alert = screen.getByText('Insira seu Login e Senha.')
expect(alert).toBe({})
})
结果
- Expected - 1
+ Received + 22
- Object {}
+ <div
+ class="v-alert v-sheet theme--dark v-alert--dense v-alert--text error--text"
+ data-testid="alert"
+ role="alert"
+ style="display: none;"
+ >
+ <div
+ class="v-alert__wrapper"
+ >
+ <i
+ aria-hidden="true"
+ class="v-icon notranslate v-alert__icon mdi mdi-alert theme--dark error--text"
+ />
+ <div
+ class="v-alert__content"
+ >
+
+
+
+ </div>
+ </div>
+ </div>
我无法使用 testing-library
执行此操作,我使用更复杂的 vue-test-utils
执行此操作,但有效。
挑战
如果有人可以使用 testing-library
解决问题,请在此处回答。
解决方案
it('should show error message if username or password are not present', async () => {
const wrapper = mount(Form, {
localVue,
vuetify,
})
const loginButton = wrapper.find('[data-testid="login-button"]')
loginButton.trigger('click')
await Vue.nextTick()
const alertMessage = wrapper.find('.v-alert__content')
expect(alertMessage.text()).toBe('Insira seu Login e Senha.')
})
问题
Jest 正在返回整个 Vuetify 组件,我无法只获取内容
问题
如何为 Vuetify 的 v-alert 创建单元测试?
我的目标
我想获取警报的内容文本并测试此警报文本是否正确
代码
测试
fit('should show error message if username or password are not present', () => {
const { getByTestId } = renderComponent()
const alert = getByTestId('alert')
const loginButton = getByTestId('login-button')
userEvent.click(loginButton)
// const alert = screen.getByText('Insira seu Login e Senha.')
expect(alert).toBe({})
})
结果
- Expected - 1
+ Received + 22
- Object {}
+ <div
+ class="v-alert v-sheet theme--dark v-alert--dense v-alert--text error--text"
+ data-testid="alert"
+ role="alert"
+ style="display: none;"
+ >
+ <div
+ class="v-alert__wrapper"
+ >
+ <i
+ aria-hidden="true"
+ class="v-icon notranslate v-alert__icon mdi mdi-alert theme--dark error--text"
+ />
+ <div
+ class="v-alert__content"
+ >
+
+
+
+ </div>
+ </div>
+ </div>
我无法使用 testing-library
执行此操作,我使用更复杂的 vue-test-utils
执行此操作,但有效。
挑战
如果有人可以使用 testing-library
解决问题,请在此处回答。
解决方案
it('should show error message if username or password are not present', async () => {
const wrapper = mount(Form, {
localVue,
vuetify,
})
const loginButton = wrapper.find('[data-testid="login-button"]')
loginButton.trigger('click')
await Vue.nextTick()
const alertMessage = wrapper.find('.v-alert__content')
expect(alertMessage.text()).toBe('Insira seu Login e Senha.')
})