Как смоделировать переменные в действии магазина vuex с помощью jest - PullRequest
0 голосов
/ 03 июля 2019

У меня есть такой js-код:

const Repository = axios.create( {
  baseURL: process.env.VUE_APP_API_HOST
} );

const state = { ... }
const getters = { ... }
const mutations = { ... }


const actions = {
  fetch: async ( { commit, getters }, { apiPath, id } ) => {

    const response = await Repository.get( apiPath );

    commit( 'setArticleList', { response.data, id } );
    commit( 'changeApiStatus', { status: 'success', id } );
  }
};

export const articleList = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};

У меня есть такой jest-код:

import { store as store2 } from './store';

describe( 'store article_list test: ', () => {

  let store;
  beforeEach( () => {
    store = new Vuex.Store( store2 );
  } );

  describe( 'test actions', () => {
    test( 'get value correctly', async () => {
      const apiPath = 'api/v1/article_list.json';
      const id = 1;
        await store.dispatch( 'articleList/fetch', { apiPath, id } );
    } );
  } );


} );

Это код магазина:

store.JS

import Vue from 'vue';
import Vuex from 'vuex';
import { articleList } from '@/store/modules/article_list';

Vue.use( Vuex );

export const store = {
  modules: {
    articleList
  }
};
export default new Vuex.Store( store );

await store.dispatch( 'articleList/fetch', { apiPath, id } );

В настоящее время этот код имеет Error: Error: connect ECONNREFUSED 127.0.0.1: 8080 ошибка

Я хотел бы издеваться над этой частью const response = await Repository.get( apiPath );

Но у меня естьне знаю, как смоделировать переменные в действии vuex store с помощью jest.

Дайте мне знать, если вы знаете, как высмеивать эту часть.

Спасибо, что прочитали мой вопрос.

1 Ответ

1 голос
/ 03 июля 2019

Я использую moxios , чтобы высмеивать мои запросы axios, работает довольно хорошо, это позволяет вам устанавливать свои собственные данные ответа.

...