Skip to content

Navigation Menu

Sign in
Sign up

Feat : added unit tests using keploy - #30

Open
TvisharajiK wants to merge 1 commit into
fossasia:master from
TvisharajiK:unit-tests
Open

Feat : added unit tests using keploy #30
TvisharajiK wants to merge 1 commit into
fossasia:master from
TvisharajiK:unit-tests

Conversation

@TvisharajiK

@TvisharajiK TvisharajiK commented Feb 5, 2025
edited by sourcery-ai Bot
Loading

Copy link
Copy Markdown

This pull request introduces comprehensive unit tests for the com_handler.js and drawingtool.js module, significantly increasing the coverage of the file.

The new tests cover various scenarios, ensuring the reliability and robustness of the functionalities.

Came across Knitweb while exploring Js based projects that had a good star count.

Let me know if these test cases have added any value

Screenshot 2025年02月05日 at 2 59 40 PM

Summary by Sourcery

Add unit tests for the com_handler.js and drawingtool.js modules.

Tests:

  • Added unit tests for the com_handler.js module, covering functions like refreshPorts, updateMachineType, configureKnit, createJob, and startKnit.
  • Added unit tests for the drawingtool.js module, focusing on mouse events and layout updates.

sourcery-ai Bot commented Feb 5, 2025
edited
Loading

Copy link
Copy Markdown

Reviewer's Guide by Sourcery

This pull request introduces unit tests for com_handler.js and drawingtool.js using Jest and JSDOM, aiming to improve code reliability and robustness. The changes include refactoring com_handler.js to a module for testability, adding Jest and related dependencies to package.json, and creating new test files with comprehensive test cases.

Class diagram showing the refactored ComHandler module

classDiagram
 class ComHandler {
 -String knit_job_id
 -Boolean knit_status
 -Boolean isStarted
 +refreshPorts()
 +updateMachineType()
 +configureKnit()
 +createJob()
 +startKnit()
 }
 note for ComHandler "Refactored from global functions to module pattern"
Loading

File-Level Changes

Change Details Files
Refactor com_handler.js to a module
  • Wrapped the code in com_handler.js into a module.
  • Exported the comHandler object to make its functions testable.
app/controller/com_handler.js
Add Jest and JSDOM test dependencies
  • Added jest, jest-environment-jsdom, jsdom, nodemon, and supertest as dev dependencies.
  • Added test scripts for running tests, watching tests, and generating coverage reports.
package.json
Implement unit tests for com_handler.js
  • Created com_handler.test.js with tests for refreshPorts, updateMachineType, createJob, configureKnit, and startKnit.
  • Used Jest to mock global functions and JSDOM to simulate the DOM environment.
  • Verified that the functions call the expected global functions and update the DOM as expected.
test/com_handler.test.js
Implement unit tests for drawingtool.js
  • Created drawingtool.test.js with tests for mouse events on the canvas.
  • Used Jest and JSDOM to simulate the DOM environment.
  • Verified that the functions correctly update the state based on mouse events.
test/drawingtool.test.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Author

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey @TvisharajiK - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider enhancing the auto-generated tests with more edge cases and error conditions. The current tests mainly cover happy paths but should also verify behavior with invalid inputs and error scenarios.
  • The drawingtool.test.js tests are quite basic - consider adding tests for different drawing operations, boundary conditions, and canvas state verification.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 2 issues found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

