-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: add getCopyrightYearString tests * ci: add tests on CI * ci: fix action names * test: add 404-page tests * test: add blog page tests * test: fix next page error vercel/next.js#34249 * test: add PostReaction tests * test: 404 page rm beforeAll render page * test: add test view-counter * test: fix 404 page * feat: update introducing article * feat: add lint to test action
- Loading branch information
1 parent
7afb6a5
commit 171f2e5
Showing
15 changed files
with
311 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Lint | ||
name: Linting | ||
on: | ||
- push | ||
- pull_request | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Tests | ||
on: | ||
- push | ||
- pull_request | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node-version: [ 16.x ] | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- name: npm install, build | ||
run: npm ci | ||
env: | ||
CI: true | ||
- name: run tests | ||
run: npm run test:ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import * as Sentry from '@sentry/nextjs'; | ||
|
||
import NotFoundPage from '@/pages/404'; | ||
|
||
jest.mock('@sentry/nextjs'); | ||
|
||
describe('404 page', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('renders with correct heading', () => { | ||
render(<NotFoundPage />); | ||
|
||
const heading = screen.getByRole('heading', { | ||
name: "YOU'RE IN THE WRONG PLACE", | ||
}); | ||
|
||
expect(heading).toBeInTheDocument(); | ||
}); | ||
|
||
test('send sentry error on render', () => { | ||
render(<NotFoundPage />); | ||
|
||
expect(Sentry.captureException).toHaveBeenCalledWith(new Error('404')); | ||
}); | ||
|
||
test('send sentry error only on initial render', () => { | ||
render(<NotFoundPage />); | ||
|
||
expect(Sentry.captureException).toBeCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
|
||
import BlogPage from '@/pages/blog'; | ||
import { Post } from '@/lib/types'; | ||
|
||
jest.mock('@/lib/posts/api'); | ||
|
||
const posts: Post[] = [ | ||
{ | ||
data: { | ||
slug: 'first-article', | ||
description: 'First article description', | ||
featured: true, | ||
readTime: '1', | ||
tags: ['JS', 'React'], | ||
title: 'First article title', | ||
createDate: 11, | ||
updateData: 12, | ||
}, | ||
content: 'First article long text', | ||
}, | ||
{ | ||
data: { | ||
slug: 'second-article', | ||
description: 'Second article description', | ||
featured: false, | ||
readTime: '2', | ||
tags: ['TS', 'Redux'], | ||
title: 'Second article title', | ||
createDate: 124, | ||
updateData: 123, | ||
}, | ||
content: 'Second article text', | ||
}, | ||
]; | ||
|
||
describe('Blog Page', () => { | ||
test('renders with correct heading', () => { | ||
render(<BlogPage posts={posts} postsAmount={posts.length} />); | ||
const heading = screen.getByRole('heading', { | ||
name: 'Blog', | ||
}); | ||
|
||
expect(heading).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders with initial posts', () => { | ||
render(<BlogPage posts={posts} postsAmount={posts.length} />); | ||
const blogPostsLinks = screen.getAllByRole('link'); | ||
|
||
expect(blogPostsLinks).toHaveLength(posts.length); | ||
}); | ||
|
||
test('on search correct filtered exist posts', () => { | ||
render(<BlogPage posts={posts} postsAmount={posts.length} />); | ||
const inputElement = screen.getByLabelText('Search articles'); | ||
|
||
fireEvent.change(inputElement, { target: { value: posts[0].data.title } }); | ||
const post = screen.getByRole('heading', { | ||
name: posts[0].data.title, | ||
}); | ||
|
||
expect(post).toBeInTheDocument(); | ||
}); | ||
|
||
test('on search render "No posts found" if unknown post', () => { | ||
render(<BlogPage posts={posts} postsAmount={posts.length} />); | ||
const inputElement = screen.getByLabelText('Search articles'); | ||
|
||
fireEvent.change(inputElement, { target: { value: 'Unknown post' } }); | ||
const post = screen.getByText(/No posts found/); | ||
|
||
expect(post).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getCopyrightYearString } from '@/components/footer/get-copyright'; | ||
|
||
describe('getCopyrightYearString', () => { | ||
test('Return current year if now is 2022', () => { | ||
const currentYear = 2022; | ||
const createYear = 2022; | ||
|
||
const result = getCopyrightYearString(createYear, currentYear); | ||
|
||
expect(result).toStrictEqual('2022'); | ||
}); | ||
|
||
test('Return string range between create and current', () => { | ||
const currentYear = 2022; | ||
const createYear = 2020; | ||
|
||
const result = getCopyrightYearString(createYear, currentYear); | ||
|
||
expect(result).toStrictEqual(`${createYear} - ${currentYear}`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
const YEAR_OF_CREATE = 2022; | ||
|
||
export const getCopyright = () => { | ||
const currentYear = new Date().getFullYear(); | ||
|
||
if (YEAR_OF_CREATE === currentYear) { | ||
export const getCopyrightYearString = ( | ||
createYear: number, | ||
currentYear: number, | ||
) => { | ||
if (createYear === currentYear) { | ||
return currentYear.toString(); | ||
} | ||
|
||
return `${YEAR_OF_CREATE} - ${currentYear}`; | ||
return `${createYear} - ${currentYear}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
|
||
import { PostReaction } from '@/components/post-reaction/post-reaction'; | ||
|
||
describe('PostReaction component', () => { | ||
test('Render with initial state', () => { | ||
render(<PostReaction />); | ||
|
||
const title = screen.getByRole('heading', { | ||
name: 'Was this article helpful ?', | ||
}); | ||
const description = screen.getByText('Help me improve my blog'); | ||
|
||
expect(title).toBeInTheDocument(); | ||
expect(description).toBeInTheDocument(); | ||
}); | ||
|
||
describe('reactions click:', () => { | ||
test('worthless', () => { | ||
window.gtag = jest.fn(); | ||
render(<PostReaction />); | ||
|
||
const worthlessReactionButton = screen.getByRole('button', { | ||
name: 'No', | ||
}); | ||
fireEvent.click(worthlessReactionButton); | ||
|
||
expect(window.gtag).toHaveBeenCalledWith('event', 'Reaction click', { | ||
event_category: 'Blog - article', | ||
event_label: 'No', | ||
value: undefined, | ||
}); | ||
}); | ||
|
||
test('helpful', () => { | ||
window.gtag = jest.fn(); | ||
render(<PostReaction />); | ||
|
||
const worthlessReactionButton = screen.getByRole('button', { | ||
name: 'Yes', | ||
}); | ||
fireEvent.click(worthlessReactionButton); | ||
|
||
expect(window.gtag).toHaveBeenCalledWith('event', 'Reaction click', { | ||
event_category: 'Blog - article', | ||
event_label: 'Yes', | ||
value: undefined, | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.