Skip to content

Commit

Permalink
test: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed May 31, 2023
1 parent 30c4de1 commit 098172b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/__tests__/component.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import React from 'react';
import { act, render } from '@testing-library/react';
import '@testing-library/jest-dom';
import { makeReactive, reactive, ref, useComputed, useWatchEffect } from '..';
import {
makeReactive,
reactive,
ref,
useComputed,
useWatch,
useWatchEffect,
} from '..';
import { perf, wait } from 'react-performance-testing';
import 'jest-performance-testing';

Expand Down Expand Up @@ -150,12 +157,22 @@ describe('makeReactive', () => {

const mockEffect = jest.fn();
const mockCleanup = jest.fn();
const mockEffect2 = jest.fn();
const mockCleanup2 = jest.fn();
const mockGetter = jest.fn(() => count.value + 1);
const Tester = makeReactive(function Tester() {
useWatchEffect(() => {
mockEffect(count.value);
return mockCleanup;
});
useWatch(
count,
(...args) => {
mockEffect2(...args);
return mockCleanup2;
},
{ immediate: true }
);
const derived = useComputed(mockGetter);
return <p>{derived.value}</p>;
});
Expand All @@ -164,6 +181,8 @@ describe('makeReactive', () => {

expect(mockEffect).toBeCalledTimes(1);
expect(mockCleanup).toBeCalledTimes(0);
expect(mockEffect2).toBeCalledTimes(1);
expect(mockCleanup2).toBeCalledTimes(0);
expect(mockGetter).toBeCalledTimes(1);
const content1 = await findByText('1');
expect(content1).toBeTruthy();
Expand All @@ -174,6 +193,8 @@ describe('makeReactive', () => {

expect(mockEffect).toBeCalledTimes(2);
expect(mockCleanup).toBeCalledTimes(1);
expect(mockEffect2).toBeCalledTimes(2);
expect(mockCleanup2).toBeCalledTimes(1);
expect(mockGetter).toBeCalledTimes(2);
const content2 = await findByText('2');
expect(content2).toBeTruthy();
Expand All @@ -182,6 +203,8 @@ describe('makeReactive', () => {

expect(mockEffect).toBeCalledTimes(2);
expect(mockCleanup).toBeCalledTimes(2);
expect(mockEffect2).toBeCalledTimes(2);
expect(mockCleanup2).toBeCalledTimes(2);
expect(mockGetter).toBeCalledTimes(2);

act(() => {
Expand All @@ -190,19 +213,31 @@ describe('makeReactive', () => {

expect(mockEffect).toBeCalledTimes(2);
expect(mockCleanup).toBeCalledTimes(2);
expect(mockEffect2).toBeCalledTimes(2);
expect(mockCleanup2).toBeCalledTimes(2);
expect(mockGetter).toBeCalledTimes(2);
});
it('stops reactive effects on unmount (Strict Mode)', async () => {
const count = ref(0);

const mockEffect = jest.fn();
const mockCleanup = jest.fn();
const mockEffect2 = jest.fn();
const mockCleanup2 = jest.fn();
const mockGetter = jest.fn(() => count.value + 1);
const Tester = makeReactive(function Tester() {
useWatchEffect(() => {
mockEffect(count.value);
return mockCleanup;
});
useWatch(
count,
(...args) => {
mockEffect2(...args);
return mockCleanup2;
},
{ immediate: true }
);
const derived = useComputed(mockGetter);
return <p>{derived.value}</p>;
});
Expand All @@ -215,6 +250,8 @@ describe('makeReactive', () => {

expect(mockEffect).toBeCalledTimes(3);
expect(mockCleanup).toBeCalledTimes(2);
expect(mockEffect2).toBeCalledTimes(3);
expect(mockCleanup2).toBeCalledTimes(2);
expect(mockGetter).toBeCalledTimes(3);
const content1 = await findByText('1');
expect(content1).toBeTruthy();
Expand All @@ -225,6 +262,8 @@ describe('makeReactive', () => {

expect(mockEffect).toBeCalledTimes(4);
expect(mockCleanup).toBeCalledTimes(3);
expect(mockEffect2).toBeCalledTimes(4);
expect(mockCleanup2).toBeCalledTimes(3);
expect(mockGetter).toBeCalledTimes(4);
const content2 = await findByText('2');
expect(content2).toBeTruthy();
Expand All @@ -233,6 +272,8 @@ describe('makeReactive', () => {

expect(mockEffect).toBeCalledTimes(4);
expect(mockCleanup).toBeCalledTimes(4);
expect(mockEffect2).toBeCalledTimes(4);
expect(mockCleanup2).toBeCalledTimes(4);
expect(mockGetter).toBeCalledTimes(4);

act(() => {
Expand All @@ -241,6 +282,8 @@ describe('makeReactive', () => {

expect(mockEffect).toBeCalledTimes(4);
expect(mockCleanup).toBeCalledTimes(4);
expect(mockEffect2).toBeCalledTimes(4);
expect(mockCleanup2).toBeCalledTimes(4);
expect(mockGetter).toBeCalledTimes(4);
});
});
33 changes: 33 additions & 0 deletions src/__tests__/core.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,39 @@ describe('watch', () => {

expect(consoleWarn).toBeCalled();
});
it('traverses complex object', () => {
const obj = reactive({
a: 1,
nested: { b: 2 },
nested2: { b: 2 },
r: ref(2),
values: [{ a: 1 }, { a: 2 }, ref(5)],
set: new Set([{ a: 1 }, { a: 2 }]),
map: new Map([
['a', 1],
['b', 2],
]),
});
obj.nested2 = obj.nested;

const effectFn = jest.fn();

const runner = watch(obj, (...args) => {
effectFn(...args);
});

expect(effectFn).toBeCalledTimes(0);

obj.nested.b++;

expect(effectFn).toBeCalledTimes(1);
expect(effectFn).toBeCalledWith(obj, obj);

runner();
obj.nested.b++;

expect(effectFn).toBeCalledTimes(1);
});
});

describe('useWatch', () => {
Expand Down

0 comments on commit 098172b

Please sign in to comment.