Skip to content

Commit

Permalink
Add Jest configuration, survey model, and enhance backend API with su…
Browse files Browse the repository at this point in the history
…rvey routes
  • Loading branch information
austenstone committed Oct 22, 2024
1 parent 0d9f847 commit 8cf8c76
Show file tree
Hide file tree
Showing 19 changed files with 472 additions and 49 deletions.
11 changes: 10 additions & 1 deletion backend/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
GITHUB_WEBHOOK_PROXY_URL=https://smee.io/SxQC6L1O80NWJqdL
# Databse configuration
MYSQL_HOST=db
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=octocat
MYSQL_DATABASE=value

# Base URL for the web server
WEB_URL=http://localhost

# GitHub App configuration
GITHUB_WEBHOOK_SECRET=bananas
GITHUB_APP_ID=1028384
GITHUB_APP_CLIENT_ID=Iv23ctKxsNlsTbAt3NJY
Expand Down
48 changes: 48 additions & 0 deletions backend/__tests__/survey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'dotenv/config'
import { Sequelize } from 'sequelize';
import Survey from '../src/models/survey.model'
import sequelize from '../src/database';

beforeAll(async () => {
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
await sequelize.sync({ force: true }); // Recreate the database schema
});

afterAll(async () => {
await sequelize.close();
});

describe('Survey Model', () => {
it('should create a survey with valid data', async () => {
const survey = await Survey.create({
daytime: new Date(),
userId: 1044,
usedCopilot: true,
pctTimesaved: 50,
timeUsedFor: 'Releases',
});

expect(survey).toBeDefined();
expect(survey.userId).toBe(1044);
expect(survey.usedCopilot).toBe(true);
expect(survey.pctTimesaved).toBe(50);
expect(survey.timeUsedFor).toBe('Releases');
});

it('should calculate timeSaved correctly', async () => {
const survey = await Survey.create({
daytime: new Date(),
userId: 1044,
usedCopilot: true,
pctTimesaved: 50,
timeUsedFor: 'Releases',
});

expect(survey.timeSaved).toBe('50% saved for Releases');
});
});
8 changes: 8 additions & 0 deletions backend/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};
142 changes: 142 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@
"main": "src/app.ts",
"scripts": {
"start": "tsx src/app.ts",
"test": "jest",
"build": "tsc",
"dev": "nodemon src/app.ts"
"dev": "nodemon src/app.ts",
"proxy": "npx smee -t http://127.0.0.1:3000/api/github/webhooks"
},
"dependencies": {
"@octokit/webhooks": "^13.3.0",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"eventsource": "^2.0.2",
"express": "^4.17.1",
"mysql2": "^3.11.3",
"octokit": "^4.0.2",
"sequelize": "^6.37.4",
"smee-client": "^2.0.3",
"sqlite3": "^5.0.2"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/eventsource": "^1.1.15",
"@types/express": "^4.17.13",
"@types/node": "^16.11.7",
Expand Down
5 changes: 4 additions & 1 deletion backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import apiRoutes from "./routes/index"
import { createNodeMiddleware } from "octokit";
import { setupWebhookListeners } from './controllers/webhook.controller';
import octokit from './services/octokit';
import cors from 'cors';

const app = express();
const PORT = Number(process.env.PORT) || 3000;

app.use(cors());

// Setup webhook listeners
setupWebhookListeners(octokit);
app.use(createNodeMiddleware(octokit));
Expand All @@ -19,5 +22,5 @@ app.use(bodyParser.urlencoded({ extended: true }));
app.use('/api', apiRoutes);

app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT} 🚀`);
console.log(`Server is running on port ${PORT}`);
});
Loading

0 comments on commit 8cf8c76

Please sign in to comment.