Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smikhalevski committed Apr 26, 2024
1 parent 8660f23 commit 2db9dc3
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/plugin/disposeDeactivated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ExecutorPlugin } from '../types';
*
* @param ms The timeout in milliseconds after which the executor is disposed.
*/
export default function disposeDeactivated(ms = 0): ExecutorPlugin {
export default function disposeDeactivated(ms = 5_000): ExecutorPlugin {
return executor => {
let timer: NodeJS.Timeout;

Expand Down
3 changes: 2 additions & 1 deletion src/main/plugin/invalidateAfter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export default function invalidateAfter(ms: number): ExecutorPlugin {
case 'activated':
case 'fulfilled':
case 'rejected':
case 'aborted':
clearTimeout(timer);

if (executor.isActive && executor.isSettled) {
if (!executor.isPending && executor.isActive && executor.isSettled) {
timer = setTimeout(
() => {
executor.invalidate();
Expand Down
2 changes: 1 addition & 1 deletion src/main/plugin/repeatFulfilled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function repeatFulfilled<Value = any>(
case 'fulfilled':
clearTimeout(timer);

if (executor.isActive && executor.isFulfilled && index < count) {
if (!executor.isPending && executor.isActive && executor.isFulfilled && index < count) {
timer = setTimeout(
() => {
index++;
Expand Down
2 changes: 1 addition & 1 deletion src/main/plugin/retryRejected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function retryRejected<Value = any>(
case 'rejected':
clearTimeout(timer);

if (executor.isActive && executor.isRejected && index < count) {
if (!executor.isPending && executor.isActive && executor.isRejected && index < count) {
timer = setTimeout(
() => {
index++;
Expand Down
4 changes: 2 additions & 2 deletions src/test/plugin/disposeDeactivated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('disposeDeactivated', () => {
});

test('disposes a deactivated executor', async () => {
const executor = manager.getOrCreate('xxx', undefined, [disposeDeactivated()]);
const executor = manager.getOrCreate('xxx', undefined, [disposeDeactivated(0)]);
const deactivate = executor.activate();
const taskMock = jest.fn(_signal => delay(100, 'aaa'));
const promise = executor.execute(taskMock);
Expand All @@ -32,7 +32,7 @@ describe('disposeDeactivated', () => {
});

test('does not dispose if the executor was reactivated', async () => {
const executor = manager.getOrCreate('xxx', undefined, [disposeDeactivated()]);
const executor = manager.getOrCreate('xxx', undefined, [disposeDeactivated(0)]);
const deactivate = executor.activate();
const taskMock = jest.fn(_signal => delay(100, 'aaa'));
const promise = executor.execute(taskMock);
Expand Down
14 changes: 9 additions & 5 deletions src/test/plugin/invalidateAfter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@ describe('invalidateAfter', () => {

test('invalidates an executor after the timeout', async () => {
const executor = manager.getOrCreate('xxx', undefined, [invalidateAfter(100)]);
executor.activate();
executor.resolve('aaa');

jest.runAllTimers();

expect(executor.isStale).toBe(true);

expect(listenerMock).toHaveBeenCalledTimes(3);
expect(listenerMock).toHaveBeenCalledTimes(4);
expect(listenerMock).toHaveBeenNthCalledWith(1, { type: 'configured', target: executor });
expect(listenerMock).toHaveBeenNthCalledWith(2, { type: 'fulfilled', target: executor });
expect(listenerMock).toHaveBeenNthCalledWith(3, { type: 'invalidated', target: executor });
expect(listenerMock).toHaveBeenNthCalledWith(2, { type: 'activated', target: executor });
expect(listenerMock).toHaveBeenNthCalledWith(3, { type: 'fulfilled', target: executor });
expect(listenerMock).toHaveBeenNthCalledWith(4, { type: 'invalidated', target: executor });
});

test('delays invalidation every time an executor is fulfilled', async () => {
const executor = manager.getOrCreate('xxx', undefined, [invalidateAfter(100)]);
executor.activate();
executor.resolve('aaa');

jest.advanceTimersByTime(99);
Expand All @@ -38,9 +41,10 @@ describe('invalidateAfter', () => {

jest.advanceTimersByTime(99);

expect(listenerMock).toHaveBeenCalledTimes(3);
expect(listenerMock).toHaveBeenCalledTimes(4);
expect(listenerMock).toHaveBeenNthCalledWith(1, { type: 'configured', target: executor });
expect(listenerMock).toHaveBeenNthCalledWith(2, { type: 'fulfilled', target: executor });
expect(listenerMock).toHaveBeenNthCalledWith(2, { type: 'activated', target: executor });
expect(listenerMock).toHaveBeenNthCalledWith(3, { type: 'fulfilled', target: executor });
expect(listenerMock).toHaveBeenNthCalledWith(4, { type: 'fulfilled', target: executor });
});
});

0 comments on commit 2db9dc3

Please sign in to comment.