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 2f9335d commit f0260cd
Show file tree
Hide file tree
Showing 7 changed files with 3,968 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();
53 changes: 53 additions & 0 deletions objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
- Title: "JavaScript: The Definitive Guide"
- Author: "David Flanagan"
- Published Year: 2020
- Genre: "Programming"
*/

// 1) Using `createBook` function return an object named using curly braces {} that includes the information above.
function createBook() {
// write your code here...
}

// DO NOT CHANGE THE LINE OF CODE BELOW (you can use it for testing your code)
const book = createBook();

// 2) Using `printBookTitleAndYear` function return the book’s title with its publish year separated by space. Access the book title using dot-notation, and access the publish year using bracket-notation.
function printBookTitleAndYear(book) {
// write your code here...
}

// 3) Using `addPageCount` function modify `book` argument by adding a property 'pageCount' with the value 1096 to the 'book' argument, and return `book`.
function addPageCount(book, pageCount) {
// write your code here...
}

// 4) Using `addISBN` function modify book argument by adding ISBN number, and return `book`. Add a property of 'ISBN' with a value of "978-1491952023".
function addISBN(book, ISBN) {
// write your code here...
}

// 5) Using `addUpdateYear` function modify `book` argument by changing the 'publishedYear' to 2021 as a new edition has been released, and return `book`.
function addUpdateYear(book, newYear) {
// write your code here...
}

// 6) Using `modifyAuthor` function modify `book` argument by modifying the 'author' property to hold an array of two authors: "David Flanagan" and "Another Author", and return `book`
function modifyAuthor(book, additionalAuthor) {
// write your code here...
}
// 7) Using `addReviews` function modify `book` argument by adding a 'reviews' property to the 'book' argument, which will store an array of `review` objects. Each `review` object should have 'reviewer' and 'comment' properties. Start with one review: {reviewer: "Book Critic", comment: "A comprehensive guide to JavaScript."}.
function addReviews(book, reviews) {
// write your code here...
}

module.exports = {
createBook,
printBookTitleAndYear,
addPageCount,
addISBN,
addUpdateYear,
modifyAuthor,
addReviews,
};
77 changes: 77 additions & 0 deletions objects.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const {
createBook,
printBookTitleAndYear,
addPageCount,
addISBN,
addUpdateYear,
modifyAuthor,
addReviews,
} = require("./objects.js");

describe("Book Object Manipulations", () => {
let book;

beforeEach(() => {
book = createBook();
});

describe("Create Book", () => {
it("should create a book object with specified properties", () => {
expect(book).toEqual({
title: "JavaScript: The Definitive Guide",
author: "David Flanagan",
publishedYear: 2020,
genre: "Programming",
});
});
});

describe("Print Book Title and Year", () => {
it("should return the book title and published year as a string", () => {
expect(printBookTitleAndYear(book)).toBe(
"JavaScript: The Definitive Guide 2020"
);
});
});

describe("Add Page Count", () => {
it("should add a pageCount property to the book", () => {
const updatedBook = addPageCount(book, 1096);
expect(updatedBook.pageCount).toBe(1096);
});
});

describe("Add ISBN", () => {
it("should add an ISBN to the book", () => {
const updatedBook = addISBN(book, "978-1491952023");
expect(updatedBook.ISBN).toBe("978-1491952023");
});
});

describe("Update Published Year", () => {
it("should update the published year of the book", () => {
const updatedBook = addUpdateYear(book, 2021);
expect(updatedBook.publishedYear).toBe(2021);
});
});

describe("Modify Author", () => {
it("should modify the author property to include an additional author", () => {
const updatedBook = modifyAuthor(book, "Another Author");
expect(updatedBook.author).toEqual(["David Flanagan", "Another Author"]);
});
});

describe("Add Reviews", () => {
it("should add reviews to the book", () => {
const reviews = [
{
reviewer: "Book Critic",
comment: "A comprehensive guide to JavaScript.",
},
];
const updatedBook = addReviews(book, reviews);
expect(updatedBook.reviews).toEqual(reviews);
});
});
});
Loading

0 comments on commit f0260cd

Please sign in to comment.