Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Tests #12

Merged
merged 49 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
a4ce3ca
Create basic redux actions unit tests
developer239 Apr 21, 2019
054c627
Update jest collect coverage settings
developer239 Apr 21, 2019
ecb3da2
Write basic reducer integration test
developer239 Apr 21, 2019
283478e
Remove redux actions unit tests
developer239 Apr 21, 2019
d695e9f
Add react-testing-library and jest-styled-components dependencies
developer239 Apr 21, 2019
96a137b
Create basic button component unit test
developer239 Apr 21, 2019
6215985
Create basic button component snapshot
developer239 Apr 21, 2019
592604b
Create test case for disabled button
developer239 Apr 21, 2019
67fe678
Create failing test for not found page
developer239 Apr 21, 2019
95b8e31
Create successful test for not found page
developer239 Apr 21, 2019
c61a349
Create basic products list snapshot test
developer239 Apr 21, 2019
1cc2c59
Create basic products list snapshot test when products loaded
developer239 Apr 21, 2019
02bed15
Fix react-testing-library react-hooks errors
developer239 Apr 21, 2019
016098f
Add jest-mock dependency
developer239 Apr 21, 2019
08e4cea
Added CartItem product id into array
arnostpleskot Apr 21, 2019
c4bd7cd
Getting rid of classes
arnostpleskot Apr 21, 2019
5e1aaa8
Adding number of products per size selector
arnostpleskot Apr 21, 2019
21635ee
Refactored getting url params in product list
arnostpleskot Apr 21, 2019
5fdc5dd
Mock Fetch requests
developer239 Apr 21, 2019
b3f09d2
ramdify
varholak-peter Apr 21, 2019
dcf549c
Create basic tests for product detail page
developer239 Apr 21, 2019
91a2813
Create basic tests for cart page
developer239 Apr 21, 2019
6831962
Use routes constants instead of hard-coded routes
developer239 Apr 21, 2019
e601196
Create basic test for not-logged-in user on account page
developer239 Apr 21, 2019
3642e2e
Create basic test user account page
developer239 Apr 21, 2019
6c5c135
Create basic signup page test
developer239 Apr 21, 2019
21ed32b
Write advanced tests for signup page
developer239 Apr 21, 2019
7b0e673
Replace hard-coded user data with constant mock user data
developer239 Apr 21, 2019
8104b90
Write tests for sign in page
developer239 Apr 21, 2019
5687627
Merge branch 'week5-homework' of https://github.com/strvcom/react-nig…
developer239 Apr 21, 2019
026e2fe
Update products list snapshot
developer239 Apr 21, 2019
365a1d1
Fix typo
developer239 Apr 21, 2019
3fe0468
Polish source code
developer239 Apr 21, 2019
741a015
Make store mock nicer
developer239 Apr 23, 2019
ac821ce
Detect account page by page-id
developer239 Apr 23, 2019
8fcb048
Fix snapshot tests
developer239 Apr 23, 2019
584b989
Install cypress
developer239 Apr 23, 2019
529852b
Initialize cypress
developer239 Apr 23, 2019
03f45e1
Add eslint-plugin-cypress
developer239 Apr 23, 2019
e58c97c
Fix cypress base url
developer239 Apr 23, 2019
4dc68aa
Write basic cypress test
developer239 Apr 23, 2019
1fb47c2
Fix cypress test
developer239 Apr 24, 2019
ba0a6ed
Resolve tasks from code review
developer239 Apr 24, 2019
fa13c9e
Merge branch 'master' of https://github.com/strvcom/react-nights-2018…
developer239 Apr 24, 2019
c6635c0
Run tests before every commit
developer239 Apr 24, 2019
0ceeb98
Add cross-env
developer239 Apr 24, 2019
02e2c23
Revert "Add cross-env"
developer239 Apr 24, 2019
daffaa2
Revert "Run tests before every commit"
developer239 Apr 24, 2019
15e7a9a
Recover README files 💣
developer239 Apr 24, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
'prettier',
'prettier/react',
],
plugins: ['react-hooks'],
plugins: ['react-hooks', 'cypress'],
root: true,
env: {
browser: true,
Expand Down
3 changes: 3 additions & 0 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"baseUrl": "http://localhost:3000"
}
1 change: 1 addition & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
18 changes: 18 additions & 0 deletions cypress/integration/add-to-cart.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
describe('home page', () => {
it('should display home page', () => {
cy.visit('/products')
// Find first Add To Cart button
cy.get('button')
.eq(0)
.click() // put the product into shopping cart
// Find second Add To Cart button
cy.get('button')
.eq(1)
.click() // put the product into shopping cart
.click() // put the product into shopping cart
// Navigate to shopping cart
cy.contains('My Cart').click()
// Expect to have 2 different products in shopping cart
cy.get('li').should('have.length', 2)
})
})
17 changes: 17 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

