Skip to content

Commit

Permalink
Merge pull request #188 from daithihearn/sonar-code-smells
Browse files Browse the repository at this point in the history
fix: sonar code smells
  • Loading branch information
daithihearn authored Oct 2, 2023
2 parents c304828 + 660dbc8 commit 9c0cae7
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 31 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ jobs:
- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run tests
run: yarn test
- name: Test and coverage
run: yarn jest --coverage
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ node_modules/
yarn-error.log
yarn.lock
prod.env

# testing
/coverage
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=daithihearn_cards-110-frontend&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=daithihearn_cards-110-frontend)

# cards-110-frontend

The frontend for the [Cards 110 application](https://github.com/daithihearn/cards-110)
Expand Down
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module.exports = {
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
testMatch: ["**/*.(spec|test).(ts|tsx|js|jsx)"],
maxWorkers: 2,
workerIdleMemoryLimit: "1GB",
reporters: ["default", "jest-junit"],
moduleNameMapper: {
"^model/(.*)$": "<rootDir>/src/model/$1",
},
}
153 changes: 153 additions & 0 deletions junit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="jest tests" tests="74" failures="0" errors="0" time="2.341">
<testsuite name="GameUtils" errors="0" failures="0" skipped="0" timestamp="2023-10-02T21:11:05" time="2.248" tests="74">
<testcase classname="GameUtils removeEmptyCards empty hand" name="GameUtils removeEmptyCards empty hand" time="0.001">
</testcase>
<testcase classname="GameUtils removeEmptyCards full hand" name="GameUtils removeEmptyCards full hand" time="0">
</testcase>
<testcase classname="GameUtils removeEmptyCards hand with empty card at end" name="GameUtils removeEmptyCards hand with empty card at end" time="0.001">
</testcase>
<testcase classname="GameUtils removeEmptyCards hand with empty card at beginning" name="GameUtils removeEmptyCards hand with empty card at beginning" time="0">
</testcase>
<testcase classname="GameUtils removeEmptyCards hand with empty card in middle" name="GameUtils removeEmptyCards hand with empty card in middle" time="0">
</testcase>
<testcase classname="GameUtils removeEmptyCards hand with multiple empty cards" name="GameUtils removeEmptyCards hand with multiple empty cards" time="0">
</testcase>
<testcase classname="GameUtils removeEmptyCards hand with multiple empty cards at beginning" name="GameUtils removeEmptyCards hand with multiple empty cards at beginning" time="0.001">
</testcase>
<testcase classname="GameUtils removeEmptyCards hand with multiple empty cards at end" name="GameUtils removeEmptyCards hand with multiple empty cards at end" time="0">
</testcase>
<testcase classname="GameUtils removeEmptyCards hand with multiple empty cards in middle" name="GameUtils removeEmptyCards hand with multiple empty cards in middle" time="0">
</testcase>
<testcase classname="GameUtils removeEmptyCards hand with only empty cards" name="GameUtils removeEmptyCards hand with only empty cards" time="0">
</testcase>
<testcase classname="GameUtils compareCards 2 empty hands should return true" name="GameUtils compareCards 2 empty hands should return true" time="0.001">
</testcase>
<testcase classname="GameUtils compareCards equal hands" name="GameUtils compareCards equal hands" time="0.006">
</testcase>
<testcase classname="GameUtils compareCards different hands" name="GameUtils compareCards different hands" time="0">
</testcase>
<testcase classname="GameUtils compareCards equal hands with different order" name="GameUtils compareCards equal hands with different order" time="0">
</testcase>
<testcase classname="GameUtils padMyHand empty hand" name="GameUtils padMyHand empty hand" time="0">
</testcase>
<testcase classname="GameUtils padMyHand full hand" name="GameUtils padMyHand full hand" time="0">
</testcase>
<testcase classname="GameUtils padMyHand partial hand" name="GameUtils padMyHand partial hand" time="0">
</testcase>
<testcase classname="GameUtils processOrderedCardsAfterGameUpdate same cards" name="GameUtils processOrderedCardsAfterGameUpdate same cards" time="0">
</testcase>
<testcase classname="GameUtils processOrderedCardsAfterGameUpdate same cards with different order" name="GameUtils processOrderedCardsAfterGameUpdate same cards with different order" time="0">
</testcase>
<testcase classname="GameUtils processOrderedCardsAfterGameUpdate First card removed" name="GameUtils processOrderedCardsAfterGameUpdate First card removed" time="0.001">
</testcase>
<testcase classname="GameUtils processOrderedCardsAfterGameUpdate Last card removed" name="GameUtils processOrderedCardsAfterGameUpdate Last card removed" time="0">
</testcase>
<testcase classname="GameUtils processOrderedCardsAfterGameUpdate Middle card removed" name="GameUtils processOrderedCardsAfterGameUpdate Middle card removed" time="0">
</testcase>
<testcase classname="GameUtils processOrderedCardsAfterGameUpdate any other configuration 1" name="GameUtils processOrderedCardsAfterGameUpdate any other configuration 1" time="0">
</testcase>
<testcase classname="GameUtils processOrderedCardsAfterGameUpdate any other configuration 2" name="GameUtils processOrderedCardsAfterGameUpdate any other configuration 2" time="0">
</testcase>
<testcase classname="GameUtils areAllTrumpCards empty hand" name="GameUtils areAllTrumpCards empty hand" time="0.001">
</testcase>
<testcase classname="GameUtils areAllTrumpCards all trump cards" name="GameUtils areAllTrumpCards all trump cards" time="0">
</testcase>
<testcase classname="GameUtils areAllTrumpCards not all trump cards" name="GameUtils areAllTrumpCards not all trump cards" time="0">
</testcase>
<testcase classname="GameUtils containsATrumpCard empty hand" name="GameUtils containsATrumpCard empty hand" time="0">
</testcase>
<testcase classname="GameUtils containsATrumpCard has a trump card" name="GameUtils containsATrumpCard has a trump card" time="0">
</testcase>
<testcase classname="GameUtils containsATrumpCard doesn&apos;t have a trump card" name="GameUtils containsATrumpCard doesn&apos;t have a trump card" time="0">
</testcase>
<testcase classname="GameUtils removeCard empty hand" name="GameUtils removeCard empty hand" time="0">
</testcase>
<testcase classname="GameUtils removeCard remove first card" name="GameUtils removeCard remove first card" time="0.001">
</testcase>
<testcase classname="GameUtils removeCard remove last card" name="GameUtils removeCard remove last card" time="0">
</testcase>
<testcase classname="GameUtils removeCard remove middle card" name="GameUtils removeCard remove middle card" time="0">
</testcase>
<testcase classname="GameUtils removeAllFromHand empty hand" name="GameUtils removeAllFromHand empty hand" time="0">
</testcase>
<testcase classname="GameUtils removeAllFromHand remove first card" name="GameUtils removeAllFromHand remove first card" time="0">
</testcase>
<testcase classname="GameUtils removeAllFromHand remove last card" name="GameUtils removeAllFromHand remove last card" time="0">
</testcase>
<testcase classname="GameUtils removeAllFromHand remove middle card" name="GameUtils removeAllFromHand remove middle card" time="0">
</testcase>
<testcase classname="GameUtils removeAllFromHand remove multiple cards" name="GameUtils removeAllFromHand remove multiple cards" time="0">
</testcase>
<testcase classname="GameUtils removeAllFromHand remove multiple cards in different order" name="GameUtils removeAllFromHand remove multiple cards in different order" time="0.001">
</testcase>
<testcase classname="GameUtils removeAllFromHand remove all cards" name="GameUtils removeAllFromHand remove all cards" time="0">
</testcase>
<testcase classname="GameUtils riskOfMistakeBuyingCards none selected" name="GameUtils riskOfMistakeBuyingCards none selected" time="0">
</testcase>
<testcase classname="GameUtils riskOfMistakeBuyingCards no trump cards" name="GameUtils riskOfMistakeBuyingCards no trump cards" time="0">
</testcase>
<testcase classname="GameUtils riskOfMistakeBuyingCards select all cards" name="GameUtils riskOfMistakeBuyingCards select all cards" time="0">
</testcase>
<testcase classname="GameUtils riskOfMistakeBuyingCards select all trumps" name="GameUtils riskOfMistakeBuyingCards select all trumps" time="0">
</testcase>
<testcase classname="GameUtils riskOfMistakeBuyingCards don&apos;t select all trumps" name="GameUtils riskOfMistakeBuyingCards don&apos;t select all trumps" time="0">
</testcase>
<testcase classname="GameUtils getTrumpCards empty hand" name="GameUtils getTrumpCards empty hand" time="0">
</testcase>
<testcase classname="GameUtils getTrumpCards all trump cards" name="GameUtils getTrumpCards all trump cards" time="0">
</testcase>
<testcase classname="GameUtils getTrumpCards no trump cards" name="GameUtils getTrumpCards no trump cards" time="0.001">
</testcase>
<testcase classname="GameUtils getTrumpCards all trump cards with joker and ace of hearts" name="GameUtils getTrumpCards all trump cards with joker and ace of hearts" time="0">
</testcase>
<testcase classname="GameUtils getTrumpCards some trump cards" name="GameUtils getTrumpCards some trump cards" time="0">
</testcase>
<testcase classname="GameUtils bestCardLead no suit" name="GameUtils bestCardLead no suit" time="0">
</testcase>
<testcase classname="GameUtils bestCardLead best card lead" name="GameUtils bestCardLead best card lead" time="0">
</testcase>
<testcase classname="GameUtils bestCardLead best card not lead" name="GameUtils bestCardLead best card not lead" time="0">
</testcase>
<testcase classname="GameUtils getBestCard empty hand" name="GameUtils getBestCard empty hand" time="0">
</testcase>
<testcase classname="GameUtils getBestCard trump card" name="GameUtils getBestCard trump card" time="0">
</testcase>
<testcase classname="GameUtils getBestCard follow cold card" name="GameUtils getBestCard follow cold card" time="0">
</testcase>
<testcase classname="GameUtils getWorstCard empty hand" name="GameUtils getWorstCard empty hand" time="0">
</testcase>
<testcase classname="GameUtils getWorstCard trump card" name="GameUtils getWorstCard trump card" time="0">
</testcase>
<testcase classname="GameUtils getWorstCard follow cold card" name="GameUtils getWorstCard follow cold card" time="0">
</testcase>
<testcase classname="GameUtils calculateMinCardsToKeep less than 2 players. Should throw an error" name="GameUtils calculateMinCardsToKeep less than 2 players. Should throw an error" time="0.023">
</testcase>
<testcase classname="GameUtils calculateMinCardsToKeep more than 6 players. Should throw an error" name="GameUtils calculateMinCardsToKeep more than 6 players. Should throw an error" time="0">
</testcase>
<testcase classname="GameUtils calculateMinCardsToKeep 2 players" name="GameUtils calculateMinCardsToKeep 2 players" time="0">
</testcase>
<testcase classname="GameUtils calculateMinCardsToKeep 3 players" name="GameUtils calculateMinCardsToKeep 3 players" time="0.001">
</testcase>
<testcase classname="GameUtils calculateMinCardsToKeep 4 players" name="GameUtils calculateMinCardsToKeep 4 players" time="0">
</testcase>
<testcase classname="GameUtils calculateMinCardsToKeep 5 players" name="GameUtils calculateMinCardsToKeep 5 players" time="0">
</testcase>
<testcase classname="GameUtils calculateMinCardsToKeep 6 players" name="GameUtils calculateMinCardsToKeep 6 players" time="0">
</testcase>
<testcase classname="GameUtils pickBestCards empty hand" name="GameUtils pickBestCards empty hand" time="0">
</testcase>
<testcase classname="GameUtils pickBestCards all trumps" name="GameUtils pickBestCards all trumps" time="0">
</testcase>
<testcase classname="GameUtils pickBestCards Must keep 2" name="GameUtils pickBestCards Must keep 2" time="0">
</testcase>
<testcase classname="GameUtils pickBestCards Must keep 1" name="GameUtils pickBestCards Must keep 1" time="0.001">
</testcase>
<testcase classname="GameUtils pickBestCards Must keep 0" name="GameUtils pickBestCards Must keep 0" time="0">
</testcase>
<testcase classname="GameUtils pickBestCards Wild cards one" name="GameUtils pickBestCards Wild cards one" time="0">
</testcase>
<testcase classname="GameUtils pickBestCards Wild cards 2" name="GameUtils pickBestCards Wild cards 2" time="0">
</testcase>
</testsuite>
</testsuites>
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@
"@types/react-loadable": "5.5.9",
"enzyme": "3.11.0",
"enzyme-adapter-react-16": "1.15.7",
"jest-junit": "^16.0.0",
"node-sass-chokidar": "2.0.0",
"npm-run-all": "4.1.5",
"prettier": "3.0.3",
"react-scripts": "5.0.1"
"react-scripts": "5.0.1",
"ts-jest": "^29.1.1"
},
"scripts": {
"build-css": "node-sass-chokidar --include-path ./node_modules ./src/scss -o ./src/scss",
Expand Down
12 changes: 12 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sonar.organization=daithihearn
sonar.projectKey=daithihearn_cards-110-frontend
sonar.sources=src
sonar.tests=src
sonar.test.inclusions=**/*.spec.ts,**/*.spec.tsx
# Exclude from analysis: tests and test helpers
sonar.exclusions=**/*.spec.ts,**/*.spec.tsx,src/test/**/*
# Exclude from coverage: tests, e2e tests, test helpers, content-scripts, service-worker, web-accessible-resources
sonar.coverage.exclusions=**/*.spec.ts,**/*.spec.tsx
sonar.verbose=false
sonar.working.directory=/tmp/sonar
sonar.javascript.lcov.reportPaths=./coverage/lcov.info
1 change: 0 additions & 1 deletion src/caches/GameSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { GameState, GameStatus, PlayedCard } from "model/Game"
import { Player } from "model/Player"
import { RoundStatus } from "model/Round"
import { RootState } from "./caches"
import { get } from "http"

const initialState: GameState = {
iamSpectator: true,
Expand Down
10 changes: 0 additions & 10 deletions src/components/Footer/Footer.spec.tsx

This file was deleted.

14 changes: 8 additions & 6 deletions src/components/Game/Actions/Buying.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ const Buying = () => {
</b>
</Button>

<ThrowCardsWarningModal
modalVisible={deleteCardsDialog}
cancelCallback={hideCancelDeleteCardsDialog}
continueCallback={buyCards}
suit={suit!}
/>
{suit && (
<ThrowCardsWarningModal
modalVisible={deleteCardsDialog}
cancelCallback={hideCancelDeleteCardsDialog}
continueCallback={buyCards}
suit={suit}
/>
)}
</>
)
}
Expand Down
5 changes: 1 addition & 4 deletions src/components/Header/_header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
width: auto;
min-height: 80px;
position: relative;
transition-delay: 0.1s;
transition: all 0.2s linear;
transition-delay: 0.1s;
}

