import { flushPromises } from '@vue/test-utils' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { resetAuth } from '@/composables/useAuth' import RegisterStatusView from '@/views/RegisterStatusView.vue' import RegisterView from '@/views/RegisterView.vue' import { jsonResponse, makeRouter, mockFetch, mountWithPlugins } from './helpers' beforeEach(resetAuth) afterEach(() => { vi.unstubAllGlobals() history.replaceState(null, '') }) const PASSWORD = 'un-mot-de-passe-solide' async function fill(wrapper: ReturnType, overrides = {}) { const values = { '#first_name': 'Marie', '#last_name': 'Curie', '#email': 'marie@exemple.be', '#inami': '', '#password': PASSWORD, '#confirm': PASSWORD, ...overrides, } for (const [selector, value] of Object.entries(values)) { await wrapper.find(selector).setValue(value) } await wrapper.find('#terms').setValue(true) } describe('RegisterView', () => { it('envoie la demande puis affiche « Vérifiez votre boîte mail » sans e-mail dans l’URL', async () => { const fetch = mockFetch(jsonResponse(201)) const router = makeRouter() const push = vi.spyOn(router, 'push').mockResolvedValue(undefined) const wrapper = mountWithPlugins(RegisterView, { router }) await fill(wrapper) await wrapper.find('form').trigger('submit') await flushPromises() expect(JSON.parse(fetch.mock.calls[0]![1]!.body as string)).toEqual({ first_name: 'Marie', last_name: 'Curie', email: 'marie@exemple.be', inami_number: '', password: PASSWORD, accept_terms: true, }) expect(push).toHaveBeenCalledWith({ name: 'register-status', state: { email: 'marie@exemple.be' }, }) }) it('bloque l’envoi si les mots de passe diffèrent, sont trop courts ou sans conditions', async () => { const wrapper = mountWithPlugins(RegisterView) const button = () => wrapper.find('button[type="submit"]') await fill(wrapper, { '#confirm': 'autre-mot-de-passe' }) expect(button().attributes('disabled')).toBeDefined() expect(wrapper.find('#confirm').classes()).toContain('is-invalid') await fill(wrapper, { '#password': 'court', '#confirm': 'court' }) expect(button().attributes('disabled')).toBeDefined() await fill(wrapper) expect(button().attributes('disabled')).toBeUndefined() await wrapper.find('#terms').setValue(false) expect(button().attributes('disabled')).toBeDefined() }) it('affiche les erreurs de validation du serveur sous chaque champ', async () => { mockFetch( jsonResponse(400, { password: ['Ce mot de passe est trop courant.'], email: ['Saisissez une adresse e-mail valide.'], }), ) const wrapper = mountWithPlugins(RegisterView) await fill(wrapper) await wrapper.find('form').trigger('submit') await flushPromises() expect(wrapper.find('[role="alert"]').text()).toBe('Corrigez les champs signalés.') expect(wrapper.find('#password').classes()).toContain('is-invalid') expect(wrapper.text()).toContain('Ce mot de passe est trop courant.') expect(wrapper.text()).toContain('Saisissez une adresse e-mail valide.') }) }) describe('RegisterStatusView', () => { async function mountAt(path: string, state?: Record) { const router = makeRouter() await router.push(router.resolve(path)).catch(() => {}) if (state) history.replaceState(state, '') const wrapper = mountWithPlugins(RegisterStatusView, { router }) await flushPromises() return { wrapper, router } } it('après inscription : « Vérifiez votre boîte mail » et renvoi par e-mail', async () => { const fetch = mockFetch(jsonResponse(202)) const { wrapper } = await mountAt('/inscription/statut', { email: 'marie@exemple.be' }) expect(wrapper.find('h1').text()).toBe('Vérifiez votre boîte mail') expect(wrapper.text()).toContain('marie@exemple.be') await wrapper.find('button').trigger('click') await flushPromises() expect(JSON.parse(fetch.mock.calls[0]![1]!.body as string)).toEqual({ email: 'marie@exemple.be', }) expect(wrapper.find('button').attributes('disabled')).toBeDefined() expect(wrapper.text()).toContain('un nouvel e-mail vient d’être envoyé'.replace('’', "'")) }) it('lien valide : adresse confirmée et jeton retiré de l’URL', async () => { const fetch = mockFetch(jsonResponse(200, { status: 'approval_pending' })) const { wrapper, router } = await mountAt('/inscription/statut?jeton=abc') expect(JSON.parse(fetch.mock.calls[0]![1]!.body as string)).toEqual({ token: 'abc' }) expect(wrapper.find('h1').text()).toBe('Adresse confirmée') expect(router.currentRoute.value.fullPath).toBe('/inscription/statut') }) it('lien expiré : renvoi à partir du jeton', async () => { const fetch = mockFetch( jsonResponse(400, { code: 'expired_token', detail: 'Ce lien a expiré.' }), jsonResponse(202), ) const { wrapper } = await mountAt('/inscription/statut?jeton=vieux') expect(wrapper.find('h1').text()).toBe('Lien expiré') await wrapper.find('button').trigger('click') await flushPromises() expect(JSON.parse(fetch.mock.calls[1]![1]!.body as string)).toEqual({ token: 'vieux' }) }) it.each([ ['invalid_token', 'Lien invalide'], ['used_token', 'Adresse confirmée'], ])('%s → « %s »', async (code, title) => { mockFetch(jsonResponse(400, { code, detail: '' })) const { wrapper } = await mountAt('/inscription/statut?jeton=x') expect(wrapper.find('h1').text()).toBe(title) }) })