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

Refactoring helper formatApiResponse in src/controllers/helpers.js #576

Open
wants to merge 3 commits into
base: f24
Choose a base branch
from

Conversation

etong11
Copy link

@etong11 etong11 commented Sep 6, 2024

Refactoring helpers.formatApiResponse in src/controllers/helpers.js to reduce its Cognitive Complexity from 20 to the 15 allowed. This was done by replacing the nested switch and if statements with function calls.

Resolves #341

Sonar Cloud link: https://sonarcloud.io/project/issues?open=AZFmi__BybYwxy-_uEXS&id=CMU-313_NodeBB

Execution from UI

formatApiResponse can be triggered when the code returns an API response. For example, the function can be triggered from the home page of NodeBB when clicking on one of the categories displayed ("General Discussion").

Screenshot 2024-09-05 at 8 50 46 PM Screenshot 2024-09-05 at 8 46 27 PM

Copy link

sonarcloud bot commented Sep 6, 2024

@coveralls
Copy link

Pull Request Test Coverage Report for Build 10730488514

Details

  • 33 of 39 (84.62%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.006%) to 82.677%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/controllers/helpers.js 33 39 84.62%
Totals Coverage Status
Change from base Build 10607818019: 0.006%
Covered Lines: 22336
Relevant Lines: 25595

💛 - Coveralls

@etong11
Copy link
Author

etong11 commented Sep 6, 2024

Code coverage is not 100% in the refactored code due to two spots:
Screenshot 2024-09-05 at 11 44 41 PM
Screenshot 2024-09-05 at 11 45 02 PM
When looking through the existing tests, I could not find an existing file that contained tests for helper.formatApiResponse. There is an existing test file, api.js, that tests the API but only aspects such as whether it has a valid request body and routes, not whether the correct formatting was applied to the API response. I attempted to get the case of when the message is '[[error:user-banned]]' to run by creating a test in which a user had a ban applied to them but this did not invoke formatApiResponse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactoring code in src/controllers/helpers.js
2 participants