feat: allow refreshing QR code

This commit is contained in:
Phan An 2024-06-16 18:01:58 +02:00
parent 3da777dbb2
commit eef2cfa0e0
3 changed files with 36 additions and 13 deletions

View file

@ -1,17 +1,30 @@
import { expect, it, vi } from 'vitest' import { expect, it, vi } from 'vitest'
import UnitTestCase from '@/__tests__/UnitTestCase' import UnitTestCase from '@/__tests__/UnitTestCase'
import { authService } from '@/services' import { authService } from '@/services'
import QRLogin from '@/components/profile-preferences/QRLogin.vue' import { screen } from '@testing-library/vue'
import Component from './QRLogin.vue'
new class extends UnitTestCase { new class extends UnitTestCase {
protected beforeEach () {
vi.mock('@vueuse/integrations/useQRCode', () => ({
useQRCode: () => 'data:image/png;base64,my-qr-code'
}))
}
protected test () { protected test () {
it('renders', async () => { it('renders', async () => {
vi.mock('@vueuse/integrations/useQRCode', () => ({
useQRCode: () => 'data:image/png;base64,my-qr-code'
}))
const getTokenMock = this.mock(authService, 'getOneTimeToken').mockResolvedValue('my-token') const getTokenMock = this.mock(authService, 'getOneTimeToken').mockResolvedValue('my-token')
const { html } = this.render(QRLogin) const { html } = this.render(Component)
expect(getTokenMock).toHaveBeenCalled()
expect(html()).toMatchSnapshot()
})
it('refreshes QR code', async () => {
const getTokenMock = this.mock(authService, 'getOneTimeToken').mockResolvedValue('my-token')
const { html } = this.render(Component)
await this.user.click(screen.getByRole('button', { name: 'Refresh now' }))
expect(getTokenMock).toHaveBeenCalled() expect(getTokenMock).toHaveBeenCalled()
expect(html()).toMatchSnapshot() expect(html()).toMatchSnapshot()

View file

@ -1,9 +1,10 @@
<template> <template>
<article class="text-k-text-secondary"> <article class="text-k-text-secondary">
Instead of using a password, you can scan the QR code below to log in to Instead of using a password, you can scan the QR code below to log in to
<a class="text-k-highlight" href="https://koel.dev/#mobile" target="_blank">Koel Player</a> <a href="https://koel.dev/#mobile" target="_blank">Koel Player</a>
on your mobile device.<br> on your mobile device.<br>
The QR code will refresh every 10 minutes. The QR code will refresh every 10 minutes.
<a role="button" @click.prevent="resetOneTimeToken">Refresh now</a>
<img :src="qrCodeUrl" alt="QR Code" class="mt-4 rounded-4" height="192" width="192"> <img :src="qrCodeUrl" alt="QR Code" class="mt-4 rounded-4" height="192" width="192">
</article> </article>
</template> </template>
@ -29,10 +30,17 @@ const qrCodeUrl = useQRCode(qrCodeData, {
height: window.devicePixelRatio === 1 ? 196 : 384 height: window.devicePixelRatio === 1 ? 196 : 384
}) })
const resetOneTimeToken = async () => (oneTimeToken.value = await authService.getOneTimeToken()) let oneTimeTokenTimeout: number | null = null
onMounted(() => { const resetOneTimeToken = async () => {
window.setInterval(resetOneTimeToken, 60 * 10 * 1000) oneTimeToken.value = await authService.getOneTimeToken()
resetOneTimeToken()
}) if (oneTimeTokenTimeout) {
window.clearTimeout(oneTimeTokenTimeout)
}
oneTimeTokenTimeout = window.setTimeout(resetOneTimeToken, 60 * 10 * 1000)
}
onMounted(() => resetOneTimeToken())
</script> </script>

View file

@ -1,3 +1,5 @@
// Vitest Snapshot v1 // Vitest Snapshot v1
exports[`renders 1`] = `<article class="text-k-text-secondary"> Instead of using a password, you can scan the QR code below to log in to <a class="text-k-highlight" href="https://koel.dev/#mobile" target="_blank">Koel Player</a> on your mobile device.<br> The QR code will refresh every 10 minutes. <img src="data:image/png;base64,my-qr-code" alt="QR Code" class="mt-4 rounded-4" height="192" width="192"></article>`; exports[`refreshes QR code 1`] = `<article class="text-k-text-secondary"> Instead of using a password, you can scan the QR code below to log in to <a href="https://koel.dev/#mobile" target="_blank">Koel Player</a> on your mobile device.<br> The QR code will refresh every 10 minutes. <a role="button">Refresh now</a><img src="data:image/png;base64,my-qr-code" alt="QR Code" class="mt-4 rounded-4" height="192" width="192"></article>`;
exports[`renders 1`] = `<article class="text-k-text-secondary"> Instead of using a password, you can scan the QR code below to log in to <a href="https://koel.dev/#mobile" target="_blank">Koel Player</a> on your mobile device.<br> The QR code will refresh every 10 minutes. <a role="button">Refresh now</a><img src="data:image/png;base64,my-qr-code" alt="QR Code" class="mt-4 rounded-4" height="192" width="192"></article>`;