kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Migrate Device content tree tests to Vue Testing Library

Open rtibbles opened this issue 9 months ago • 21 comments

🙂 Looking for an issue? Welcome! This issue is open for contribution. If this is the first time you’re requesting an issue, please:

  • Read Contributing guidelines carefully. Pay extra attention to Using generative AI. Pull requests and comments that don’t follow the guidelines won’t be answered.
  • Confirm that you’ve read the guidelines in your comment.

Overview

Complexity: Low

Context

The test suites for the Device plugin's content tree browsing components currently use @vue/test-utils, which couples tests to component internals.

The Change

Refactor these test files to use Vue Testing Library (VTL) and write tests that reflect how a user interacts with the content tree:

  • kolibri/plugins/device/frontend/views/__tests__/ContentNodeRow.spec.js
  • kolibri/plugins/device/frontend/views/__tests__/ContentTreeViewer.spec.js
  • kolibri/plugins/device/frontend/views/__tests__/SelectContentPage.spec.js

Do not refactor any other test files or areas of the codebase.

Acceptance Criteria

  • [ ] All three files migrated to use @testing-library/vue
  • [ ] No residual @vue/test-utils imports in migrated files
  • [ ] Cover all major workflows while avoiding excessive testing of details
  • [ ] Concise but explicit test descriptions that describe what's tested from a user point of view
  • [ ] Follow Testing Library principles and Testing Library query priority recommendation
  • [ ] Use patterns consistent with existing VTL test suites (search for from '@testing-library/vue')
  • [ ] All migrated tests pass: pnpm run test-jest -- --testPathPattern device

References

🤖 This issue was written with AI assistance, under supervision, review and final edits by @rtibbles 🤖

rtibbles avatar Feb 26 '26 17:02 rtibbles

Hi @rtibbles, I would like to work on this issue. I have read the Contributing guidelines. Could you please assign it to me? Thanks!

aadityasingh9601 avatar Mar 07 '26 04:03 aadityasingh9601

I have assigned you @aadityasingh9601

rtibbles avatar Mar 07 '26 05:03 rtibbles

I have assigned you @aadityasingh9601

Thanks! I am starting to work on it & will follow up with you!

aadityasingh9601 avatar Mar 07 '26 06:03 aadityasingh9601

Hi @rtibbles,

While migrating the ContentNodeRow.spec.js file from @vue/test-utils to @testing-library/vue, I came across these two tests.

  it('when props.checked, the checkbox is checked', () => {
    renderComponent({
      disabled: true,
      checked: true,
    });

    // For some reason, the HTML for the actual checkbox does not have checked attribute
    const { KCheckbox } = getElements(wrapper);
    expect(KCheckbox().props().checked).toEqual(true);
  });

  it('when props.determinate, the checkbox is indeterminate', () => {
    renderComponent({
      disabled: true,
      checked: true,
      indeterminate: true,
    });
    const { KCheckbox } = getElements(wrapper);
    expect(KCheckbox().props().indeterminate).toEqual(true);
  });

In the first test, there's a comment, "For some reason, the HTML for the actual checkbox does not have checked attribute".

My query is, are we using KCheckBox here instead of a normal checkbox, just because it doesn't have checked attribute? Same goes for the second test.

Also, some tests use props() method that exists in @vue/test-utils but not in @testing-library/vue, so how do we migrate those tests?

Do we keep using the props() method from @vue/test-utils or do it indirectly by asserting the rendered output or behavior that results from those props?

aadityasingh9601 avatar Mar 13 '26 12:03 aadityasingh9601

Hi @aadityasingh9601, basically the main point of using VTL is to have tests describe & test from user point of view. So the best way to resolve this is to run the development server, preview the feature as a user and then describe & test it in the same way. You don't need to feel limited by the previous implementation ~ only use it as pointers into which areas to look at.

MisRob avatar Mar 16 '26 07:03 MisRob

Hi @aadityasingh9601, basically the main point of using VTL is to have tests describe & test from user point of view. So the best way to resolve this is to run the development server, preview the feature as a user and then describe & test it in the same way. You don't need to feel limited by the previous implementation ~ only use it as pointers into which areas to look at.

Thanks @MisRob, it was helpful & cleared up a lot of things!

aadityasingh9601 avatar Mar 16 '26 15:03 aadityasingh9601

Hi @aadityasingh9601, basically the main point of using VTL is to have tests describe & test from user point of view. So the best way to resolve this is to run the development server, preview the feature as a user and then describe & test it in the same way. You don't need to feel limited by the previous implementation ~ only use it as pointers into which areas to look at.

Thanks @MisRob, it was helpful & cleared up a lot of things!

Small update: The work is almost done & I will create a PR soon!

aadityasingh9601 avatar Mar 23 '26 06:03 aadityasingh9601

can i work on this issue?

sebinbabu5555 avatar Mar 24 '26 17:03 sebinbabu5555

Hi! 👋

Thanks so much for your interest! This issue is already assigned. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues. If there are no unassigned 'help wanted' issues available, please wait until new ones are added.

