Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce function #106

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ node_modules
.pnp.*

# testing
/coverage
coverage
.out/

# Build directories (next.js...)
Expand Down
11 changes: 11 additions & 0 deletions jest.config.base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
preset: 'ts-jest',
roots: ['<rootDir>/'],
testEnvironment: 'node',
collectCoverage: true,
coveragePathIgnorePatterns: [
'<rootDir>/node_modules/',
],
coverageDirectory: '<rootDir>/coverage/',
verbose: true
};
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const base = require('./jest.config.base');

module.exports = {
...base,
roots: ['<rootDir>'],
projects: [
'<rootDir>/packages/ui',
'<rootDir>/packages/api',
'<rootDir>/packages/diceroll'
],
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
"main": "index.js",
"private": true,
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/prettier": "^2.4.4",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.5.1",
"lerna": "^4.0.0",
"prettier": "^2.5.1",
"stylus-loader": "^6.2.0",
"ts-jest": "^27.1.3",
"typescript": "^4.6.2"
},
"scripts": {
Expand Down
28 changes: 28 additions & 0 deletions packages/search-engine/__tests__/debunce.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { debounce } from "../src/scripts/debounce";

jest.useFakeTimers();
describe('Given the debounce function is used', () => {
test('when is called sometimes in a short time period, then should just executed once', () => {
const func = jest.fn();
const debouncedFunc = debounce(func, 500);

// Execute for the first time
debouncedFunc();

// Move on the timer
jest.advanceTimersByTime(250);
// try to execute a 2nd time
debouncedFunc();

// Fast-forward time
jest.runAllTimers();

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

test('when we do not pass a function, then should not execute', () => {
const debouncedFunc = debounce(undefined, 500);

expect(debouncedFunc).toEqual(undefined);
});
});
6 changes: 4 additions & 2 deletions packages/search-engine/__tests__/search-engine.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

const searchEngine = require('..');
// const searchEngine = require('..');
Cristhian-Medina marked this conversation as resolved.
Show resolved Hide resolved

describe('@booking-ui/search-engine', () => {
it('needs tests');
it('needs tests', () => {

});
});
9 changes: 9 additions & 0 deletions packages/search-engine/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const base = require('../../jest.config.base');
const packageJson = require('./package');

module.exports = {
...base,
testEnvironment: 'jsdom', // This is overriden, from the base testEnvironment
name: packageJson.name,
displayName: packageJson.name,
};
3 changes: 2 additions & 1 deletion packages/search-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"url": "git+https://github.com/Platzi-Master-C9/booking-ui.git"
},
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
"test": "yarn jest",
"test:watch": "yarn jest --watch"
},
"bugs": {
"url": "https://github.com/Platzi-Master-C9/booking-ui/issues"
Expand Down
16 changes: 16 additions & 0 deletions packages/search-engine/src/scripts/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function debounce (func: () => void, wait: number) {
let timeout;

if(!func)
return

return function () {
if(timeout){
clearTimeout(timeout);
}

timeout = setTimeout(() => {
func.apply(this, arguments);
}, wait);
};
}
Loading