Open etong11 opened 2 months ago
Issues
0 New issues
0 Accepted issues
Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code
Changes Missing Coverage | Covered Lines | Changed/Added Lines | % | ||
---|---|---|---|---|---|
src/controllers/helpers.js | 33 | 39 | 84.62% | ||
<!-- | Total: | 33 | 39 | 84.62% | --> |
Totals | |
---|---|
Change from base Build 10607818019: | 0.006% |
Covered Lines: | 22336 |
Relevant Lines: | 25595 |
Code coverage is not 100% in the refactored code due to two spots:
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.
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").