We really appreciate your willingness to help. 😊

Are you preparing for Google Summer of Code? See our GSoC guidelines.

learning-equality-bot avatar Mar 24 '26 17:03 learning-equality-bot

Hi @sebinbabu5555 ~ instead of posting so many comments, would you take a while and check the guidelines and the responses you got from our info bot?

MisRob avatar Mar 24 '26 17:03 MisRob

Hi @MisRob , recently I was just a little busy in writing the GSoC proposal, that's why this got a bit delayed, but I am just wrapping up the work now!

aadityasingh9601 avatar Apr 06 '26 07:04 aadityasingh9601

Hi @rtibbles,

While migrating this test to testing-library, I noticed that KIcon inside ContentNodeRow.vue component doesn't have a data-testid or any accessible attribute either, that testing-library can query. So, in order to migrate this properly, we would either need to add data-testid to KIcon in the or expose an accessible label.

Some existing test files that are migrated already migrated to testing-library/vue use data-testid to access the icons. For example, packages/kolibri/components/CoreMenu/__tests__/CoreMenuOption.spec.js.

So should I also assign a data-testid="icon-${icon}" to the KIcon inside the ContentNodeRow.vue component?

    it('uses the course icon instead of topic icon for course nodes', () => {
      const wrapper = makeWrapper({
        node: {
          title: 'My Course',
          kind: 'topic',
          modality: 'COURSE',
          id: 'course_1',
        },
      });
      const kIcons = wrapper.findAllComponents({ name: 'KIcon' });
      const courseIcon = kIcons.wrappers.find(w => w.props('icon') === 'course');
      expect(courseIcon).toBeTruthy();
      expect(wrapper.findComponent({ name: 'ContentIcon' }).exists()).toBe(false);
    });

aadityasingh9601 avatar Apr 22 '26 07:04 aadityasingh9601

Hi @rtibbles,

While migrating the tests to testing-library/vue in the ContentTreeViewer.spec.js file, I came across this test.

it('child nodes are annotated with their full path', () => {
    store.state.manageContent.wizard.path = [
      { id: 'channel_1', title: 'Channel 1' },
      { id: 'topic_1', title: 'Topic 1' },
    ];
    const wrapper = makeWrapper({ store });
    wrapper.vm.annotatedChildNodes.forEach(n => {
      const expectedPath = [
        { id: 'channel_1', title: 'Channel 1' },
        { id: 'topic_1', title: 'Topic 1' },
        { id: n.id, title: n.title },
      ];
      expect(n.path).toEqual(expectedPath);
    });
  });

This seems to be testing only internal computed data rather than user-visible behavior, I couldn't find any direct UI change. So, keeping the testing-library perspective in mind, how should we proceed here ?

  1. Keep this test as it is (using vue/test-utils).
  2. Move the logic to a function & test it separately?
  3. Remove it altogether?
  4. Or is there a UI behavior that I am missing here?

I will adjust the test accordingly.

aadityasingh9601 avatar Apr 23 '26 05:04 aadityasingh9601

Hi @aadityasingh9601. We've just merged few new conventions & related linting rules to develop (https://github.com/learningequality/kolibri/pull/14579). Please make sure to fetch the latest develop, rebase your working branch, and then run lint & fix if anything is reported. Here is guidance on how to fix reported errors:

As for your questions: (1) It's fine to use data-testid for icons (2) Please double-check whether annotation has any impact of UI, even indirect & if so + if it's something important, feel free to write a different test. If not, just remove it. I recommend you mention it in your PR for a reviewer.

Thanks!

MisRob avatar Apr 23 '26 08:04 MisRob

Hi @aadityasingh9601. We've just merged few new conventions & related linting rules to develop (#14579). Please make sure to fetch the latest develop, rebase your working branch, and then run lint & fix if anything is reported. Here is guidance on how to fix reported errors:

As for your questions: (1) It's fine to use data-testid for icons (2) Please double-check whether annotation has any impact of UI, even indirect & if so + if it's something important, feel free to write a different test. If not, just remove it. I recommend you mention it in your PR for a reviewer.

Thanks!

Hi @MisRob, thanks for the update. I will make sure to fetch the latest develop & rebase my working branch & follow the new rules & conventions for the tests.

Plus, I will make sure to check if the annotation has any UI impact & proceed accordingly.

Thanks!

aadityasingh9601 avatar Apr 24 '26 05:04 aadityasingh9601

Hi @MisRob,

Kindly take a look at this test in ContentTreeViewer.spec.js file.

