Skip to content

Commit 829efc7

Browse files
committed
test changes
1 parent 552ae15 commit 829efc7

File tree

8 files changed

+3029
-37
lines changed

8 files changed

+3029
-37
lines changed

package-lock.json

Lines changed: 2785 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
"main": "index.js",
66
"scripts": {
77
"start": "node src/index.js",
8-
"dev": "env-cmd -f ./config/dev.env nodemon src/index.js -e *"
8+
"dev": "env-cmd -f ./config/dev.env nodemon src/index.js -e *",
9+
"test": "env-cmd -f ./config/test.env jest --watch"
10+
},
11+
"jest": {
12+
"testEnvironment": "node"
913
},
1014
"keywords": [],
1115
"author": "",
@@ -23,6 +27,8 @@
2327
},
2428
"devDependencies": {
2529
"env-cmd": "^10.0.1",
26-
"nodemon": "^1.19.1"
30+
"jest": "^24.9.0",
31+
"nodemon": "^1.19.1",
32+
"supertest": "^4.0.2"
2733
}
2834
}

src/app.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
const express = require('express')
2+
const bcrypt = require('bcryptjs')
3+
require('./db/mongoose.js')
4+
5+
const userRouter = require('./routers/users')
6+
const taskRouter = require('./routers/tasks')
7+
8+
const app=express()
9+
10+
11+
// {
12+
// lastvisit:null
13+
// util
14+
// }
15+
16+
17+
//Uploading Image to server
18+
//========================================
19+
// const multer = require('multer')
20+
// const upload = multer({
21+
// dest:'images',
22+
// limits:{
23+
// fileSize:1000000
24+
// },
25+
// fileFilter(req,file, cb){
26+
// if(!file.originalname.match(/\.(doc|docx|jpg)$/)){
27+
// return cb(new Error('Please Upload a word Document!'))
28+
// }
29+
// cb(undefined,true)
30+
// // cb(new Error('File must be '))
31+
// // cb(undefined,true)
32+
// // cb(undefined,false)
33+
34+
// }
35+
// })
36+
37+
38+
// app.post('/upload',upload.single('uploadfile'),(req,res)=>{
39+
// res.send()
40+
// }, (error, req, res, next)=>{
41+
// res.status(400).send({error: error.message})
42+
// })
43+
app.use(express.json())
44+
app.use(userRouter)
45+
app.use(taskRouter)
46+
47+
//MASTER SWITCH To Enable and Disable services
48+
//=================================================
49+
// app.use((req,res,next)=>{
50+
51+
// // console.log(req.method, req.path)
52+
// // next()
53+
// if(envStatus === false ){
54+
55+
// res.status(503).send('POST/GET/PATCH/DELETE requests are disabled')
56+
57+
// }else{
58+
// if(req.method === 'GET' || req.method === 'PATCH' || req.method ==='DELETE'){
59+
// res.status(400).send('GET/PATCH/DELETE requests are disabled')
60+
// }
61+
// else{
62+
// next()
63+
// }
64+
// }
65+
66+
// })
67+
68+
///Adding file update to Express.!!!!
69+
//=====================================================
70+
71+
72+
73+
74+
75+
76+
//Password Encryption example using bcyrpt library
77+
//=====================================================
78+
// const myfunc=async ()=>{
79+
// const pass='mypassword'
80+
// const hashpass=await bcrypt.hash(pass,8)
81+
// console.log(pass)
82+
// console.log(hashpass)
83+
// const isMatch = await bcrypt.compare('mypassword','$2a$08$9TxBexSjsy4QKiBj81rNLuqMuoUgzb/k/VgNvebgly66qH9NXJ1eG')
84+
// console.log(isMatch)
85+
// }
86+
87+
// myfunc()
88+
89+
//JWT Token example for generate Json Web token
90+
//==================================================
91+
// const jwt = require('jsonwebtoken')
92+
93+
// const myfunc = async()=>{
94+
// const token = jwt.sign({_id:'abcd123'},'thisismynewcourse',{expiresIn:'7 seconds'})
95+
// console.log(token)
96+
// const data=jwt.verify(token,'thisismynewcourse')
97+
// console.log(data)
98+
// }
99+
// myfunc()
100+
// //Base64 Json string Base64 Encoded (payload/body) contains the id'abcd123' signature used to verify the token
101+
// //part1{eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9}.part2{eyJfaWQiOiJhYmNkMTIzIiwiaWF0IjoxNTY1OTYwMzM3fQ}.part3{qh8LZF-AbfT7_AL9QR8kyve_E7bG5xqqwlwaNty6cd4}
102+
103+
104+
// const pet = {
105+
// name : 'Hal'
106+
// }
107+
108+
// pet.toJSON = function(){
109+
// console.log(this)
110+
// return this
111+
// }
112+
// console.log(JSON.stringify(pet))
113+
114+
// const Task = require ('./models/tasks')
115+
// const User = require('./models/users')
116+
117+
// const main = async ()=>{
118+
// // const task = await Task.findById('5d598bcd788beb0e04a6fc36')
119+
// // await task.populate('author').execPopulate()
120+
// // console.log(task.author)
121+
// const user = await User.findById('5d5af89e95437a20f0e07047')
122+
// await user.populate('task').execPopulate()
123+
// //console.log(user.task )
124+
125+
// }
126+
127+
// main()
128+
129+
// app.listen(port,()=>{
130+
// console.log('App is running on Port: '+port)
131+
// })
132+
133+
module.exports=app

