Skip to content

chore(refactor): add unit tests#1

Open
smarcet wants to merge 1 commit intomasterfrom
feature/unit-tests
Open

chore(refactor): add unit tests#1
smarcet wants to merge 1 commit intomasterfrom
feature/unit-tests

Conversation

@smarcet
Copy link
Contributor

@smarcet smarcet commented Mar 11, 2026

Summary by CodeRabbit

  • Chores

    • Added GitHub Actions workflows for automated building and testing on code pushes and pull requests.
    • Configured testing and build tooling infrastructure.
  • Tests

    • Added comprehensive unit test coverage for search results, suggestions, and pagination utilities.

@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

📝 Walkthrough

Walkthrough

This PR introduces GitHub Actions CI/CD workflows, refactors pagination and search result mapping logic from app.js into a new helpers module, establishes testing infrastructure with Jest and Babel configuration, and adds comprehensive unit tests for the new helpers module.

Changes

Cohort / File(s) Summary
CI/CD Workflows
.github/workflows/build.yml, .github/workflows/tests.yml
Added GitHub Actions workflows triggering on pushes and PRs to master branch. Build workflow runs make; Tests workflow runs yarn test. Both set up Node.js, cache Yarn, and install dependencies.
Application Refactoring
app/app.js, app/helpers.js
Extracted pagination and search result mapping logic from app.js into new helpers module. Added dependency on helpers in app.js factory function; app/helpers.js exports mapSearchResults, mapSuggestions, resetPagination, changePagination, and MAX_DETAIL_LEN constant.
Test & Build Configuration
babel.config.js, jest.config.js, package.json
Added Babel configuration for AMD-to-CommonJS transformation. Added Jest configuration targeting tests/**/*.test.js with jsdom environment. Updated package.json with dev dependencies (@babel/core, babel-jest, babel-plugin-transform-amd-to-commonjs, jest, jest-environment-jsdom) and changed test script to run jest.
Test Suite
tests/helpers.test.js
Comprehensive unit tests covering mapSearchResults (truncation, field preference), mapSuggestions (payload/term mapping), resetPagination (state initialization), and changePagination (page navigation, window shifting, bounds clamping).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~18 minutes

Poem

🐰 Hop hop, the helpers now stand tall,
Extracted logic, refactored for all,
With Jest and Babel paving the way,
Tests multiply like carrots in May,
This fluffy refactor makes my whiskers sway!

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'chore(refactor): add unit tests' is vague and incomplete. While it mentions adding unit tests, it obscures the substantial refactoring performed, including new helper modules, workflow configurations, and Babel/Jest setup. Consider a more descriptive title that captures the main changes, such as 'refactor: extract pagination and search helpers with test suite and CI setup'.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/unit-tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
app/app.js (1)

102-117: ⚠️ Potential issue | 🟠 Major

Reset the requested page before issuing a fresh search.

doSearch(that, true) still uses the previous page on Line 104 and only resets pagination on Line 117 after the response. If the user starts a new query from page > 1, the request fetches the wrong slice while the UI shows page 1 / results 1-10.

