Skip to content

Commit

Permalink
test: spyon console warn and error at setup (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
seaneez authored Mar 18, 2024
1 parent 385eaf2 commit e2e2553
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from 'vitest';
import { describe, expect, it } from 'vitest';
import { mount } from '@vue/test-utils';
import VsStepper from '@/components/vs-stepper/VsStepper.vue';
import { mockConsoleError } from '@/test/setup';

function mountComponent() {
return mount(VsStepper);
Expand All @@ -26,8 +27,6 @@ describe('vs-stepper', () => {

it('props steps에 전달된 string 배열이 중복되면 validator가 false를 리턴한다', () => {
// given
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

const steps = ['step1', 'step1'];
const wrapper = mount(VsStepper, {
props: {
Expand All @@ -37,10 +36,7 @@ describe('vs-stepper', () => {

// then
expect(wrapper.vm.$options.props.steps.validator?.(steps)).toBe(false);
expect(consoleSpy).toHaveBeenCalledTimes(1);

// clear
consoleSpy.mockRestore();
expect(mockConsoleError).toHaveBeenCalledTimes(2);
});

it('각 스텝의 step slot을 통해 step에서 표시할 내용을 커스터마이징할 수 있다', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from 'vitest';
import { describe, expect, it } from 'vitest';
import { mount } from '@vue/test-utils';
import VsTabs from '../VsTabs.vue';
import { mockConsoleError } from '@/test/setup';

function mountComponent() {
return mount(VsTabs);
Expand All @@ -25,8 +26,6 @@ describe('vs-tabs', () => {

it('props tabs에 전달된 string 배열이 중복되면 validator 가 false 를 리턴한다', () => {
// given
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

const tabs = ['tab1', 'tab1'];

const wrapper = mount(VsTabs, {
Expand All @@ -37,10 +36,7 @@ describe('vs-tabs', () => {

// then
expect(wrapper.vm.$options.props.tabs.validator?.(tabs)).toBe(false);
expect(consoleSpy).toHaveBeenCalledTimes(1);

// clear
consoleSpy.mockRestore();
expect(mockConsoleError).toHaveBeenCalledTimes(2);
});

it('각 탭의 slot을 통해 탭을 커스터마이징 할 수 있다', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { toastPlugin } from '..';
import { store } from '@/stores';

import type { ToastInfo } from '@/plugins';
import { mockConsoleError, mockConsoleWarn } from '@/test/setup';

describe('toast-plugin', () => {
describe('toastPlugin', () => {
Expand Down Expand Up @@ -62,7 +63,6 @@ describe('toast-plugin', () => {

it('UIState 가 error 인 toast 를 보여줄 수 있다', () => {
// given
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
vi.spyOn(store.toast, 'addToast').mockImplementation(() => vi.fn());

// when
Expand All @@ -71,15 +71,11 @@ describe('toast-plugin', () => {
// then
expect(store.toast.addToast).toHaveBeenCalledTimes(1);
expect(document.querySelectorAll('.vs-toast-view').length).toBe(1);
expect(consoleSpy).toHaveBeenCalledTimes(1);

// clear
consoleSpy.mockRestore();
expect(mockConsoleError).toHaveBeenCalledTimes(1);
});

it('UIState 가 warning 인 toast 를 보여줄 수 있다', () => {
// given
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
vi.spyOn(store.toast, 'addToast').mockImplementation(() => vi.fn());

// when
Expand All @@ -88,10 +84,7 @@ describe('toast-plugin', () => {
// then
expect(store.toast.addToast).toHaveBeenCalledTimes(1);
expect(document.querySelectorAll('.vs-toast-view').length).toBe(1);
expect(consoleSpy).toHaveBeenCalledTimes(1);

// clear
consoleSpy.mockRestore();
expect(mockConsoleWarn).toHaveBeenCalledTimes(1);
});
});
});
9 changes: 8 additions & 1 deletion packages/vlossom/src/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { afterEach, beforeEach, vi } from 'vitest';
import { afterEach, beforeEach, vi, type MockInstance } from 'vitest';
import { config } from '@vue/test-utils';
import { VsStore } from '@/stores';
import * as exports from '@/stores';
import { createVlossom } from '@/index';

export let mockConsoleWarn: MockInstance | null = null;
export let mockConsoleError: MockInstance | null = null;

beforeEach(() => {
const fakeStore = new VsStore();
vi.spyOn(exports, 'store', 'get').mockReturnValue(fakeStore);
mockConsoleWarn = vi.spyOn(console, 'warn').mockImplementation(() => {});
mockConsoleError = vi.spyOn(console, 'error').mockImplementation(() => {});

const vlossom = createVlossom();
config.global.plugins = [vlossom];
});
afterEach(() => {
vi.restoreAllMocks();
vi.clearAllMocks();
mockConsoleWarn = null;
mockConsoleError = null;
});

const matchMediaMock = vi.fn((query) => ({
Expand Down

0 comments on commit e2e2553

Please sign in to comment.