Skip to content

Commit 44515ea

Browse files
committed
added userAuth testing file
1 parent 6cd2b6a commit 44515ea

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

__tests__/userAuth.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { sessionIsCreated, newUserIsCreated } from '../app/src/helperFunctions/auth';
2+
3+
describe('Login Tests', () => {
4+
let username;
5+
let password;
6+
7+
// Called under SignIn.tsx
8+
describe('sessionIsCreated', async () => {
9+
10+
it('returns the message \'No Username Input\' when no username is entered', async () => {
11+
username = '';
12+
password = 'codesmith'
13+
const result = await sessionIsCreated(username, password).then((loginStatus) => loginStatus);
14+
expect(result).toEqual('No Username Input');
15+
})
16+
17+
it('returns the message \'No Password Input\' when no password is entered', async () => {
18+
username = 'reactype';
19+
password = ''
20+
const result = await sessionIsCreated(username, password).then((loginStatus) => loginStatus);
21+
expect(result).toEqual('No Password Input');
22+
})
23+
24+
})
25+
26+
})
27+

app/src/helperFunctions/auth.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fetch = require("node-fetch");
2+
13
const isDev = process.env.NODE_ENV === 'development';
24
let serverURL = 'https://reactype.herokuapp.com';
35
if (isDev) {
@@ -12,7 +14,6 @@ export const sessionIsCreated = (
1214
username,
1315
password
1416
});
15-
console.log(process.env.NODE_ENV);
1617
const result = fetch(`${serverURL}/login`, {
1718
method: 'POST',
1819
credentials: 'include',
@@ -25,18 +26,15 @@ export const sessionIsCreated = (
2526
return res.json();
2627
})
2728
.then(data => {
28-
console.log('the data', data);
2929
if (data.sessionId && typeof data.sessionId === 'string') {
3030
// check that a session id was passed down
31-
console.log('Inside success');
3231
window.localStorage.setItem('ssid', data.sessionId);
3332
return 'Success';
3433
} else {
3534
return data; // error message returned from userController.verifyUser
3635
}
3736
})
3837
.catch(err => {
39-
console.log('Error while trying to login', err);
4038
return 'Error';
4139
});
4240
return result;
@@ -64,7 +62,6 @@ export const newUserIsCreated = (
6462
return res.json();
6563
})
6664
.then(data => {
67-
console.log('the data', data);
6865
if (data.sessionId && typeof data.sessionId === 'string') {
6966
// check that a session id was passed down
7067
window.localStorage.setItem('ssid', data.sessionId);
@@ -73,7 +70,6 @@ export const newUserIsCreated = (
7370
return data; // response is either Email Taken or Username Taken, refer to userController.createUser
7471
})
7572
.catch(err => {
76-
console.log(err);
7773
return 'Error';
7874
});
7975
return result;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
"lodash.merge": "^4.6.2",
145145
"lodash.throttle": "^4.1.1",
146146
"material-table": "^1.64.0",
147+
"node-fetch": "^2.6.0",
147148
"prettier": "^1.19.1",
148149
"prismjs": "^1.19.0",
149150
"prop-types": "^15.6.2",
@@ -174,6 +175,7 @@
174175
"@babel/preset-env": "^7.10.4",
175176
"@babel/preset-react": "^7.10.4",
176177
"@babel/preset-typescript": "^7.10.4",
178+
"@testing-library/react": "^10.4.6",
177179
"@types/chai": "^4.2.11",
178180
"@types/jest": "^25.1.5",
179181
"@types/mocha": "^8.0.0",

0 commit comments

Comments
 (0)