improve tests

This commit is contained in:
Rene Haas
2022-11-09 12:51:22 +01:00
parent e292e0e539
commit 6bd4acdedd
4 changed files with 21 additions and 17 deletions
@@ -1,13 +1,15 @@
import { describe, test, expect, vitest } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { useState } from 'react'; import { useState } from 'react';
import { describe, test, afterEach, expect, vitest } from 'vitest';
import { render, screen, cleanup } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { OverlayScrollbars } from 'overlayscrollbars'; import { OverlayScrollbars } from 'overlayscrollbars';
import { OverlayScrollbarsComponent } from '~/overlayscrollbars-react'; import { OverlayScrollbarsComponent } from '~/overlayscrollbars-react';
import type { RefObject } from 'react'; import type { RefObject } from 'react';
import type { OverlayScrollbarsComponentRef } from '~/overlayscrollbars-react'; import type { OverlayScrollbarsComponentRef } from '~/overlayscrollbars-react';
describe('OverlayScrollbarsComponent', () => { describe('OverlayScrollbarsComponent', () => {
afterEach(() => cleanup());
describe('correct rendering', () => { describe('correct rendering', () => {
test('correct root element with instance', () => { test('correct root element with instance', () => {
const elementA = 'code'; const elementA = 'code';
@@ -1,11 +1,13 @@
import { useRef } from 'react'; import { useRef } from 'react';
import { describe, test, expect } from 'vitest'; import { describe, test, afterEach, expect } from 'vitest';
import { render, screen } from '@testing-library/react'; import { render, screen, cleanup } from '@testing-library/react';
import userEvent from '@testing-library/user-event'; import userEvent from '@testing-library/user-event';
import { useOverlayScrollbars } from '~/overlayscrollbars-react'; import { useOverlayScrollbars } from '~/overlayscrollbars-react';
import type { OverlayScrollbars } from 'overlayscrollbars'; import type { OverlayScrollbars } from 'overlayscrollbars';
describe('useOverlayScrollbars', () => { describe('useOverlayScrollbars', () => {
afterEach(() => cleanup());
test('re-initialization', () => { test('re-initialization', () => {
const Test = () => { const Test = () => {
const instanceRef = useRef<OverlayScrollbars | null>(null); const instanceRef = useRef<OverlayScrollbars | null>(null);
@@ -1,11 +1,13 @@
import { onMounted, ref, toRefs } from 'vue'; import { onMounted, ref, toRefs } from 'vue';
import { describe, test, expect, vitest } from 'vitest'; import { describe, test, afterEach, expect, vitest } from 'vitest';
import { OverlayScrollbars } from 'overlayscrollbars'; import { OverlayScrollbars } from 'overlayscrollbars';
import { fireEvent, render, screen } from '@testing-library/vue'; import { fireEvent, render, screen, cleanup } from '@testing-library/vue';
import userEvent from '@testing-library/user-event'; import userEvent from '@testing-library/user-event';
import { OverlayScrollbarsComponent } from '~/overlayscrollbars-vue'; import { OverlayScrollbarsComponent } from '~/overlayscrollbars-vue';
describe('OverlayScrollbarsComponent', () => { describe('OverlayScrollbarsComponent', () => {
afterEach(() => cleanup());
describe('correct rendering', () => { describe('correct rendering', () => {
test('correct root element with instance', async () => { test('correct root element with instance', async () => {
const elementA = 'code'; const elementA = 'code';
@@ -1,13 +1,15 @@
import { reactive, onMounted, ref, watch, toRaw, watchPostEffect } from 'vue'; import { reactive, onMounted, ref, watch, toRaw, watchPostEffect } from 'vue';
import { describe, test, expect, vitest } from 'vitest'; import { describe, test, afterEach, expect, vitest } from 'vitest';
import { render, screen } from '@testing-library/vue'; import { render, screen, cleanup } from '@testing-library/vue';
import userEvent from '@testing-library/user-event'; import userEvent from '@testing-library/user-event';
import { useOverlayScrollbars } from '~/overlayscrollbars-vue'; import { useOverlayScrollbars } from '~/overlayscrollbars-vue';
import type { PartialOptions, EventListeners, OverlayScrollbars } from 'overlayscrollbars'; import type { PartialOptions, EventListeners, OverlayScrollbars } from 'overlayscrollbars';
describe('useOverlayScrollbars', () => { describe('useOverlayScrollbars', () => {
afterEach(() => cleanup());
test('re-initialization', () => { test('re-initialization', () => {
const { unmount } = render({ render({
setup() { setup() {
const instanceRef = ref<OverlayScrollbars | null>(null); const instanceRef = ref<OverlayScrollbars | null>(null);
const [initialize, instance] = useOverlayScrollbars(); const [initialize, instance] = useOverlayScrollbars();
@@ -40,13 +42,12 @@ describe('useOverlayScrollbars', () => {
userEvent.click(initializeBtn); userEvent.click(initializeBtn);
expect(snapshot).toBe(initializeBtn.innerHTML); expect(snapshot).toBe(initializeBtn.innerHTML);
unmount();
}); });
test('reactive params', async () => { test('reactive params', async () => {
let osInstance: OverlayScrollbars; let osInstance: OverlayScrollbars;
const onUpdated = vitest.fn(); const onUpdated = vitest.fn();
const { unmount } = render({ render({
setup() { setup() {
const div = ref<HTMLElement | null>(null); const div = ref<HTMLElement | null>(null);
const params = reactive<{ options?: PartialOptions; events?: EventListeners }>({}); const params = reactive<{ options?: PartialOptions; events?: EventListeners }>({});
@@ -91,13 +92,12 @@ describe('useOverlayScrollbars', () => {
expect(onUpdated).toHaveBeenCalledTimes(1); expect(onUpdated).toHaveBeenCalledTimes(1);
expect(osInstance!.options().paddingAbsolute).toBe(true); expect(osInstance!.options().paddingAbsolute).toBe(true);
unmount();
}); });
test('ref params', async () => { test('ref params', async () => {
let osInstance: OverlayScrollbars; let osInstance: OverlayScrollbars;
const onUpdated = vitest.fn(); const onUpdated = vitest.fn();
const { unmount } = render({ render({
setup() { setup() {
const div = ref<HTMLElement | null>(null); const div = ref<HTMLElement | null>(null);
const params = ref<{ options?: PartialOptions; events?: EventListeners }>({}); const params = ref<{ options?: PartialOptions; events?: EventListeners }>({});
@@ -138,13 +138,12 @@ describe('useOverlayScrollbars', () => {
expect(onUpdated).toHaveBeenCalledTimes(1); expect(onUpdated).toHaveBeenCalledTimes(1);
expect(osInstance!.options().paddingAbsolute).toBe(true); expect(osInstance!.options().paddingAbsolute).toBe(true);
unmount();
}); });
test('ref params fields', async () => { test('ref params fields', async () => {
let osInstance: OverlayScrollbars; let osInstance: OverlayScrollbars;
const onUpdated = vitest.fn(); const onUpdated = vitest.fn();
const { unmount } = render({ render({
setup() { setup() {
const div = ref<HTMLElement | null>(null); const div = ref<HTMLElement | null>(null);
const options = ref<PartialOptions | undefined>(); const options = ref<PartialOptions | undefined>();
@@ -189,6 +188,5 @@ describe('useOverlayScrollbars', () => {
expect(onUpdated).toHaveBeenCalledTimes(1); expect(onUpdated).toHaveBeenCalledTimes(1);
expect(osInstance!.options().paddingAbsolute).toBe(true); expect(osInstance!.options().paddingAbsolute).toBe(true);
unmount();
}); });
}); });