sourcery-ai[bot] reacted with thumbs up emoji sourcery-ai[bot] reacted with thumbs down emoji
var dataUrl = canvas.toDataURL('image/png');
var fileUrl = 'embedded';
var that = this;
setTimeout(function() {

@sourcery-ai sourcery-ai Bot Feb 5, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): Replace arbitrary setTimeout delay with proper async flow control

Using a fixed 1000ms delay could cause race conditions. Consider using Promises or async/await to properly sequence initKnitJob and configKnitJob calls.

sourcery-ai[bot] reacted with thumbs up emoji sourcery-ai[bot] reacted with thumbs down emoji
Comment thread test/com_handler.test.js
</select>
`);
global.document = dom.window.document;
global.window = dom.window;

@sourcery-ai sourcery-ai Bot Feb 5, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (testing): Missing edge case: empty previewTable

The test currently assumes there are rows in the previewTable. Add a test case where the table is empty to ensure colour_count is handled correctly when no rows are present. This will help verify the robustness of the configureKnit function in different scenarios.

sourcery-ai[bot] reacted with thumbs up emoji sourcery-ai[bot] reacted with thumbs down emoji
Comment thread test/com_handler.test.js
const canvas = document.getElementById('canvas2');
canvas.toDataURL = jest.fn(() => 'mockDataUrl');

comHandler.knit_job_id = 'mockJobId';

@sourcery-ai sourcery-ai Bot Feb 5, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (testing): Add test case for when knit_job_id is empty or null

It's important to test how the function behaves when knit_job_id is not set. This could reveal potential issues or unexpected behavior in the code when the ID is missing.

Suggested implementation:

 expect(configKnitJobMock).toHaveBeenCalledWith('mockJobId', 'mockDataUrl', 2, 'embedded');
});
it('should handle null knit_job_id appropriately', () => {
 const dom = new JSDOM(`
 <button id="configure-btn"></button>
 <button id="start-btn"></button>
 <table id="previewTable"><tr></tr><tr></tr></table>
 <canvas id="canvas2"></canvas>
 `);
 global.document = dom.window.document;
 global.window = dom.window;
 const canvas = document.getElementById('canvas2');
 canvas.toDataURL = jest.fn(() => 'mockDataUrl');
 comHandler.knit_job_id = null;
 comHandler.configureKnit();
 expect(document.getElementById('configure-btn').disabled).toBe(false);
 expect(document.getElementById('start-btn').disabled).toBe(true);
 expect(initKnitJobMock).not.toHaveBeenCalled();
 expect(configKnitJobMock).not.toHaveBeenCalled();
});
it('should handle empty knit_job_id appropriately', () => {
 const dom = new JSDOM(`
 <button id="configure-btn"></button>
 <button id="start-btn"></button>
 <table id="previewTable"><tr></tr><tr></tr></table>
 <canvas id="canvas2"></canvas>
 `);
 global.document = dom.window.document;
 global.window = dom.window;
 const canvas = document.getElementById('canvas2');
 canvas.toDataURL = jest.fn(() => 'mockDataUrl');
 comHandler.knit_job_id = '';
 comHandler.configureKnit();
 expect(document.getElementById('configure-btn').disabled).toBe(false);
 expect(document.getElementById('start-btn').disabled).toBe(true);
 expect(initKnitJobMock).not.toHaveBeenCalled();
 expect(configKnitJobMock).not.toHaveBeenCalled();
});

Note: These test cases assume that when knit_job_id is null or empty:

  1. The configure button should remain enabled
  2. The start button should be disabled
  3. Neither initKnitJob nor configKnitJob should be called

You may need to adjust the expected behaviors based on how your application actually handles these cases. If the actual behavior is different, modify the expectations in the test cases accordingly.

sourcery-ai[bot] reacted with thumbs up emoji sourcery-ai[bot] reacted with thumbs down emoji

@febuz febuz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for adding tests. I reviewed and tried running them locally; the Jest suite passes (8/8).

However, the refactor of "app/controller/com_handler.js" from standalone functions to a CommonJS module breaks the existing UI. The HTML buttons in app/view/index.html still call global functions:

  • onclick="refreshPorts()"
  • onclick="updateMachineType()"
  • onclick="configureKnit()"
  • onclick="startKnit()"

After this PR those functions are no longer in the global scope, so the buttons will throw ReferenceError in the browser.

To resolve this while keeping the tests working, add global window bindings at the bottom of com_handler.js:

I applied this fix locally and tests still pass. Please update the PR and I'll be happy to approve.

@febuz febuz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for adding tests. I reviewed and tried running them locally; the Jest suite passes (8/8).

However, the refactor of app/controller/com_handler.js from standalone functions to a CommonJS module breaks the existing UI. The HTML buttons in app/view/index.html still call global functions:

  • onclick="refreshPorts()"
  • onclick="updateMachineType()"
  • onclick="configureKnit()"
  • onclick="startKnit()"

After this PR those functions are no longer in the global scope, so the buttons will throw ReferenceError in the browser.

To resolve this while keeping the tests working, add global window bindings at the bottom of com_handler.js:

module.exports = comHandler;
if (typeof window !== 'undefined') {
 window.refreshPorts = comHandler.refreshPorts;
 window.updateMachineType = comHandler.updateMachineType;
 window.configureKnit = comHandler.configureKnit.bind(comHandler);
 window.createJob = comHandler.createJob.bind(comHandler);
 window.startKnit = comHandler.startKnit.bind(comHandler);
}

I applied this fix locally and tests still pass. Please update the PR and I'll be happy to approve.

febuz commented Jun 17, 2026

Copy link
Copy Markdown

Hi @TvisharajiK — I reviewed this and it needs one fix before merge: the refactor to a CommonJS module breaks the global onclick handlers in app/view/index.html. I opened #31 with the fix applied on top of your branch so it can be merged cleanly. If you prefer, you can apply the window-binding change from #31 to this PR and I'll close #31.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@sourcery-ai sourcery-ai[bot] sourcery-ai[bot] left review comments
+1 more reviewer
@febuz febuz febuz requested changes
Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /