Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smikhalevski committed Nov 25, 2024
1 parent e66bc3c commit dba8d9e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
111 changes: 111 additions & 0 deletions src/test/ExecutorSuspense.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import React, { useEffect } from 'react';
import {
ExecutorManager,
ExecutorManagerProvider,
ExecutorSuspense,
useExecutor,
useExecutorSubscription,
} from '../main';

describe('ExecutorSuspense', () => {
test('suspends component rendering until executors are settled', async () => {
const Component = () => {
const executor1 = useExecutor('xxx', () => 'aaa');
const executor2 = useExecutor('yyy', () => 'bbb');

return (
<ExecutorSuspense
fallback={'ccc'}
executors={[executor1, executor2]}
>
{executors => executors[0].get() + executors[1].get()}
</ExecutorSuspense>
);
};

const result = render(<Component />);

expect(result.getByText('ccc')).toBeInTheDocument();

expect(await result.findByText('aaabbb')).toBeInTheDocument();
});

test('does not suspend rendering if the pending executor is settled', async () => {
const manager = new ExecutorManager();
const capture = jest.fn();

const executor = manager.getOrCreate('xxx', 'aaa');

const Component = () => {
useExecutorSubscription(executor);

useEffect(() => {
executor.execute(async () => 'bbb');
}, []);

return (
<ExecutorSuspense executors={executor}>
{executor => {
capture(executor.value);
return executor.value;
}}
</ExecutorSuspense>
);
};

const result = render(
<ExecutorManagerProvider value={manager}>
<Component />
</ExecutorManagerProvider>
);

expect(await result.findByText('bbb')).toBeInTheDocument();

expect(capture).toHaveBeenCalledTimes(3);
expect(capture).toHaveBeenNthCalledWith(1, 'aaa');
expect(capture).toHaveBeenNthCalledWith(2, 'aaa');
expect(capture).toHaveBeenNthCalledWith(3, 'bbb');
});

test('respects a predicate', async () => {
const manager = new ExecutorManager();
const capture = jest.fn();
const predicateMock = jest.fn().mockReturnValue(true);

const executor = manager.getOrCreate('xxx', 'aaa');

const Component = () => {
useExecutorSubscription(executor);

useEffect(() => {
executor.execute(async () => 'bbb');
}, []);

return (
<ExecutorSuspense
executors={executor}
predicate={predicateMock}
>
{executor => {
capture(executor.value);
return executor.value;
}}
</ExecutorSuspense>
);
};

const result = render(
<ExecutorManagerProvider value={manager}>
<Component />
</ExecutorManagerProvider>
);

expect(await result.findByText('bbb')).toBeInTheDocument();

expect(capture).toHaveBeenCalledTimes(2);
expect(capture).toHaveBeenNthCalledWith(1, 'aaa');
expect(capture).toHaveBeenNthCalledWith(2, 'bbb');
});
});
6 changes: 3 additions & 3 deletions src/test/useExecutorSuspense.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ describe('useExecutorSuspense', () => {
expect(capture).toHaveBeenNthCalledWith(3, 'bbb');
});

test('respects a custom condition', async () => {
test('respects a predicate', async () => {
const manager = new ExecutorManager();
const capture = jest.fn();
const conditionMock = jest.fn().mockReturnValue(true);
const predicateMock = jest.fn().mockReturnValue(true);

const executor = manager.getOrCreate('xxx', 'aaa');

Expand All @@ -81,7 +81,7 @@ describe('useExecutorSuspense', () => {
executor.execute(async () => 'bbb');
}, []);

useExecutorSuspense(executor, conditionMock);
useExecutorSuspense(executor, predicateMock);

capture(executor.value);

Expand Down

0 comments on commit dba8d9e

Please sign in to comment.