.header_backgroundSearch {
Expand Down Expand Up @@ -148,7 +148,6 @@
}

&_CloseButton {
height: 20px;
position: absolute;
top: 25px;
right: 104px;
Expand Down Expand Up @@ -254,7 +253,6 @@
}
&_toggleButton {
display: block;
height: 20px;
position: fixed;
bottom: 210px;
right: 24px;
Expand All @@ -273,7 +271,6 @@

&_toggleButton2 {
display: block;
height: 20px;
position: fixed;
bottom: 210px;
right: 24px;
Expand Down
4 changes: 2 additions & 2 deletions src/serviceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function registerValidSW(swUrl, config) {
)

// Execute callback
if (config && config.onUpdate) {
if (config?.onUpdate) {
config.onUpdate(registration)
}
} else {
Expand All @@ -85,7 +85,7 @@ function registerValidSW(swUrl, config) {
console.log("Content is cached for offline use.")

// Execute callback
if (config && config.onSuccess) {
if (config?.onSuccess) {
config.onSuccess(registration)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/ProfileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const updateProfile =
accessToken?: string,
): AppThunk<Promise<void>> =>
async (dispatch, getState) => {
const token = accessToken || getAccessToken(getState())
const token = accessToken ?? getAccessToken(getState())
if (!token) throw Error("No access token found")

const response = await axios.put<ProfileResponse>(
Expand Down
4 changes: 2 additions & 2 deletions src/utils/GameUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export const getWorstCard = (cards: Card[], round: Round) => {

// Check if must follow suit
const leadOut = round.currentHand?.leadOut
let suitLead = leadOut ? CARDS[leadOut as CardName]?.suit : undefined
let suitLead = leadOut ? CARDS[leadOut]?.suit : undefined

if (suitLead === Suit.WILD) {
suitLead = round.suit
Expand Down Expand Up @@ -221,7 +221,7 @@ export const getBestCard = (cards: Card[], round: Round) => {

// Check if have any cold cards
const leadOut = round.currentHand?.leadOut
const suitLead = leadOut ? CARDS[leadOut as CardName]?.suit : undefined
const suitLead = leadOut ? CARDS[leadOut]?.suit : undefined

if (suitLead && suitLead !== Suit.WILD && suitLead !== round.suit) {
const myColdCards = cards.filter(card => card.suit === suitLead)
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"baseUrl": "src"
},
"include": ["src"],
"exclude": ["build", "node_modules"]
"exclude": ["build", "node_modules"],
"typeAcquisition": {
"include": ["jest"]
}
}

0 comments on commit 9c0cae7

Please sign in to comment.