describe('toggling "select all" checkbox', () => {
    const sanitizeNode = omit(['message', 'checkboxType', 'disabled', 'children']);
    it('if unchecked, clicking the "Select All" for the topic triggers an "add node" action', () => {
      // Selected w/ unselected child scenario
      setIncludedNodes([makeNode('topic_1', { total_resources: 1000 })]);
      setOmittedNodes([makeNode('subtopic_1', { path: [{ id: 'topic_1', title: '' }] })]);
      const wrapper = makeWrapper({ store });
      const { selectAllCheckbox, addNodeForTransferMock } = getElements(wrapper);
      const addNodeMock = addNodeForTransferMock();
      selectAllCheckbox().trigger('click');
      expect(addNodeMock).toHaveBeenCalledTimes(1);
      expect(addNodeMock).toHaveBeenCalledWith(
        expect.objectContaining(sanitizeNode(wrapper.vm.annotatedTopicNode)),
      );
    });

    it('if topic is checked, clicking the "Select All" for the topic triggers a "remove node" action', () => {
      setIncludedNodes([makeNode('topic_1')]);
      const wrapper = makeWrapper({ store });
      const { selectAllCheckbox, removeNodeForTransferMock } = getElements(wrapper);
      const removeNodeMock = removeNodeForTransferMock();
      selectAllCheckbox().trigger('click');
      expect(removeNodeMock).toHaveBeenCalledTimes(1);
      expect(removeNodeMock).toHaveBeenCalledWith(
        expect.objectContaining(sanitizeNode(wrapper.vm.annotatedTopicNode)),
      );
    });
  });

These 2 tests only check the implementation details here.

But the core UI behavior of these from toggling the "Select all" checkbox is --

Toggling the "Select all" checkbox by clicking it checks & unchecks the "Select all" checkbox plus all of the child nodes checkboxes too.

But there's an issue here. In order to test this UI behavior in test files, we need the actual addNodeForTransfer & removeNodeForTransfer functions access in this test file, currently their behavior is just mocked, with the actual functions access, clicking on the "Select all" checkbox just unchecks/unchecks it, but there's no effect on the child nodes checkboxes state.

So how should I proceed with this one?

  1. Migrate to testing-library but just mock the functions & test the implementation details just like shown in the above tests.
  2. Test only the "Select all" checkbox toggling state & not the child nodes checkboxes, but this has weaker test coverage, it doesn't fully tests the UI behavior as seen by user.
  3. Or is there something else can be done here that I might be missing?

Kindly guide me what should be done here & I'll proceed accordingly.

Thanks!

aadityasingh9601 avatar Apr 28 '26 07:04 aadityasingh9601

Hi @aadityasingh9601, it's fine to mock Vuex actions (such as add/removeNodeMock) and check that they were called on a particular user interaction. When we talk about not testing component implementation details, that refers to an internal logic of a particular component (e.g. its methods, computed, ..) These are still unit tests & we need to keep them focused on a component itself. Generally where unit tests begin and where they end is somewhat gray area. In open-source codebases I recommend you spend time previewing other tests and use them as a reference ~ also please do so if you had another question. There are many VTL examples available in the codebase and you can be confident about using them as guidance. If there's anything to be fine-tuned still, we can address it during PR review. Thanks you.

MisRob avatar Apr 28 '26 09:04 MisRob

In order to test this UI behavior in test files, we need the actual addNodeForTransfer & removeNodeForTransfer functions access in this test file, currently their behavior is just mocked, with the actual functions access, clicking on the "Select all" checkbox just unchecks/unchecks it, but there's no effect on the child nodes checkboxes state.

As for this, you will have to investigate why there's no effect on the child nodes. Try to find the most feasible way to resolve it in a way that is as best as possible aligned with https://kolibri-dev.readthedocs.io/en/develop/frontend_architecture/unit_testing.html & existing VTL tests. If you get stuck for too long, you can raise the issue in your pull request.

MisRob avatar Apr 28 '26 09:04 MisRob

Hi @aadityasingh9601, it's fine to mock Vuex actions (such as add/removeNodeMock) and check that they were called on a particular user interaction. When we talk about not testing component implementation details, that refers to an internal logic of a particular component (e.g. its methods, computed, ..) These are still unit tests & we need to keep them focused on a component itself. Generally where unit tests begin and where they end is somewhat gray area. In open-source codebases I recommend you spend time previewing other tests and use them as a reference ~ also please do so if you had another question. There are many VTL examples available in the codebase and you can be confident about using them as guidance. If there's anything to be fine-tuned still, we can address it during PR review. Thanks you.

Really thankyou so much for this @MisRob. This really cleared up my confusion about the "testing implementation details", I'll make sure to preview existing test files first & then use them as guidance for the migration, the work is all done by the way, this is the last test that was left.

aadityasingh9601 avatar Apr 29 '26 04:04 aadityasingh9601

In order to test this UI behavior in test files, we need the actual addNodeForTransfer & removeNodeForTransfer functions access in this test file, currently their behavior is just mocked, with the actual functions access, clicking on the "Select all" checkbox just unchecks/unchecks it, but there's no effect on the child nodes checkboxes state.

As for this, you will have to investigate why there's no effect on the child nodes. Try to find the most feasible way to resolve it in a way that is as best as possible aligned with https://kolibri-dev.readthedocs.io/en/develop/frontend_architecture/unit_testing.html & existing VTL tests. If you get stuck for too long, you can raise the issue in your pull request.

Got it, Thanks! I will do that.

aadityasingh9601 avatar Apr 29 '26 04:04 aadityasingh9601