4

I'm trying to write my first unit test for a VueJS component that grabs some data from an API endpoint when it renders:

My VueJS component:

import axios from 'axios';

export default {
  props: {
          userIndexUrl: {
            required: true
          }
        },
  data() {
    userList: [],
    tableIsLoading: true
  },
  created() {
    const url = this.userIndexUrl;
    axios.get(url)
    .then(response => {
      this.userList = response.data.data;
      this.tableIsLoading = false;
    })
  },
}

and my test:

import { mount } from 'vue-test-utils'
import moxios from 'moxios'
import UsersAddRemove from 'users_add_remove.vue'

describe('UsersAddRemove', () => {
  const PROPS_DATA = {
      userIndexUrl: '/users.json'
  }

  beforeEach(() => {
    moxios.install();
    moxios.stubRequest('/users1.json', {
      status: 200,
      response: {
        "count": 1,
        "data": [
            { "id" : 1,
              "email" : "brayan@schaeferkshlerin.net",
              "first_name" : "Kenneth",
              "last_name" : "Connelly"
            }
          ]
        }
    });
  });

  afterEach(() => {
    moxios.uninstall();
  });

  it('loads users from remote JSON endpoint and renders table', () => {
    const wrapper = mount(UsersAddRemove, { propsData: PROPS_DATA });
    moxios.wait(() => {
      expect(wrapper.html()).toContain('<td class="">brayan@schaeferkshlerin1.net</td>');
      done();
    });
  });
});

So what I expect to happen is the component gets instantiated, after which it does an API call, which is caught by Moxios, after which it should execute moxios.wait, which isn't happening. The tests seem to ignore moxios.wait, and it passes successfully even when it shouldn't.

What am I missing here?

Constant Meiring
  • 3,285
  • 3
  • 40
  • 52

1 Answers1

7

Try adding the done as argument:

  it('loads users from remote JSON endpoint and renders table', (done) => {
  // -----------------------------------------------------------^^^^^^
    const wrapper = mount(UsersAddRemove, { propsData: PROPS_DATA });
    moxios.wait(() => {
      expect(wrapper.html()).toContain('<td class="">brayan@schaeferkshlerin1.net</td>');
      done();
    });
  });

Waiting for the Ajax

Change as follows:

// remove the stubRequest of beforeEach()
beforeEach(() => {
  moxios.install();
});

// afterEach stays the same

it('loads users from remote JSON endpoint and renders table', (done) => {
  const wrapper = mount(UsersAddRemove, { propsData: PROPS_DATA });

  moxios.wait(() => {
    let request = moxios.requests.mostRecent();
    request.respondWith({
      status: 200,
      response: {
        "count": 1,
        "data": [{
          "id": 1,
          "email": "brayan@schaeferkshlerin.net",
          "first_name": "Kenneth",
          "last_name": "Connelly"
        }]
      }
    }).then(function() {
      expect(wrapper.html()).toContain('<td class="">brayan@schaeferkshlerin1.net</td>');
      done();
    });
  });
});
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • Awesome, that did the trick thanks! However, now it doesn't actually wait for the AJAX call to complete. Any ideas on that? – Constant Meiring Mar 06 '18 at 12:26
  • 1
    The thing is your own `created()` doesn't wait for the ajax request (it simply invokes it); not that I know if it *could* wait for it. I will see if you can wait until the `stubRequest` is called. – acdcjunior Mar 06 '18 at 12:37
  • I have an idea. Don't know if it will work (can't test it), but give it a shot, would you? I added the `moxios.requests.mostRecent().then(() => {`. – acdcjunior Mar 06 '18 at 12:46
  • Another shot. Can you try it? – acdcjunior Mar 06 '18 at 13:07
  • Thanks! I've seen that method used in some examples, but it defies the point of creating stubRequests. Either way, I tried it and it still doesn't work. I'm beginning to think that something else may be the culprit here. You did answer the original question though! – Constant Meiring Mar 06 '18 at 14:09
  • You can try increasing the waiting time: `moxios.wait(() => { ... }, 500);` – acdcjunior Mar 06 '18 at 14:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166330/discussion-between-constant-meiring-and-acdcjunior). – Constant Meiring Mar 06 '18 at 14:30