From eef2cfa0e0fa8de96314cbbb51810baaee3a6256 Mon Sep 17 00:00:00 2001 From: Phan An Date: Sun, 16 Jun 2024 18:01:58 +0200 Subject: [PATCH] feat: allow refreshing QR code --- .../profile-preferences/QRLogin.spec.ts | 25 ++++++++++++++----- .../profile-preferences/QRLogin.vue | 20 ++++++++++----- .../__snapshots__/QRLogin.spec.ts.snap | 4 ++- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/resources/assets/js/components/profile-preferences/QRLogin.spec.ts b/resources/assets/js/components/profile-preferences/QRLogin.spec.ts index d50e18be..568ce08b 100644 --- a/resources/assets/js/components/profile-preferences/QRLogin.spec.ts +++ b/resources/assets/js/components/profile-preferences/QRLogin.spec.ts @@ -1,17 +1,30 @@ import { expect, it, vi } from 'vitest' import UnitTestCase from '@/__tests__/UnitTestCase' 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 { + protected beforeEach () { + vi.mock('@vueuse/integrations/useQRCode', () => ({ + useQRCode: () => 'data:image/png;base64,my-qr-code' + })) + } + protected test () { 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 { 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(html()).toMatchSnapshot() diff --git a/resources/assets/js/components/profile-preferences/QRLogin.vue b/resources/assets/js/components/profile-preferences/QRLogin.vue index d0d8e6f8..b960006f 100644 --- a/resources/assets/js/components/profile-preferences/QRLogin.vue +++ b/resources/assets/js/components/profile-preferences/QRLogin.vue @@ -1,9 +1,10 @@ @@ -29,10 +30,17 @@ const qrCodeUrl = useQRCode(qrCodeData, { height: window.devicePixelRatio === 1 ? 196 : 384 }) -const resetOneTimeToken = async () => (oneTimeToken.value = await authService.getOneTimeToken()) +let oneTimeTokenTimeout: number | null = null -onMounted(() => { - window.setInterval(resetOneTimeToken, 60 * 10 * 1000) - resetOneTimeToken() -}) +const resetOneTimeToken = async () => { + oneTimeToken.value = await authService.getOneTimeToken() + + if (oneTimeTokenTimeout) { + window.clearTimeout(oneTimeTokenTimeout) + } + + oneTimeTokenTimeout = window.setTimeout(resetOneTimeToken, 60 * 10 * 1000) +} + +onMounted(() => resetOneTimeToken()) diff --git a/resources/assets/js/components/profile-preferences/__snapshots__/QRLogin.spec.ts.snap b/resources/assets/js/components/profile-preferences/__snapshots__/QRLogin.spec.ts.snap index c2f640f4..2164a048 100644 --- a/resources/assets/js/components/profile-preferences/__snapshots__/QRLogin.spec.ts.snap +++ b/resources/assets/js/components/profile-preferences/__snapshots__/QRLogin.spec.ts.snap @@ -1,3 +1,5 @@ // Vitest Snapshot v1 -exports[`renders 1`] = `
Instead of using a password, you can scan the QR code below to log in to Koel Player on your mobile device.
The QR code will refresh every 10 minutes. QR Code
`; +exports[`refreshes QR code 1`] = `
Instead of using a password, you can scan the QR code below to log in to Koel Player on your mobile device.
The QR code will refresh every 10 minutes. Refresh nowQR Code
`; + +exports[`renders 1`] = `
Instead of using a password, you can scan the QR code below to log in to Koel Player on your mobile device.
The QR code will refresh every 10 minutes. Refresh nowQR Code
`;