Skip to content

Commit

Permalink
migrating cancelPreviousify
Browse files Browse the repository at this point in the history
  • Loading branch information
vlio20 committed Nov 2, 2024
1 parent c07b501 commit 2f8f5fb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"scripts": {
"test:full": "npm run clean && tsc -p tsconfig.test.json --noEmit && npm run lint && node --test --experimental-test-coverage && npm run test:mutation",
"test:unit": "npm run clean && tsc --noEmit && npm run lint && node --test",
"test": "tsx --test $(find src -type f -name 'before.spec.ts')",
"test": "tsx --test $(find src -type f -name 'cancel-previous.spec.ts')",
"test:mutation": "stryker run",
"lint": "eslint ./src --ext .ts --quiet",
"lint:fix": "eslint ./src --ext .ts --fix",
Expand Down
44 changes: 20 additions & 24 deletions src/cancel-previous/cancel-previous.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { cancelPrevious } from './cancel-previous';
import { CanceledPromise } from './canceled-promise';
import { describe, it } from 'node:test';
import assert from 'node:assert';

describe('cancelPrevious', () => {
it('should make sure error thrown when decorator not set on method', () => {
try {
assert.throws(() => {
const nonValidCancelPrevious: any = cancelPrevious<T>();

class T {
@nonValidCancelPrevious
boo: string;
@nonValidCancelPrevious boo: string;
}
} catch (e) {
expect('@cancelPrevious is applicable only on a methods.').toBe(e.message);

return;
}

throw new Error('should not reach this line');
}, '@cancelPrevious is applicable only on a methods.');
});

it('should cancel prev invocation', (done) => {
it('should cancel prev invocation', (ctx, done) => {
class T {
@cancelPrevious<T>()
foo(x: number): Promise<number> {
Expand All @@ -37,17 +32,18 @@ describe('cancelPrevious', () => {
const func = (x: number) => {
t.foo(x)
.then((data) => {
expect(data).toBe(100);
expect(cancelHappened).toBe(1);
assert.equal(data, 100);
assert.equal(cancelHappened, 1);
done();
})
.catch((e) => {
if (e instanceof CanceledPromise) {
expect(e.message).toBe('canceled');
assert.equal(e.message, 'canceled');
cancelHappened += 1;

return;
}

throw new Error('should\'t get here');
});
};
Expand All @@ -59,7 +55,7 @@ describe('cancelPrevious', () => {
}, 5);
});

it('should invoke original method id was resolved before second call', (done) => {
it('should invoke original method if it was resolved before second call', (ctx, done) => {
class T {
@cancelPrevious()
foo(x: number): Promise<number> {
Expand All @@ -80,19 +76,18 @@ describe('cancelPrevious', () => {
.then((data) => {
if (round === 1) {
round += 1;
expect(data).toBe(10);
assert.equal(data, 10);
} else {
expect(data).toBe(100);
assert.equal(data, 100);
}

expect(cancelHappened).toBe(0);
done();
assert.equal(cancelHappened, 0);
})
.catch((e) => {
if (e instanceof CanceledPromise) {
cancelHappened += 1;
} else {
throw new Error('should\'t get here');
throw new Error('shouldn\'t get here');
}
});
};
Expand All @@ -101,10 +96,11 @@ describe('cancelPrevious', () => {

setTimeout(() => {
func(100);
done();
}, 15);
});

it('should invoke rejection if original method got an error', (done) => {
it('should invoke rejection if original method got an error', (ctx, done) => {
class T {
@cancelPrevious<T>()
foo(x: number): Promise<number> {
Expand All @@ -121,13 +117,13 @@ describe('cancelPrevious', () => {
const func = (x: number) => {
t.foo(x)
.then(() => {
throw new Error('should\'t get here');
throw new Error('shouldn\'t get here');
})
.catch((e) => {
if (e instanceof CanceledPromise) {
throw new Error('should\'t get here');
throw new Error('shouldn\'t get here');
} else {
expect(e.message).toBe('server error');
assert.equal(e.message, 'server error');
done();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/cancel-previous/cancel-previousify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function cancelPreviousify<D = any, A extends any[] = any[]>(originalMeth

originalMethod.apply(this, args)
.then((data: any) => resolve(data))
.catch((err) => reject(err));
.catch((err: Error) => reject(err));
});
};
}

0 comments on commit 2f8f5fb

Please sign in to comment.