Skip to content

Commit

Permalink
add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JVGS1111 committed Aug 9, 2024
1 parent a41cf26 commit 8e99f4c
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts

.env
.env
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20
FROM node:20-bookworm

WORKDIR /app

Expand All @@ -8,6 +8,8 @@ RUN npm install

COPY . .

RUN npx -y [email protected] install --with-deps

RUN npm run build

CMD ["npm", "run", "dev"]
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ services:
container_name: news-aggregator
ports:
- '3000:3000'
volumes:
- .:/app
- /app/node_modules

playwright:
build:
context: .
dockerfile: Dockerfile
container_name: playwright-tests
ports:
- '3333:3333'
depends_on:
- app
command: npx playwright test --ui-port=3333 --ui-host=0.0.0.0
environment:
- NODE_ENV=test
volumes:
- .:/app
- /app/node_modules
60 changes: 60 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@playwright/test": "^1.46.0",
"@rocketseat/eslint-config": "^2.2.2",
"@testing-library/jest-dom": "^6.4.8",
"@testing-library/react": "^16.0.0",
Expand Down
24 changes: 24 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defineConfig, devices } from '@playwright/test'

export default defineConfig({
testDir: './test',
testMatch: /.*\.e2e-spec\.ts$/,
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
use: {
baseURL: 'http://localhost:3000',
},
webServer: {
command: 'npm run dev',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
},
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
})
5 changes: 4 additions & 1 deletion src/components/news-feed/news-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ interface NewsCardProps {

export function NewsCard({ article, number }: NewsCardProps) {
return (
<article className="group flex cursor-pointer flex-row justify-start gap-2">
<article
className="group flex cursor-pointer flex-row justify-start gap-2"
data-testid="article-card"
>
{number && (
<div className="min-w-6">
<span className="text-lg text-title">{number}.</span>
Expand Down
48 changes: 48 additions & 0 deletions test/custom-news-feed.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { test, expect } from '@playwright/test'

test('Access the custom news feed page', async ({ page }) => {
await page.goto('/custom-news-feed', { waitUntil: 'networkidle' })
const header = page.getByRole('img', { name: 'News Aggregator' })
expect(header).toBeVisible()
})

test('Access preferences tab', async ({ page }) => {
await page.goto('/custom-news-feed', { waitUntil: 'networkidle' })
const prefTabBtn = page.getByRole('button', { name: 'Preferences' })
await prefTabBtn.click()

expect(
page.getByRole('heading', { name: 'Edit your article preferences' }),
).toBeVisible()
})

test('Add author preference', async ({ page }) => {
await page.goto('/custom-news-feed', { waitUntil: 'networkidle' })
const prefTabBtn = page.getByRole('button', { name: 'Preferences' })
await prefTabBtn.click()
const authorInput = page.getByPlaceholder('Add your authors')
const addAuthorBtn = page.getByTestId('add-author')
await authorInput.fill('Test')
await addAuthorBtn.click()

const badger = page.getByTestId('author-badger')
badger.isVisible()
expect(await badger.isVisible()).toBe(true)
})

test('Change category and source preference', async ({ page }) => {
await page.goto('/custom-news-feed', { waitUntil: 'networkidle' })
const prefTabBtn = page.getByRole('button', { name: 'Preferences' })
await prefTabBtn.click()

const categoryChk = page.getByText('World')
const sourceChk = page.getByText('The Guardian')
let savePrefBtn = page.getByRole('button', { name: 'Save' })

await categoryChk.check()
await sourceChk.check()
expect(await savePrefBtn.isEnabled()).toBe(true)
await savePrefBtn.click()
savePrefBtn = page.getByRole('button', { name: 'Save' })
expect(await savePrefBtn.isDisabled()).toBe(true)
})
79 changes: 79 additions & 0 deletions test/home.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { test, expect } from '@playwright/test'

test('Access the home page', async ({ page }) => {
await page.goto('/', { waitUntil: 'networkidle' })
const header = page.getByRole('img', { name: 'News Aggregator' })
expect(header).toBeVisible()
})

test('Open search modal and search', async ({ page }) => {
await page.goto('/', { waitUntil: 'networkidle' })
const button = page.getByTestId('dialog-button')
await button.click()

const searchinput = page.getByPlaceholder('Search')
const submit = page.getByTestId('submit_btn')

await searchinput.fill('Brazil')
await submit.click()

await page.waitForTimeout(3000)
const list = page.getByRole('list').nth(1)

const article = await list.getByTestId('article-card').all()

await expect(article.length).toBeGreaterThan(1)
})

test('Open search modal and search with filters', async ({ page }) => {
await page.goto('/', { waitUntil: 'networkidle' })
const button = page.getByTestId('dialog-button')
await button.click()

const searchinput = page.getByPlaceholder('Search')
const submit = page.getByTestId('submit_btn')
const filterBtn = page.getByTestId('open_close_filters')
const categoryBtn = page
.locator('button')
.filter({ hasText: 'Select a category' })
const sourceFilter = page
.locator('button')
.filter({ hasText: 'Select a source' })
await searchinput.fill('Economy')
await filterBtn.click()
await categoryBtn.click()
const categoryBusiness = page.getByLabel('Business')

await categoryBusiness.click()
await sourceFilter.click()
const sourceTheGuardian = page.getByLabel('The Guardian')
await sourceTheGuardian.click()

await submit.click()

await page.waitForTimeout(3000)
const list = page.getByRole('list').nth(1)

const article = await list.getByTestId('article-card').all()

await expect(article.length).toBeGreaterThan(1)
})

test('Access news feed', async ({ page }) => {
await page.goto('/', { waitUntil: 'networkidle' })
const link = page.getByRole('link', { name: 'News feed' })
await link.click()
await page.waitForTimeout(1000)

expect(page.url()).toContain('/custom-news-feed')
})

test('Change to next page', async ({ page }) => {
await page.goto('/', { waitUntil: 'networkidle' })
const nextPageBtn = page.getByRole('button', { name: 'Next' })
await nextPageBtn.click()
await page.waitForTimeout(1000)
const previousBtn = page.getByRole('button', { name: 'Previous' })

expect(await previousBtn.isEnabled()).toBe(true)
})

0 comments on commit 8e99f4c

Please sign in to comment.