module.exports = () => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
25 changes: 25 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
20 changes: 20 additions & 0 deletions cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@
"eject": "react-scripts eject",
"format": "prettier --write '*/**/*.{js,css,md,json}'",
"lint:js": "eslint . --ignore-path .gitignore --fix",
"lint:css": "stylelint 'src/**/*.js'"
"lint:css": "stylelint 'src/**/*.js'",
"cypress:open": "cypress open"
},
"jest": {
"collectCoverageFrom": [
"src/**/*.{js,jsx}",
"!/node_modules/",
"!src/index.js",
"!src/serviceWorker.js",
"!src/store/index.js"
developer239 marked this conversation as resolved.
Show resolved Hide resolved
]
},
"lint-staged": {
"*.js": [
Expand Down Expand Up @@ -55,11 +65,16 @@
"devDependencies": {
"@strv/eslint-config-react": "^1.0.1",
"@strv/stylelint-config-styled-components": "^1.0.0",
"cypress": "^3.2.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-cypress": "^2.2.1",
"eslint-plugin-react-hooks": "^1.6.0",
"fetch-mock": "^7.3.1",
"husky": "^1.3.1",
"jest-styled-components": "^6.3.1",
"lint-staged": "^8.1.5",
"prettier": "^1.16.4",
"react-testing-library": "^6.1.2",
"stylelint": "^9.10.1",
"stylelint-config-prettier": "^5.0.0"
}
Expand Down
6 changes: 3 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import { getCustomer } from './utils/customer'
import { configureStore } from './store'
import * as routes from './routes'

const store = configureStore({
const defaultStore = configureStore({
customer: getCustomer(),
})

const App = () => (
<Provider store={store}>
const App = ({ store }) => (
<Provider store={store || defaultStore}>
<React.Fragment>
<GlobalStyles />
<ToastContainer position={toast.POSITION.BOTTOM_RIGHT} />
Expand Down
20 changes: 20 additions & 0 deletions src/components/Button/test/Button.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react'
import { render } from 'react-testing-library'
import 'jest-styled-components'

import Button from '../index'

// This is just an example how to test components
describe('[components] Button', () => {
it('should render correctly', () => {
const renderer = render(<Button>My Button</Button>)
expect(renderer.container).toMatchSnapshot()
})

describe('when disabled', () => {
it('should render correctly', () => {
const renderer = render(<Button disabled>My Button</Button>)
expect(renderer.container).toMatchSnapshot()
})
})
})
42 changes: 42 additions & 0 deletions src/components/Button/test/__snapshots__/Button.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`[components] Button should render correctly 1`] = `
.c0 {
background: #ef0d33;
cursor: pointer;
padding: 1rem;
margin-top: 0.5rem;
border: none;
border-radius: 5px;
color: #fff;
}

<div>
<button
class="c0"
>
My Button
</button>
</div>
`;

exports[`[components] Button when disabled should render correctly 1`] = `
.c0 {
background: #e5e5e5;
cursor: default;
padding: 1rem;
margin-top: 0.5rem;
border: none;
border-radius: 5px;
color: #fff;
}

<div>
<button
class="c0"
disabled=""
>
My Button
</button>
</div>
`;
4 changes: 2 additions & 2 deletions src/components/Layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as routes from '../../routes'

import { Wrapper, Header, HeaderSection, HeaderLink } from './styled'

const Layout = ({ isAuthenticated, children }) => (
const Layout = ({ isAuthenticated, children, dataTestId }) => (
<Fragment>
<Header>
<HeaderSection>
Expand All @@ -26,7 +26,7 @@ const Layout = ({ isAuthenticated, children }) => (
)}
</HeaderSection>
</Header>
<Wrapper>{children}</Wrapper>
<Wrapper data-testid={dataTestId}>{children}</Wrapper>
</Fragment>
)

Expand Down
2 changes: 1 addition & 1 deletion src/components/Loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { LoaderWrap, StyledLoader, Circular, Path } from './styled'

const Loader = props => (
<LoaderWrap {...props}>
<LoaderWrap data-testid="loader" {...props}>
<StyledLoader>
<Circular viewBox="25 25 50 50">
<Path
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Layout from '../../components/Layout'
import { H1 } from '../../components/Typography'

const AccountPage = ({ customer }) => (
<Layout>
<Layout dataTestId="account-page">
<H1>Welcome {customer.attributes.metadata.firstName}</H1>
</Layout>
)
Expand Down
36 changes: 36 additions & 0 deletions src/pages/Account/test/Account.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react'
import 'jest-styled-components'

import { App } from '../../../App'
import * as routes from '../../../routes'
import { renderWithRouter } from '../../../utilsTest/render'
import { configureStore } from '../../../store'
import { USER } from '../../../utilsTest/mockData'

describe('[pages] Account', () => {
describe('when not logged in', () => {
it('should redirect to login page', () => {
const renderer = renderWithRouter(<App />, routes.ACCOUNT)
const HTMLDivElement = renderer.getByTestId('login-page')
expect(HTMLDivElement).toBeTruthy()
})
})

describe('when logged in', () => {
it('should render private user account page', () => {
const store = configureStore({
customer: {
attributes: {
metadata: {
firstName: USER.firstName,
},
},
},
})

const renderer = renderWithRouter(<App store={store} />, routes.ACCOUNT)
const H1HtmlElement = renderer.getByTestId('account-page')
expect(H1HtmlElement).toBeTruthy()
})
})
})
18 changes: 11 additions & 7 deletions src/pages/Cart/CartItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ const CartItem = ({ productId, quantity, removeProduct }) => {
return (
<li key={productId}>
{isLoading && <Loader small />}
<p>
{/* and finally here we try to get name from downloaded product */}
{getName(product)} - {quantity}
</p>
<Button type="button" onClick={() => removeProduct(productId)}>
Remove
</Button>
{!isLoading && (
<div data-testid="product-in-cart">
<p>
{/* and finally here we try to get name from downloaded product */}
{getName(product)} - {quantity}
</p>
<Button type="button" onClick={() => removeProduct(productId)}>
Remove
</Button>
</div>
)}
</li>
)
}
Expand Down
37 changes: 37 additions & 0 deletions src/pages/Cart/test/CartItem.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react'
import { waitForElement } from 'react-testing-library'
import 'jest-styled-components'

import { App } from '../../../App'
import * as routes from '../../../routes'
import { renderWithRouter } from '../../../utilsTest/render'
import { mockFetchProduct } from '../../../utilsTest/mockHelpers'
import { configureStore } from '../../../store'
import { getCustomer } from '../../../utils/customer'

describe('[pages] Cart', () => {
describe('when loading', () => {
it('should render correctly', () => {
const renderer = renderWithRouter(<App />, routes.CART)
expect(renderer.container).toMatchSnapshot()
})
})

describe('when products loaded', () => {
mockFetchProduct()

it('should render correctly', async () => {
const productId = 1
const store = configureStore({
customer: getCustomer(),
cart: {
[productId]: productId,
},
})

const renderer = renderWithRouter(<App store={store} />, routes.CART)
await waitForElement(() => renderer.getByTestId('product-in-cart'))
expect(renderer.getAllByTestId('product-in-cart').length).toEqual(1)
})
})
})
Loading