Skip to content

Commit

Permalink
feat(lib): debounce support multi instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Netanel Basal committed Mar 14, 2019
1 parent f203809 commit 2423fe2
Show file tree
Hide file tree
Showing 18 changed files with 6,616 additions and 17,673 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
node_modules
.idea
dist
dist
pkg
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ cache:
directories:
- node_modules
notifications:
email: false
email: true
node_js:
- '10'
before_script:
- npm prune
script:
- npm run test
after_success:
- npm run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
49 changes: 13 additions & 36 deletions __tests__/decorators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { timeout, debounce, throttle, once } from '../src';
import { debounce, throttle, once } from '../src';
jest.mock('lodash.debounce');
jest.mock('lodash.throttle');
import * as throttleFn from 'lodash.throttle';
Expand All @@ -7,34 +7,11 @@ import * as debounceFn from 'lodash.debounce';
jest.useFakeTimers();

describe('Decorators', () => {
describe('timeout', function () {
class Test {
@timeout(1000)
method() {
console.log('Worked');
}
}

it('should call setTimeout', function () {
new Test().method();
expect(setTimeout['mock'].calls.length).toBe(1);
expect(setTimeout['mock'].calls[0][1]).toBe(1000);
});

it('should invoke the console.log', function () {
const spy = jest.spyOn(console, 'log');
new Test().method();
jest.runAllTimers();
expect(spy).toBeCalled();
expect(spy).toHaveBeenCalledWith('Worked');
});
});

describe('debounce', function () {
const func = function () {
describe('debounce', function() {
const func = function() {
return 'called';
};
debounceFn['mockImplementation'](function () {
debounceFn['mockImplementation'](function() {
return func;
});
class TestDebounce {
Expand All @@ -43,19 +20,19 @@ describe('Decorators', () => {
console.log('Debounce Worked!');
}
}
it('should call debounce', function () {
it('should call debounce', function() {
new TestDebounce().method();
expect(debounceFn).toBeCalled();
expect(debounceFn['mock'].calls[0][1]).toEqual(3000);
expect(debounceFn['mock'].calls[0][2]).toEqual({});
});
});

describe('throttle', function () {
const func = function () {
describe('throttle', function() {
const func = function() {
return 'called';
};
throttleFn['mockImplementation'](function () {
throttleFn['mockImplementation'](function() {
return func;
});
class TestThrottle {
Expand All @@ -64,22 +41,22 @@ describe('Decorators', () => {
console.log('Throttle Worked!');
}
}
it('should call throttle', function () {
it('should call throttle', function() {
new TestThrottle().method();
expect(throttleFn).toBeCalled();
expect(throttleFn['mock'].calls[0][1]).toEqual(3000);
expect(throttleFn['mock'].calls[0][2]).toEqual({});
});
});

describe('once', function () {
describe('once', function() {
class TestOnce {
@once
method() {
console.warn('Once Worked!');
}
}
it('should call the method only once', function () {
it('should call the method only once', function() {
const instance = new TestOnce();
const consoleSpy = jest.spyOn(console, 'warn');
instance.method();
Expand All @@ -88,5 +65,5 @@ describe('Decorators', () => {
expect(consoleSpy).toBeCalled();
expect(consoleSpy).toHaveBeenCalledTimes(1);
});
})
});
});
});
58 changes: 29 additions & 29 deletions __tests__/multi-instance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import {debounce} from "../src";
import { debounce } from '../src';

jest.mock('lodash.debounce', () => {
return (func, wait) => {
let timeout;
return function (this: any, ...args) {
const context = this;
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(context, args), wait);
}
return (func, wait) => {
let timeout;
return function(this: any, ...args) {
const context = this;
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(context, args), wait);
};
};
});

jest.useFakeTimers();

class Test {
id;
id;

constructor(id) {
this.id = id;
}
constructor(id) {
this.id = id;
}

@debounce(3000)
method() {
this.id++;
}
@debounce(3000)
method() {
this.id++;
}
}

describe('Multi instance support', () => {
describe('Debounce', () => {
it('should work with multi instance', () => {
const classA = new Test(1);
const classB = new Test(100);
classA.method();
jest.advanceTimersByTime(1000);
classB.method();
jest.advanceTimersByTime(2001);
expect(classA.id).toBe(2);
jest.advanceTimersByTime(1000);
expect(classB.id).toBe(101);
});
describe('Debounce', () => {
it('should work with multi instance', () => {
const classA = new Test(1);
const classB = new Test(100);
classA.method();
jest.advanceTimersByTime(1000);
classB.method();
jest.advanceTimersByTime(2001);
expect(classA.id).toBe(2);
jest.advanceTimersByTime(1000);
expect(classB.id).toBe(101);
});
});
});
});
Loading

0 comments on commit 2423fe2

Please sign in to comment.