Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User cont rm #107

Merged
merged 11 commits into from
Feb 6, 2024
85 changes: 84 additions & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,93 @@ The following outlines the various responses from the API.
"token": "string"
}
```
### Unit Test Details

## Unit Test Details
The unit tests for the both the notes and user apis are found in the `backend/test/note_api_.test.js`, `backend/test/user_api_.test.js`, and `backend/test/getTokenFrom.js`. This suite will test the application logic of the API to make sure it has the correct behavior & make sure we're getting the data we expect.

### Unit Test Implementation Special Case
The unit tests in `user_api_.test.js` test the `findUserById` api. You may notice these tests look different than the others.

This suite includes an experiemental mocking module from Jest that tests ECMAScript Modules. ESM evaluates static `import` statements before looking at code therefore the hoisting of `jest.mock` calls that happen in CJS won't work for ESM.

### In this test, we are executing an asynchronous setup function
```javascript
beforeAll(async () => {

})
```

### We then use the `jest.unstable_mockModule` to our `User` module to be used in our test. Since ES6 modules are hoisted, we need to set our mocks, prior to running code that uses the User model
```javascript
jest.unstable_mockModule('../models/User', () => ({...})
```
### Here we are insuring that the default export of the `User` module is imported
```javascript
jest.unstable_mockModule('../models/User', () => ({
default:{...}))

```
### Here we are mocking the findById function so that our mock user has a mock findById method as it does in the User module
```javascript
...{
findById: jest.fn().mockImplementation(id => {...}
```
### In the UserController, we call User.findById() and it can resolve to a value that findUserById uses for two different branches: user found or user not found.
### In our mock, we are using Promise.resolve to immediately return mock values that simulate each pathway. One where our user's id is in the DB, so we set the _id property to the User's id. The other where the user is not found and `null` is returned. Note that since the id is the only data needed for the scope of these tests, the other data that the User object may return is not mocked.
```javascript
...{
if (id === '123456789') {
return Promise.resolve({
_id: '123456789'})
} else {
return Promise.resolve(null)}
}
```
### Once mock User is set, we can dynamically import the UserController. Since the mock User was set prior to this import, our imported api can utilize the mock User in the test environment
```javascript
findUserById = (await import('../controllers/UserController')).findUserById
```
### In the first test, we want to test the happy pathway where a user is present in our DB. We are mocking a user that we want to query our db for. We are also mocking the express `req` HTTP request object and `res` HTTP response object as we are not actually making the api call to the database.
### We then call `findUserById` uses the value of `req.params.id` to similulate the call => `User.findById(req.params.id)`. In this test, we expect that a user is found and `res.json` will be called with be called be the correct 'user' from the database
```javascript
test('when valid ID return user ', async () => {
const mockUser = { _id: '123456789' }
const req = {
params: { id: '123456789' }
}
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis()
}

await findUserById(req, res)

expect(res.json).toHaveBeenCalledWith(mockUser)
})
```
### The second test, we are testing the other scenario, where our database does not have the user and `findUserById` responds with the appropriate error message and status. In this test, both assertions must be true in order for the test to pass
```javascript
test('when user id is not found, return 404', async () => {
const req = {
params: { id: 'ID NOT Found IN DB' }
}
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis()
}

await findUserById(req, res)

expect(res.json).toHaveBeenCalledWith({
error: 'user with id:ID NOT Found IN DB not found'
})
expect(res.status).toHaveBeenCalledWith(404)
})
```
19 changes: 0 additions & 19 deletions backend/tests/reverse.test.js

This file was deleted.

17 changes: 0 additions & 17 deletions backend/util/for_testing.js

This file was deleted.

Loading