Proposed fix
 function doSearch(that, reset) {
     var term = that.get('term');
-    var page = that.get('page');
+    var page = reset ? 1 : that.get('page');
     var perPage = that.get('perPage');
     var url = 'https://'+that.baseUrl+'/api/public/v1/search/'+that.context;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/app.js` around lines 102 - 117, The search function doSearch currently
builds the request URL using the old page value then calls
helpers.resetPagination(that) after the AJAX completes; move/reset the page
before composing the URL when reset is true so the request uses page 1.
Specifically, in doSearch(where term,page,perPage,url are read) check the reset
flag and either set that.set('page', 1) or override the local page variable to 1
before constructing url and performing $.ajax; keep
helpers.resetPagination(that) for updating UI state after the response.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@app/helpers.js`:
- Around line 22-39: In resetPagination, explicitly handle the total === 0 case:
at the top of the function (inside resetPagination) check if total === 0 and
then set pagesToShow to an empty array, set page to 0 (or another sentinel for
“no page”), and set fromResult and toResult both to 0, then return; otherwise
continue the existing logic that computes totalPages, lastPage, newPagesToShow,
etc.; update references to pagesToShow, page, fromResult, and toResult
accordingly so the UI never shows an impossible "1-0 of 0" state.

---

Outside diff comments:
In `@app/app.js`:
- Around line 102-117: The search function doSearch currently builds the request
URL using the old page value then calls helpers.resetPagination(that) after the
AJAX completes; move/reset the page before composing the URL when reset is true
so the request uses page 1. Specifically, in doSearch(where
term,page,perPage,url are read) check the reset flag and either set
that.set('page', 1) or override the local page variable to 1 before constructing
url and performing $.ajax; keep helpers.resetPagination(that) for updating UI
state after the response.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ff28e8cc-8f1b-4b7c-9a19-45ac9b5eb99a

📥 Commits

Reviewing files that changed from the base of the PR and between 086fafc and 8ebdaf9.

⛔ Files ignored due to path filters (2)
  • embed.min.js is excluded by !**/*.min.js
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (8)
  • .github/workflows/build.yml
  • .github/workflows/tests.yml
  • app/app.js
  • app/helpers.js
  • babel.config.js
  • jest.config.js
  • package.json
  • tests/helpers.test.js

Comment on lines +22 to +39
function resetPagination(that) {
var total = that.get('total');
var perPage = that.get('perPage');
var totalPages = Math.ceil(total / perPage);
var lastPage = (totalPages < 5) ? totalPages : 5;
var newPagesToShow = [];

for (var i = 1; i <= lastPage; i++) {
newPagesToShow.push(i);
}

that.set('page', 1);
that.set('pagesToShow', newPagesToShow);

var newResultLimit = perPage;
newResultLimit = (newResultLimit > total) ? total : newResultLimit;
that.set('fromResult', 1);
that.set('toResult', newResultLimit);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Handle the zero-results case explicitly.

When total is 0, Line 38 sets fromResult to 1 while Line 39 sets toResult to 0, and pagesToShow is left empty. That produces an impossible 1-0 of 0 state for empty searches.

Proposed fix
 function resetPagination(that) {
     var total = that.get('total');
     var perPage = that.get('perPage');
+
+    if (total === 0) {
+        that.set('page', 1);
+        that.set('pagesToShow', []);
+        that.set('fromResult', 0);
+        that.set('toResult', 0);
+        return;
+    }
+
     var totalPages = Math.ceil(total / perPage);
     var lastPage = (totalPages < 5) ? totalPages : 5;
     var newPagesToShow = [];
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function resetPagination(that) {
var total = that.get('total');
var perPage = that.get('perPage');
var totalPages = Math.ceil(total / perPage);
var lastPage = (totalPages < 5) ? totalPages : 5;
var newPagesToShow = [];
for (var i = 1; i <= lastPage; i++) {
newPagesToShow.push(i);
}
that.set('page', 1);
that.set('pagesToShow', newPagesToShow);
var newResultLimit = perPage;
newResultLimit = (newResultLimit > total) ? total : newResultLimit;
that.set('fromResult', 1);
that.set('toResult', newResultLimit);
function resetPagination(that) {
var total = that.get('total');
var perPage = that.get('perPage');
if (total === 0) {
that.set('page', 1);
that.set('pagesToShow', []);
that.set('fromResult', 0);
that.set('toResult', 0);
return;
}
var totalPages = Math.ceil(total / perPage);
var lastPage = (totalPages < 5) ? totalPages : 5;
var newPagesToShow = [];
for (var i = 1; i <= lastPage; i++) {
newPagesToShow.push(i);
}
that.set('page', 1);
that.set('pagesToShow', newPagesToShow);
var newResultLimit = perPage;
newResultLimit = (newResultLimit > total) ? total : newResultLimit;
that.set('fromResult', 1);
that.set('toResult', newResultLimit);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/helpers.js` around lines 22 - 39, In resetPagination, explicitly handle
the total === 0 case: at the top of the function (inside resetPagination) check
if total === 0 and then set pagesToShow to an empty array, set page to 0 (or
another sentinel for “no page”), and set fromResult and toResult both to 0, then
return; otherwise continue the existing logic that computes totalPages,
lastPage, newPagesToShow, etc.; update references to pagesToShow, page,
fromResult, and toResult accordingly so the UI never shows an impossible "1-0 of
0" state.

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.

1 participant