Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# js-code

Useful code and practice of JavaScript

## TOC

* [Test React components using component tests](./testing-js/react-components/test-app/)
- [Test React components using component tests](./testing-js/react-components/test-app/)
- [Tic tac toe game](./tic-tac-toe/README.md)
1 change: 1 addition & 0 deletions tic-tac-toe/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CHOKIDAR_USEPOLLING=true
23 changes: 23 additions & 0 deletions tic-tac-toe/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
13 changes: 13 additions & 0 deletions tic-tac-toe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Tic Tac Toe Game

[Following ReactJs tutorial](https://reactjs.org/tutorial/tutorial.html)

## Testing

- visual tests with [wdio](https://webdriver.io/docs/gettingstarted/)

```js
npm init wdio .
```

1. What are all the tests that we need to write to ensure bug free code?
46 changes: 46 additions & 0 deletions tic-tac-toe/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "tic-tac-toe",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"wdio": "wdio run wdio.conf.js"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@wdio/cli": "^7.16.11",
"@wdio/local-runner": "^7.16.11",
"@wdio/mocha-framework": "^7.16.11",
"@wdio/sauce-service": "^7.16.11",
"@wdio/spec-reporter": "^7.16.11"
}
}
Binary file added tic-tac-toe/public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions tic-tac-toe/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added tic-tac-toe/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tic-tac-toe/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions tic-tac-toe/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions tic-tac-toe/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
51 changes: 51 additions & 0 deletions tic-tac-toe/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
body {
font: 14px "Century Gothic", Futura, sans-serif;
margin: 20px;
}

ol,
ul {
padding-left: 30px;
}

.board-row:after {
clear: both;
content: "";
display: table;
}

.status {
margin-bottom: 10px;
}

.square {
background: #fff;
border: 1px solid #999;
float: left;
font-size: 24px;
font-weight: bold;
line-height: 34px;
height: 34px;
margin-right: -1px;
margin-top: -1px;
padding: 0;
text-align: center;
width: 34px;
}

.square:focus {
outline: none;
}

.kbd-navigation .square:focus {
background: #ddd;
}

.game {
display: flex;
flex-direction: row;
}

.game-info {
margin-left: 20px;
}
134 changes: 134 additions & 0 deletions tic-tac-toe/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";

function Square(props) {
//no more render()
return (
//no more this.props and no more ()=> onClick(), instead onClick
<button
data-testid={props.testid}
className="square"
onClick={props.onClick}
>
{props.value}
</button>
);
}

/**
* the best approach is to store the game’s state in the parent Board
* component instead of in each Square.
* The Board component can tell each Square what to display
* by passing a prop
* To collect data from multiple children,
* or to have two child components communicate with each other,
* you need to declare the shared state in their parent component instead.
* The parent component can pass the state back down to the children
* by using props;
* this keeps the child components in sync with each other
* and with the parent component.
*/
class Board extends React.Component {
constructor(props) {
super(props);
this.state = {
squares: Array(9).fill(null),
xIsNext: true,
};
}
handleClick(i) {
const squares = this.state.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? "X" : "O";
this.setState({ squares: squares, xIsNext: !this.state.xIsNext });
}
renderSquare(i) {
return (
// Now we’re passing down two props from Board to Square:
// value and onClick.
// The onClick prop is a function that Square can call when clicked.
<Square
value={this.state.squares[i]}
onClick={() => this.handleClick(i)}
testid={i}
/>
);
}

//The render method returns a description of what you want to see on the screen
render() {
const winner = calculateWinner(this.state.squares);
let status;
if (winner) {
status = "Winner " + winner;
} else {
status = "Next player: " + (this.state.xIsNext ? "X" : "O");
}

return (
<div>
<div className="status">{status}</div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}

class Game extends React.Component {
render() {
return (
<div className="game">
<div className="game-board">
<Board />
<p>Built, tested, and deployed with ❤️ by Nikolay Advolodkin</p>
<a href="http://ultimateqa.com">ultimateqa</a>
</div>
<div className="game-info">
<div>{/* status */}</div>
<ol>{/* TODO */}</ol>
</div>
</div>
);
}
}

// ========================================

ReactDOM.render(<Game />, document.getElementById("root"));

function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}
17 changes: 17 additions & 0 deletions tic-tac-toe/test/specs/visual.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
describe("Tic tac toe game", () => {
it("should look correct", async () => {
await browser.url(``);
await browser.execute("/*@visual.init*/", "Tic Tac Toe");
await browser.execute("/*@visual.snapshot*/", "Empty State");

await $('[data-testid="0"]').click();
await $('[data-testid="3"]').click();
await $('[data-testid="1"]').click();
await $('[data-testid="4"]').click();
await $('[data-testid="2"]').click();
await browser.execute("/*@visual.snapshot*/", "X is Winner");

const result = await browser.execute("/*@visual.end*/");
expect(result.message).toBeNull();
});
});
Loading