반응형
간격이 있는 vueJS 라이프사이클 후크에서의 비동기 Jest 테스트
"before Create" 후크에서 다음과 같은 농담을 사용하여 "auth/refresh" 액션을 테스트하는 방법을 이해하고 싶습니다.
// 메인.표시하다
async beforeCreate() {
let authTokenRefreshIntervalId;
await this.$store.dispatch('auth/initialize');
authTokenRefreshIntervalId = setInterval(() => {
this.$store.dispatch('auth/refresh').catch(() => {
this.$store.dispatch('auth/logout');
clearInterval(authTokenRefreshIntervalId);
});
}, 30 * 1000);
}
// main.spec.disc
import Vue from 'vue';
import Vuex from 'vuex';
import { shallow, createLocalVue, mount } from '@vue/test-utils';
import Main from '@/main';
const localVue = createLocalVue();
jest.useFakeTimers();
describe('store-auth', () => {
let store;
let actions;
let getters;
beforeEach(() => {
actions = {
initialize: jest.fn(),
refresh: jest.fn(),
logout: jest.fn(),
};
getters = {
isAuthenticated: jest.fn(),
};
store = new Vuex.Store({
modules: {
auth: {
namespaced: true,
actions,
getters,
},
},
});
});
it('dispatch initialize on beforeCreate hook', () => {
const wrapper = shallow(Main, { store, localVue });
expect(actions.initialize).toHaveBeenCalled();
});
it('dispatch refresh on beforeCreate hook every 30s', () => {
const wrapper = shallow(Main, { store, localVue });
jest.runTimersToTime(30 * 1000);
expect(actions.refresh).toHaveBeenCalled();
});
});
조롱된 함수는 호출되지 않는다고 농담은 말한다.로 시도했다.expect(setInterval).toHaveBeenCalled()
시험에 합격했어요내가 틀린 곳이 어디지?
테스트에서도 비동기/대기 기능을 사용해 보십시오.
it('dispatch refresh on beforeCreate hook every 30s', async () => {
const wrapper = shallow(Main, { store, localVue });
jest.runTimersToTime(30 * 1000);
await expect(actions.refresh).toHaveBeenCalled();
});
언급URL : https://stackoverflow.com/questions/50253486/async-jest-test-in-vuejs-lifecycle-hook-with-interval
반응형
'programing' 카테고리의 다른 글
(A+B)의 FFT가 FFT(A) + FFT(B)와 다른 이유는 무엇입니까? (0) | 2022.08.01 |
---|---|
v-select에 사용자 지정 텍스트 입력 (0) | 2022.08.01 |
$http로 파일을 보내는 방법.Vue에 투고합니다.Js (0) | 2022.07.29 |
NuxtJ를 사용하여 네이티브 Vue 앱을 만들 수 있습니까?s (0) | 2022.07.29 |
Vuex 작업에서 Vuex getter로 매개 변수 전달 (0) | 2022.07.29 |