Skip to content

Commit

Permalink
Configured ESLint, Prettier and Github CI actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedalatawi committed Aug 7, 2024
1 parent 56c202c commit dbec257
Show file tree
Hide file tree
Showing 21 changed files with 2,526 additions and 1,604 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.expo
node_modules
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// https://docs.expo.dev/guides/using-eslint/
module.exports = {
extends: ["expo", "prettier"],
plugins: ["prettier"],
rules: {
"prettier/prettier": "error",
},
};
35 changes: 35 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
lint-and-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Find yarn cache location
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"

- name: JS package cache
uses: actions/cache@v1
with:
path: $(( steps.yarn-cache.outputs.dir ))
key: $(( runner.os ))-yarn-$(( hashFiles('**/yarn.lock') ))
restore-keys: |
$(( runner.os ))-yarn-
- name: Install Node Modules
run: yarn install

- name: Run Lint
run: yarn lint

- name: Run tests
run: yarn testFinal
28 changes: 14 additions & 14 deletions __tests__/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import renderer from "react-test-renderer";
import App from "../App";

describe("<App />", () => {
it("has 1 child", () => {
const tree: any = renderer.create(<App />).toJSON();
expect(tree.children.length).toBe(1);
});

it("renders correctly", () => {
const tree = renderer.create(<App />).toJSON();
expect(tree).toMatchSnapshot();
});
});
import renderer from "react-test-renderer";
import App from "../App";

describe("<App />", () => {
it("has 1 child", () => {
const tree: any = renderer.create(<App />).toJSON();
expect(tree.children.length).toBe(1);
});

it("renders correctly", () => {
const tree = renderer.create(<App />).toJSON();
expect(tree).toMatchSnapshot();
});
});
12 changes: 6 additions & 6 deletions __tests__/__mocks__/boardData.mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BOARD_SIZES } from "../../src/constants/common";
import { createBoard } from "../../src/utils/common";

const board = createBoard(BOARD_SIZES._3x3);

export default board;
import { BOARD_SIZES } from "../../src/constants/common";
import { createBoard } from "../../src/utils/common";

const board = createBoard(BOARD_SIZES._3x3);

export default board;
188 changes: 94 additions & 94 deletions __tests__/components/Board.test.tsx
Original file line number Diff line number Diff line change
@@ -1,94 +1,94 @@
import React from "react";
import renderer from "react-test-renderer";
import Board from "../../src/components/Board";
import board from "../__mocks__/boardData.mock";
import { render, screen, userEvent } from "@testing-library/react-native";
import { GAME_STATES, PLAYER_O, PLAYER_X } from "../../src/constants/common";

const onPressCallbackGlobal = jest.fn();

const mockProps = {
board: board,
gameState: GAME_STATES.inProgress,
disabled: false,
onPressUser: onPressCallbackGlobal,
};

describe("<Board />", () => {
const BoardComponent = () => <Board {...mockProps} />;

test("renders correctly", () => {
const tree = renderer.create(<BoardComponent />).toJSON();
expect(tree).toMatchSnapshot();
});

test("return the correct row and col indices when first square pressed", async () => {
render(<BoardComponent />);

const firstSquare = screen.getByTestId("00");

expect(firstSquare).toBeDefined();

await userEvent.press(firstSquare);

//enure the correct row and column indices are correct
expect(onPressCallbackGlobal).toHaveBeenCalledWith(0, 0);
});

test("prevent pressing when board is disabled", async () => {
const onPressCallback = jest.fn();
render(
<Board {...mockProps} disabled={true} onPressUser={onPressCallback} />
);

const firstSquare = screen.getByTestId("00");

expect(firstSquare).toBeDefined();

await userEvent.press(firstSquare);

//enure callBack did not fire
expect(onPressCallback).not.toHaveBeenCalled();
});

test("should display X and O for PLAYER_X and PLAYER_O", async () => {
const boardCopy = board.map((arr) => arr.slice());
boardCopy[0][0] = PLAYER_X;
boardCopy[0][1] = PLAYER_O;
const onPressCallback = jest.fn();

render(
<Board {...mockProps} board={boardCopy} onPressUser={onPressCallback} />
);

//Player X should exist on the board
expect(screen.getByText("X")).toBeDefined();

//Player O should exist on the board
expect(screen.getByText("O")).toBeDefined();

const squareX = screen.getByTestId("00");
const squareO = screen.getByTestId("01");

expect(squareX).toBeDefined();
expect(squareO).toBeDefined();
});

test("should disable board when game is ended", async () => {
const onPressCallback = jest.fn();
render(
<Board
{...mockProps}
gameState={GAME_STATES.ended}
onPressUser={onPressCallback}
/>
);

const square = screen.getByTestId("00");

await userEvent.press(square);

//enure board is disabled
expect(onPressCallback).not.toHaveBeenCalled();
});
});
import React from "react";
import renderer from "react-test-renderer";
import Board from "../../src/components/Board";
import board from "../__mocks__/boardData.mock";
import { render, screen, userEvent } from "@testing-library/react-native";
import { GAME_STATES, PLAYER_O, PLAYER_X } from "../../src/constants/common";

const onPressCallbackGlobal = jest.fn();

const mockProps = {
board: board,
gameState: GAME_STATES.inProgress,
disabled: false,
onPressUser: onPressCallbackGlobal,
};

describe("<Board />", () => {
const BoardComponent = () => <Board {...mockProps} />;

test("renders correctly", () => {
const tree = renderer.create(<BoardComponent />).toJSON();
expect(tree).toMatchSnapshot();
});

test("return the correct row and col indices when first square pressed", async () => {
render(<BoardComponent />);

const firstSquare = screen.getByTestId("00");

expect(firstSquare).toBeDefined();

await userEvent.press(firstSquare);

//enure the correct row and column indices are correct
expect(onPressCallbackGlobal).toHaveBeenCalledWith(0, 0);
});

test("prevent pressing when board is disabled", async () => {
const onPressCallback = jest.fn();
render(
<Board {...mockProps} disabled={true} onPressUser={onPressCallback} />,
);

const firstSquare = screen.getByTestId("00");

expect(firstSquare).toBeDefined();

await userEvent.press(firstSquare);

//enure callBack did not fire
expect(onPressCallback).not.toHaveBeenCalled();
});

test("should display X and O for PLAYER_X and PLAYER_O", async () => {
const boardCopy = board.map((arr) => arr.slice());
boardCopy[0][0] = PLAYER_X;
boardCopy[0][1] = PLAYER_O;
const onPressCallback = jest.fn();

render(
<Board {...mockProps} board={boardCopy} onPressUser={onPressCallback} />,
);

//Player X should exist on the board
expect(screen.getByText("X")).toBeDefined();

//Player O should exist on the board
expect(screen.getByText("O")).toBeDefined();

const squareX = screen.getByTestId("00");
const squareO = screen.getByTestId("01");

expect(squareX).toBeDefined();
expect(squareO).toBeDefined();
});

test("should disable board when game is ended", async () => {
const onPressCallback = jest.fn();
render(
<Board
{...mockProps}
gameState={GAME_STATES.ended}
onPressUser={onPressCallback}
/>,
);

const square = screen.getByTestId("00");

await userEvent.press(square);

//enure board is disabled
expect(onPressCallback).not.toHaveBeenCalled();
});
});
Loading

0 comments on commit dbec257

Please sign in to comment.