src/index.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
const express = require('express')
2-
const bcrypt = require('bcryptjs')
3-
require('./db/mongoose.js')
4-
5-
const userRouter = require('./routers/users')
6-
const taskRouter = require('./routers/tasks')
7-
const envStatus = false
8-
const app=express()
1+
const app = require('./app')
2+
3+
// const express = require('express')
4+
// const bcrypt = require('bcryptjs')
5+
// require('./db/mongoose.js')
6+
7+
// const userRouter = require('./routers/users')
8+
// const taskRouter = require('./routers/tasks')
9+
// const envStatus = false
10+
// const app=express()
911
const port=process.env.PORT || 443
1012
// {
1113
// lastvisit:null
@@ -39,9 +41,9 @@ const port=process.env.PORT || 443
3941
// }, (error, req, res, next)=>{
4042
// res.status(400).send({error: error.message})
4143
// })
42-
app.use(express.json())
43-
app.use(userRouter)
44-
app.use(taskRouter)
44+
// app.use(express.json())
45+
// app.use(userRouter)
46+
// app.use(taskRouter)
4547

4648
//MASTER SWITCH To Enable and Disable services
4749
//=================================================

src/math.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const calculateTip =(total, percTip)=>{
2+
const tip = total * percTip
3+
return total+tip
4+
}
5+
6+
const fahrenheitToCelsius = (temp) => {
7+
return (temp - 32) / 1.8
8+
}
9+
10+
const celsiusToFahrenheit = (temp) => {
11+
return (temp * 1.8) + 32
12+
}
13+
14+
const add= (a,b)=>{
15+
return new Promise((resolve, reject)=>{
16+
setTimeout(()=>{
17+
if(a<0 || b<0){
18+
return reject('Number cannot be Negative')
19+
}
20+
resolve(a+b)
21+
},2000)
22+
})
23+
}
24+
25+
26+
module.exports ={
27+
calculateTip,
28+
fahrenheitToCelsius,
29+
celsiusToFahrenheit,
30+
add
31+
}
32+
33+
34+
35+

src/middleware/auth.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ const auth = async (req, res, next)=>{
1010
console.log(token)
1111
console.log(process.env.JWT_SECRET)
1212
const decoded = jwt.verify(token, process.env.JWT_SECRET)
13-
14-
console.log(decoded)
15-
console.log('test')
13+
// console.log(decoded)
14+
// console.log('test')
1615
const user = await User.findById({_id: decoded._id, 'tokens.token': token})
1716

1817
// const tokenExp = decoded.exp

tests/math.t1est.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const math = require('../src/math')
2+
3+
test ('Calculate total with TIP',()=>{
4+
const total = math.calculateTip(10,0.3)
5+
expect(total).toBe(13)
6+
// if(total !== 13){
7+
// throw new Error('Total TIP should be 13. Got'+total)
8+
// }
9+
})
10+
11+
12+
test('Add Function',(a)=>{
13+
math.add(2,3).then((sum)=>{
14+
expect(sum).toBe(5)
15+
a()
16+
})
17+
})
18+
19+
test('Should add 2 number async/await', async ()=>{
20+
const sum = await math.add(10,32)
21+
expect(sum).toBe(42)
22+
})
23+
24+
test('Fahrenheit to Celsius',()=>{
25+
const temp = math.fahrenheitToCelsius(32)
26+
expect(temp).toBe(0)
27+
28+
})
29+
30+
test('Celsiuc to Fahrenheit',()=>{
31+
const temp = math.celsiusToFahrenheit(0)
32+
expect(temp).toBe(32)
33+
})
34+
35+
test('Async Test Demo',()=>{
36+
setTimeout(()=>{
37+
expect(2).toBe(2)
38+
a()
39+
},2000)
40+
})
41+
// test('hello world',()=>{
42+
43+
// })
44+
45+
// test('this shoudl fail',()=>{
46+
// throw new Error('error')
47+
// })

tests/user.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const request = require('supertest')
2+
const app = require('../src/app')
3+
4+
test ('Should signup a new User',async ()=>{
5+
6+
})

0 commit comments

Comments
 (0)