|
| 1 | +import { faker } from '@faker-js/faker'; |
| 2 | + |
| 3 | +import { expect, test } from '~/tests/fixtures'; |
| 4 | +import { getTranslations } from '~/tests/lib/i18n'; |
| 5 | +import { TAGS } from '~/tests/tags'; |
| 6 | + |
| 7 | +test( |
| 8 | + 'Successfully subscribes a user to the newsletter', |
| 9 | + { tag: [TAGS.writesData] }, |
| 10 | + async ({ page }) => { |
| 11 | + const t = await getTranslations('Components.Subscribe'); |
| 12 | + |
| 13 | + await page.goto('/'); |
| 14 | + await page.waitForLoadState('networkidle'); |
| 15 | + |
| 16 | + const email = faker.internet.email(); |
| 17 | + |
| 18 | + const emailInput = page.getByPlaceholder(t('placeholder')); |
| 19 | + |
| 20 | + await emailInput.fill(email); |
| 21 | + |
| 22 | + const submitButton = page.locator('input[type="email"]').locator('..').getByRole('button'); |
| 23 | + |
| 24 | + await submitButton.click(); |
| 25 | + await page.waitForLoadState('networkidle'); |
| 26 | + |
| 27 | + await expect(page.getByText(t('success'))).toBeVisible(); |
| 28 | + }, |
| 29 | +); |
| 30 | + |
| 31 | +test('Shows error when email is invalid', async ({ page }) => { |
| 32 | + const t = await getTranslations('Components.Subscribe'); |
| 33 | + |
| 34 | + await page.goto('/'); |
| 35 | + await page.waitForLoadState('networkidle'); |
| 36 | + |
| 37 | + const invalidEmail = 'not-an-email'; |
| 38 | + |
| 39 | + const emailInput = page.getByPlaceholder(t('placeholder')); |
| 40 | + |
| 41 | + await emailInput.fill(invalidEmail); |
| 42 | + |
| 43 | + const submitButton = page.locator('input[type="email"]').locator('..').getByRole('button'); |
| 44 | + |
| 45 | + await submitButton.click(); |
| 46 | + await page.waitForLoadState('networkidle'); |
| 47 | + |
| 48 | + await expect(page.getByText(t('Errors.invalidEmail'))).toBeVisible(); |
| 49 | +}); |
| 50 | + |
| 51 | +test( |
| 52 | + 'Shows error when user tries to subscribe again with the same email', |
| 53 | + { tag: [TAGS.writesData] }, |
| 54 | + async ({ page }) => { |
| 55 | + const t = await getTranslations('Components.Subscribe'); |
| 56 | + |
| 57 | + await page.goto('/'); |
| 58 | + await page.waitForLoadState('networkidle'); |
| 59 | + |
| 60 | + const email = faker.internet.email(); |
| 61 | + |
| 62 | + const emailInput = page.getByPlaceholder(t('placeholder')); |
| 63 | + const submitButton = page.locator('input[type="email"]').locator('..').getByRole('button'); |
| 64 | + |
| 65 | + // First subscription - should succeed |
| 66 | + await emailInput.fill(email); |
| 67 | + await submitButton.click(); |
| 68 | + await page.waitForLoadState('networkidle'); |
| 69 | + |
| 70 | + await expect(page.getByText(t('success'))).toBeVisible(); |
| 71 | + |
| 72 | + // Try to subscribe again with the same email |
| 73 | + await emailInput.fill(email); |
| 74 | + await submitButton.click(); |
| 75 | + await page.waitForLoadState('networkidle'); |
| 76 | + |
| 77 | + await expect(page.getByText(t('Errors.subcriberAlreadyExists'))).toBeVisible(); |
| 78 | + }, |
| 79 | +); |
0 commit comments