Skip to content

Commit e8b9e47

Browse files
authored
Merge pull request #17 from maxchistt/test
Test
2 parents 1311f9a + 3c16e0c commit e8b9e47

File tree

9 files changed

+4261
-609
lines changed

9 files changed

+4261
-609
lines changed

client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"scripts": {
3636
"start": "react-scripts start",
3737
"build": "react-scripts build",
38-
"test": "react-scripts test",
38+
"test": "react-scripts test a",
3939
"eject": "react-scripts eject"
4040
},
4141
"proxy": "http://localhost:5000",

client/src/App.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import React from 'react';
22
import { render, screen } from '@testing-library/react';
33
import App from './App';
44

5-
test('renders learn react link', () => {
5+
test('renders react logo text', () => {
66
render(<App />);
7-
const linkElement = screen.getByText(/learn react/i);
8-
expect(linkElement).toBeInTheDocument();
7+
const logoTextElement = screen.getByText(/Notes/i);
8+
expect(logoTextElement).toBeInTheDocument();
99
});

client/src/Shared/order.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { sortByOrder, calcOrder, fixOrders } from './order'
2+
3+
const noteTemplate = () => {
4+
return {
5+
id: "qweRty123",
6+
name: "note",
7+
color: "GREEN",
8+
text: "lorem ipsum adamet amer",
9+
order: 5
10+
}
11+
}
12+
13+
test('sortByOrder', () => {
14+
let note1 = noteTemplate()
15+
let note2 = noteTemplate()
16+
note1.order = 1
17+
note2.order = 2
18+
expect(sortByOrder(note2, note1)).toBe(1)
19+
expect(sortByOrder(note1, note2)).toBe(-1)
20+
expect(sortByOrder(note1, note1)).toBe(0)
21+
})
22+
23+
test('calcOrder', () => {
24+
let notesArr = []
25+
for (let i = 0; i < 10; i++) {
26+
notesArr[i] = noteTemplate()
27+
notesArr[i].id = String(Date.now() + i)
28+
notesArr[i].order = Number(i)
29+
}
30+
expect(calcOrder(notesArr)).toBe(10)
31+
notesArr.sort(() => Math.random() - 0.5)
32+
expect(calcOrder(notesArr)).toBe(10)
33+
})
34+
35+
test('fixOrders', () => {
36+
let notesArr = []
37+
for (let i = 0; i < 10; i++) {
38+
notesArr[i] = noteTemplate()
39+
notesArr[i].id = String(Date.now() + i)
40+
notesArr[i].order = Number(i * 10 * (Math.random() - 0.5))
41+
}
42+
notesArr.sort(() => Math.random() - 0.5)
43+
const fixedArr = fixOrders(notesArr)
44+
for (let i = 0; i < 10; i++) {
45+
expect(fixedArr[i].order).toBe(i)
46+
}
47+
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"prod": "npm run server:start-prod",
2222
"prod:predeploy": "npm run deploy && npm run prod",
2323
"prod:prebuild": "npm run client:build && npm run prod",
24-
"test": "echo \"Error: no test specified\" && exit 1",
24+
"test": "(echo you can also run 'test' in folders 'client' or 'server') && (concurrently \"cd client && npm test\" \"cd server && npm test\")",
2525
"heroku-postbuild": "npm run deploy",
2626
"mongo-ubuntu:update": "(sudo apt update && sudo apt upgrade)",
2727
"mongo-ubuntu:prepare": "(cd mongo/Ubuntu && chmod +x mongo_ubuntu_setup.sh && chmod +x mongo_ubuntu_install.sh)",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const authMiddleware = require("./auth.middleware");
2+
3+
test('block on incorrect token', done => {
4+
const req = {
5+
headers: {
6+
authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJraXJpZXNoa2kifQ.1rvQo1jjvB-mgTmxSGp7qWDvLGxpxY2ZHMe6qZfxOok'
7+
},
8+
url: "/example"
9+
}
10+
11+
const res = {
12+
status(code) {
13+
try {
14+
expect(code).toBe(401)
15+
return this
16+
} catch (error) {
17+
done(error)
18+
}
19+
},
20+
json(obj) {
21+
try {
22+
expect(obj).toEqual({ message: 'Нет авторизации' })
23+
done()
24+
} catch (error) {
25+
done(error)
26+
}
27+
}
28+
}
29+
30+
const next = () => {
31+
throw new Error('next')
32+
done()
33+
}
34+
35+
expect(() => authMiddleware(req, res, next)).not.toThrow('next')
36+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const httpsMiddleware = require("./https.middleware");
2+
3+
test('correct redirect', done => {
4+
const req = {
5+
header: query => {
6+
switch (query) {
7+
case 'x-forwarded-proto':
8+
return 'http'
9+
case 'host':
10+
return 'test.host'
11+
default:
12+
break
13+
}
14+
},
15+
url: "/example"
16+
}
17+
18+
const res = {
19+
redirect(address) {
20+
try {
21+
expect(address).toBe(`https://test.host/example`)
22+
done()
23+
} catch (error) {
24+
done(error)
25+
}
26+
}
27+
}
28+
29+
const next = done
30+
31+
httpsMiddleware(req, res, next)
32+
})

0 commit comments

Comments
 (0)