Skip to content

Commit

Permalink
updated readme + fixed packages
Browse files Browse the repository at this point in the history
  • Loading branch information
phucd5 committed Nov 28, 2023
1 parent 8b678a9 commit c51ee64
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 7 deletions.
110 changes: 104 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,108 @@ We are using TestFlight Internal to deploy Here Beta Version. To use this app:

Refer to API_DOC.md for API Deployment.

# Additonal Changes Instructions

### Important Note

When introducing changes, always create a new branch from the main branch. After implementing your changes, initiate a pull request and request a code review from a teammate. Following approval, merge your changes into the main branch. Ensure that your merge passes all checks in GitHub Actions to maintain build integrity.

### Adding Backend Routes
To add a new backend route, navigate to the `server` folder. In the `controllers` folder. Add a controller method corresponding to one of authentication (`auth.js`), message (`message.js`), reply (`reply.js`), and user (`user.js`). Then, navigate to the `routes` folder. Import the controller method and define/add the corresponding route.

1. **Plan the Endpoint:**
- Decide the HTTP method (GET, POST, PUT, DELETE) appropriate for the action.
- Choose a descriptive endpoint path that follows RESTful conventions and aligns with existing routes.

2. **Update or Create the Route File:**
- Locate the `routes` directory within the `server` folder.
- If a relevant route file (e.g., `message.js` for message-related features) exists, open it; otherwise, create a new `.js` file.

3. **Implement the Route:**
- Import necessary modules and middleware.
- Define the route using `router.METHOD(PATH, HANDLER)`.
- Use `handleSuccess`, `handleBadRequest`, `handleServerError`, and `handleNotFound` for response handling found in `utils`.

4. **Create Controller Functions:**
- Navigate to the `controllers` directory within the `server` folder.
- Create a new controller file or open an existing one that corresponds to the new route.
- Write a new controller function that includes:
- Input validation and sanitation.
- Interaction with the database through Mongoose models.
- Proper response with success or error messages.

5. **Testing:**
- Write unit tests for your new controller functions.
- For detailed instructions see the testing instructions below.
- Run tests to ensure your code behaves as expected.

6. **Documentation:**
- Comment your code to explain the functionality of routes and controllers.
- Update `API_DOC.md` the new endpoints and their usage.

### Example Route and Controller Implementation

```javascript
// In user.js route file
router.get("/:userId", getUserById);

// In user.js controller file
export const getUserById = async (req, res) => {
try {
const user = await UserModel.findById(req.params.userId);

if (!user) {
return handleNotFound(res, "User not found");
}

handleSuccess(res, user);
} catch (err) {
handleServerError(res, err);
}
};
```

### Adding Backend Tests
1. **Locate the Test File:** Navigate to the `__tests__` directory under `server`. Here, you'll find organized subdirectories for different aspects of the API, such as `controllers`, `models`, and `routes`.

2. **Create a Test File:** If a test file does not already exist in the section you edit, create one within the appropriate subdirectory. The file should be named after the feature with the suffix `Test.js`.

4. **Write Test Cases:**
- Import the necessary modules and mock dependencies.
- Use `describe` blocks to group related tests.
- Within each `describe` block, write individual `it` or `test` functions for each aspect of the feature you want to test.
- Use `expect` to assert expected outcomes.

5. **Mocking:**
- Mock any models or external calls that your feature interacts with to isolate the feature's functionality.
- Use `jest.mock()` to replace actual implementations with mock functions that return testable results.

6. **Running Tests:**
- To run your tests, use the command `npm test`.
- Ensure that your new tests pass without interfering with existing tests.

7. **Review and Refactor:**
- Review your tests to ensure they're comprehensive and cover all possible scenarios.

### Example

Here is an example of a basic test case:

```javascript
describe("Function", () => {
it("should return 400", async () => {
// Setup
// ...

// Execution
const result = await function();

// Assertion
expect(result).toBe(expectedResult);
});
});
```
# Testing
## Backend
To run our testing script in the backend, execute the following:
Expand All @@ -29,12 +131,6 @@ Current Coverge:
|---------|----------|---------|---------|-------------------|
| 89.72 | 91.89 | 85.18 | 89.86 | |
### Adding Backend Tests
To add a backend unit test, navigate to the `__test__` folder under `server`. You can choose to add a unit test to either one of the routes or the controllers using the `jest` framework.

### Adding Backend Routes
To add a new backend route, navigate to the `server` folder. In the `controllers` folder. Add a controller method corresponding to one of authentication (`auth.js`), message (`message.js`), reply (`reply.js`), and user (`user.js`). Then, navigate to the `routes` folder. Import the controller method and define/add the corresponding route.

## Frontend
To run our testing script in the frontend, execute the following command in XCode:
Expand All @@ -60,3 +156,5 @@ The bundle couldn’t be loaded. Try reinstalling the bundle.
We were told to not worry about getting 80% statement coverage for the frontend tests,
especially since we've met the requirement for backend tests. During break, we will
work on resolving this error and finishing the frontend tests.
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"helmet": "^7.0.0",
"mongoose": "^7.5.3",
"morgan": "^1.10.0",
"node-mocks-http": "^1.13.0"
"node-mocks-http": "^1.13.0",
"supertest": "^6.3.3"
},
"name": "server",
"version": "1.0.0",
Expand Down

0 comments on commit c51ee64

Please sign in to comment.