Skip to content

Commit

Permalink
create test file
Browse files Browse the repository at this point in the history
  • Loading branch information
TariqBazadough committed Nov 4, 2024
1 parent 522c9be commit 0076ccc
Show file tree
Hide file tree
Showing 7 changed files with 3,989 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# .github/workflows/main.yml
name: Run Jest Tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "20.x"
- run: npm install
- run: npm run ghworkflowtest -- --ci --reporters=default --reporters=jest-junit
- name: update Airtable
if: always()
run: node ./.scripts/main.js JS ${{github.actor}} https://github.com/${{github.repository}}
130 changes: 130 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
85 changes: 85 additions & 0 deletions .scripts/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const axios = require("axios");
const fs = require("fs");
const { argv } = require("process");
const xml2js = require("xml2js");

// Constants and configuration
const TOPIC = argv[2];
const STUDENT_GITHUB = argv[3];
const STUDENT_REPO = argv[4];
const JEST_REPORT_PATH = "./junit.xml";

// Airtable API functions
const airtableApiServer = axios.create({
baseURL: `https://airtable-be.vercel.app/`,
});

// Jest report parsing
function parseJestReport(xmlContent) {
return new Promise((resolve, reject) => {
xml2js.parseString(xmlContent, (err, result) => {
if (err) {
reject(err);
} else {
const testsuites = result.testsuites;
const totalTests = parseInt(testsuites.$.tests);
const failedTests = parseInt(testsuites.$.failures);
const passedTests = totalTests - failedTests;
const grade = (passedTests / totalTests) * 100;

resolve({ totalTests, passedTests, failedTests, grade });
}
});
});
}

const sendDataToServer = async (
passedTests,
failedTests,
grade,
studentGitHub,
studentRepo
) => {
try {
await airtableApiServer
.post("/", {
passedTests,
failedTests,
grade,
studentGitHub,
studentRepo,
topic: TOPIC,
})
.then((response) => console.log("Sent Data Successfully"));
} catch (error) {
console.log("error in sending data to server", error);
}
};

// Main function
async function main() {
try {
const jestResults = fs.readFileSync(JEST_REPORT_PATH, "utf8");
const { totalTests, passedTests, failedTests, grade } =
await parseJestReport(jestResults);

console.log(`Total tests: ${totalTests}`);
console.log(`Passed tests: ${passedTests}`);
console.log(`Failed tests: ${failedTests}`);
console.log(`Grade: ${grade.toFixed(2)}%`);

await sendDataToServer(
passedTests,
failedTests,
grade.toFixed(2),
STUDENT_GITHUB,
STUDENT_REPO
);
} catch (error) {
console.error("An error occurred:", error.message);
process.exit(1);
}
}

// Run the main function
main();
63 changes: 63 additions & 0 deletions objectIteration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const fruits = [
{
id: 501,
name: "Apple",
color: "Red",
taste: "Sweet",
},
{
id: 502,
name: "Banana",
color: "Yellow",
taste: "Sweet",
},
{
id: 503,
name: "Orange",
color: "Orange",
taste: "Citrusy",
},
{
id: 504,
name: "Grapes",
color: "Purple",
taste: "Sweet",
},
{
id: 505,
name: "Kiwi",
color: "Green",
taste: "Tangy",
},
];

// 1) Using `getFruitColor` function that accepts a `fruit` object as an argument, and returns the color of that `fruit` object
function getFruitColor(fruit) {
// write your code here...
}
// console.log(getFruitColor(fruits[0])); // Outputs: Red

// 2) Using `isFruitTasteMatching` function that accepts a `fruit` object as an argument and a `taste` string, return true if the fruit's taste matches the provided description, otherwise returns false
function isFruitTasteMatching(fruit, taste) {
// write your code here...
}
// console.log(isFruitTasteMatching(fruits[2], "Citrusy")); // Outputs: true

// 3) Using `addFruit` function that accepts an array of fruit object `fruits` and a `fruit` object (with id, name, color, and taste), it will add the new fruit to the end of the array, then returns the updated array
function addFruit(fruits, fruit) {
// write your code here...
}
// console.log(addFruit(fruits, { id: 506, name: "Mango", color: "Yellow", taste: "Sweet" }));

// Using `countSweetFruits`function that accepts an array of fruit objects `fruits`, and return the number of fruits with a sweet taste
function countSweetFruits(fruits) {
// write your code here...
}
// console.log(countSweetFruits(fruits)); // Outputs: 4

module.exports = {
getFruitColor,
isFruitTasteMatching,
addFruit,
countSweetFruits,
};
88 changes: 88 additions & 0 deletions objectIteration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const {
getFruitColor,
isFruitTasteMatching,
addFruit,
countSweetFruits,
} = require("./objectIteration");

describe("Fruit Data Functions", () => {
let fruits;

beforeEach(() => {
fruits = [
{ id: 501, name: "Apple", color: "Red", taste: "Sweet" },
{ id: 502, name: "Banana", color: "Yellow", taste: "Sweet" },
{ id: 503, name: "Orange", color: "Orange", taste: "Citrusy" },
{ id: 504, name: "Grapes", color: "Purple", taste: "Sweet" },
{ id: 505, name: "Kiwi", color: "Green", taste: "Tangy" },
];
});

describe("getFruitColor", () => {
it("should return the color of the fruit", () => {
expect(getFruitColor(fruits[0])).toBe("Red");
});

it("should return the color of the fruit", () => {
expect(getFruitColor(fruits[1])).toBe("Yellow");
});

it("should return the color of the fruit", () => {
expect(getFruitColor(fruits[2])).toBe("Orange");
});

it("should return the color of the fruit", () => {
expect(getFruitColor(fruits[3])).toBe("Purple");
});

it("should return the color of the fruit", () => {
expect(getFruitColor(fruits[4])).toBe("Green");
});
});

describe("isFruitTasteMatching", () => {
it("should return true if the fruit's taste matches the provided description", () => {
expect(isFruitTasteMatching(fruits[2], "Citrusy")).toBe(true);
});

it("should return false if the fruit's taste does not match the provided description", () => {
expect(isFruitTasteMatching(fruits[0], "Citrusy")).toBe(false);
});

it("should return true if the fruit's taste matches the provided description", () => {
expect(isFruitTasteMatching(fruits[1], "Sweet")).toBe(true);
});

it("should return false if the fruit's taste does not match the provided description", () => {
expect(isFruitTasteMatching(fruits[4], "Sweet")).toBe(false);
});
});

describe("addFruit", () => {
it("should add a new fruit to the end of the array and return the updated array", () => {
const newFruit = {
id: 506,
name: "Mango",
color: "Yellow",
taste: "Sweet",
};
const updatedFruits = addFruit([...fruits], newFruit);
expect(updatedFruits).toContainEqual(newFruit);
expect(updatedFruits.length).toBe(fruits.length + 1);
expect(updatedFruits[updatedFruits.length - 1]).toEqual(newFruit);
});
});

describe("countSweetFruits", () => {
it("should count the number of fruits with a sweet taste", () => {
expect(countSweetFruits(fruits)).toBe(3);
const addedFruits = addFruit([...fruits], {
id: 506,
name: "Mango",
color: "Yellow",
taste: "Sweet",
});
expect(countSweetFruits(addedFruits)).toBe(4);
});
});
});
Loading

0 comments on commit 0076ccc

Please sign in to comment.