|
| 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 | +// Shared email for tests that need to use the same email |
| 8 | +const sharedEmail = faker.internet.email(); |
| 9 | + |
| 10 | +test( |
| 11 | + 'Successfully subscribes a user to the newsletter', |
| 12 | + { tag: [TAGS.writesData] }, |
| 13 | + async ({ page, subscribe }) => { |
| 14 | + const t = await getTranslations('Components.Subscribe'); |
| 15 | + |
| 16 | + await page.goto('/'); |
| 17 | + await page.waitForLoadState('networkidle'); |
| 18 | + |
| 19 | + const email = sharedEmail; |
| 20 | + |
| 21 | + const emailInput = page.getByPlaceholder(t('placeholder')); |
| 22 | + |
| 23 | + await emailInput.fill(email); |
| 24 | + |
| 25 | + const submitButton = page.locator('input[type="email"]').locator('..').getByRole('button'); |
| 26 | + |
| 27 | + await submitButton.click(); |
| 28 | + await page.waitForLoadState('networkidle'); |
| 29 | + |
| 30 | + await expect(page.getByText(t('success'))).toBeVisible(); |
| 31 | + |
| 32 | + subscribe.trackSubscription(email); |
| 33 | + }, |
| 34 | +); |
| 35 | + |
| 36 | +test('Shows error when email is invalid', async ({ page }) => { |
| 37 | + const t = await getTranslations('Components.Subscribe'); |
| 38 | + |
| 39 | + await page.goto('/'); |
| 40 | + await page.waitForLoadState('networkidle'); |
| 41 | + |
| 42 | + const invalidEmail = 'not-an-email'; |
| 43 | + |
| 44 | + const emailInput = page.getByPlaceholder(t('placeholder')); |
| 45 | + |
| 46 | + await emailInput.fill(invalidEmail); |
| 47 | + |
| 48 | + const submitButton = page.locator('input[type="email"]').locator('..').getByRole('button'); |
| 49 | + |
| 50 | + await submitButton.click(); |
| 51 | + await page.waitForLoadState('networkidle'); |
| 52 | + |
| 53 | + await expect(page.getByText(t('Errors.invalidEmail'))).toBeVisible(); |
| 54 | +}); |
| 55 | + |
| 56 | +test('Shows error when user tries to subscribe again with the same email', async ({ |
| 57 | + page, |
| 58 | + subscribe, |
| 59 | +}) => { |
| 60 | + const t = await getTranslations('Components.Subscribe'); |
| 61 | + |
| 62 | + await page.goto('/'); |
| 63 | + await page.waitForLoadState('networkidle'); |
| 64 | + |
| 65 | + const email = sharedEmail; |
| 66 | + |
| 67 | + const emailInput = page.getByPlaceholder(t('placeholder')); |
| 68 | + const submitButton = page.locator('input[type="email"]').locator('..').getByRole('button'); |
| 69 | + |
| 70 | + // Try to subscribe again with the same email (already subscribed in first test) |
| 71 | + await emailInput.fill(email); |
| 72 | + await submitButton.click(); |
| 73 | + await page.waitForLoadState('networkidle'); |
| 74 | + |
| 75 | + await expect(page.getByText(t('Errors.subcriberAlreadyExists'))).toBeVisible(); |
| 76 | + |
| 77 | + // Track that we attempted to subscribe this email (already tracked in first test) |
| 78 | + subscribe.trackSubscription(email); |
| 79 | +}); |
0 commit comments