From 0f9bc1fb8a20dee714cd9da2e789ed5b14fe30ec Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Sat, 19 Sep 2020 08:04:43 +0700 Subject: [PATCH] updated jest __tests__ --- __test__/index.ts | 3 --- dist/__test__/index.d.ts | 1 + dist/__test__/index.js | 4 +++ dist/__tests__/index.d.ts | 1 + dist/__tests__/index.js | 7 +++++ dist/index.d.ts | 1 + dist/index.js | 8 ++++++ dist/src/index.d.ts | 1 + dist/src/index.js | 8 ++++++ dist/src/useCountdown.d.ts | 12 +++++++++ dist/src/useCountdown.js | 54 ++++++++++++++++++++++++++++++++++++++ dist/useCountdown.d.ts | 12 +++++++++ dist/useCountdown.js | 54 ++++++++++++++++++++++++++++++++++++++ src/__tests__/index.ts | 7 +++++ tsconfig.json | 2 +- 15 files changed, 171 insertions(+), 4 deletions(-) delete mode 100644 __test__/index.ts create mode 100644 dist/__test__/index.d.ts create mode 100644 dist/__test__/index.js create mode 100644 dist/__tests__/index.d.ts create mode 100644 dist/__tests__/index.js create mode 100644 dist/index.d.ts create mode 100644 dist/index.js create mode 100644 dist/src/index.d.ts create mode 100644 dist/src/index.js create mode 100644 dist/src/useCountdown.d.ts create mode 100644 dist/src/useCountdown.js create mode 100644 dist/useCountdown.d.ts create mode 100644 dist/useCountdown.js create mode 100644 src/__tests__/index.ts diff --git a/__test__/index.ts b/__test__/index.ts deleted file mode 100644 index dd949e7..0000000 --- a/__test__/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -function sumTest () { - console.log(1+2) -} \ No newline at end of file diff --git a/dist/__test__/index.d.ts b/dist/__test__/index.d.ts new file mode 100644 index 0000000..d8dae6f --- /dev/null +++ b/dist/__test__/index.d.ts @@ -0,0 +1 @@ +declare function sumTest(): void; diff --git a/dist/__test__/index.js b/dist/__test__/index.js new file mode 100644 index 0000000..2156781 --- /dev/null +++ b/dist/__test__/index.js @@ -0,0 +1,4 @@ +"use strict"; +function sumTest() { + console.log(1 + 2); +} diff --git a/dist/__tests__/index.d.ts b/dist/__tests__/index.d.ts new file mode 100644 index 0000000..cdddcc5 --- /dev/null +++ b/dist/__tests__/index.d.ts @@ -0,0 +1 @@ +declare function sumTest(): number; diff --git a/dist/__tests__/index.js b/dist/__tests__/index.js new file mode 100644 index 0000000..8669b46 --- /dev/null +++ b/dist/__tests__/index.js @@ -0,0 +1,7 @@ +"use strict"; +function sumTest() { + return 1 + 4; +} +test("test sum with 1 + 4 -> 5", function () { + expect(1 + 4).toBe(5); +}); diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..0174f99 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1 @@ +export { default as useCountdown } from './useCountdown'; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..03a1634 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,8 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.useCountdown = void 0; +var useCountdown_1 = require("./useCountdown"); +Object.defineProperty(exports, "useCountdown", { enumerable: true, get: function () { return __importDefault(useCountdown_1).default; } }); diff --git a/dist/src/index.d.ts b/dist/src/index.d.ts new file mode 100644 index 0000000..0174f99 --- /dev/null +++ b/dist/src/index.d.ts @@ -0,0 +1 @@ +export { default as useCountdown } from './useCountdown'; diff --git a/dist/src/index.js b/dist/src/index.js new file mode 100644 index 0000000..03a1634 --- /dev/null +++ b/dist/src/index.js @@ -0,0 +1,8 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.useCountdown = void 0; +var useCountdown_1 = require("./useCountdown"); +Object.defineProperty(exports, "useCountdown", { enumerable: true, get: function () { return __importDefault(useCountdown_1).default; } }); diff --git a/dist/src/useCountdown.d.ts b/dist/src/useCountdown.d.ts new file mode 100644 index 0000000..d1c80e9 --- /dev/null +++ b/dist/src/useCountdown.d.ts @@ -0,0 +1,12 @@ +interface useCountdownOptions { + now?: () => Date; + onEnd?: () => void; + onCount?: (timeLeft: number) => void; + step?: number; +} +interface CountdownHookResult { + timeleft: number; + start: (timeLeft: number) => void; +} +declare function useCountdown(options?: useCountdownOptions): CountdownHookResult; +export default useCountdown; diff --git a/dist/src/useCountdown.js b/dist/src/useCountdown.js new file mode 100644 index 0000000..e1c8f47 --- /dev/null +++ b/dist/src/useCountdown.js @@ -0,0 +1,54 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var react_1 = __importDefault(require("react")); +var DEFAULT_OPTIONS = { + step: 1000, +}; +function useCountdown(options) { + var _a = react_1.default.useState(), timeleft = _a[0], setTimeleft = _a[1]; + var _b = react_1.default.useState(), targetDatetime = _b[0], setTargetDatetime = _b[1]; + var _c = __assign(__assign({}, DEFAULT_OPTIONS), options), step = _c.step, onCount = _c.onCount, onEnd = _c.onEnd; + var start = function (countAsM) { + setTimeleft(countAsM); + }; + var varTimeout = null; + react_1.default.useEffect(function () { + return function () { return clearTimeout(varTimeout); }; + }, []); + react_1.default.useEffect(function () { + if (timeleft === undefined) { + return; + } + var newTimeleft = timeleft - step; + if (newTimeleft < 0) { + setTimeleft(0); + onEnd === null || onEnd === void 0 ? void 0 : onEnd(); + } + else { + onCount === null || onCount === void 0 ? void 0 : onCount(timeleft); + varTimeout = setTimeout(function () { + setTimeleft(newTimeleft); + }, step); + } + }, [timeleft]); + var result = { + timeleft: timeleft || 0, + start: start, + }; + return result; +} +exports.default = useCountdown; diff --git a/dist/useCountdown.d.ts b/dist/useCountdown.d.ts new file mode 100644 index 0000000..d1c80e9 --- /dev/null +++ b/dist/useCountdown.d.ts @@ -0,0 +1,12 @@ +interface useCountdownOptions { + now?: () => Date; + onEnd?: () => void; + onCount?: (timeLeft: number) => void; + step?: number; +} +interface CountdownHookResult { + timeleft: number; + start: (timeLeft: number) => void; +} +declare function useCountdown(options?: useCountdownOptions): CountdownHookResult; +export default useCountdown; diff --git a/dist/useCountdown.js b/dist/useCountdown.js new file mode 100644 index 0000000..e1c8f47 --- /dev/null +++ b/dist/useCountdown.js @@ -0,0 +1,54 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var react_1 = __importDefault(require("react")); +var DEFAULT_OPTIONS = { + step: 1000, +}; +function useCountdown(options) { + var _a = react_1.default.useState(), timeleft = _a[0], setTimeleft = _a[1]; + var _b = react_1.default.useState(), targetDatetime = _b[0], setTargetDatetime = _b[1]; + var _c = __assign(__assign({}, DEFAULT_OPTIONS), options), step = _c.step, onCount = _c.onCount, onEnd = _c.onEnd; + var start = function (countAsM) { + setTimeleft(countAsM); + }; + var varTimeout = null; + react_1.default.useEffect(function () { + return function () { return clearTimeout(varTimeout); }; + }, []); + react_1.default.useEffect(function () { + if (timeleft === undefined) { + return; + } + var newTimeleft = timeleft - step; + if (newTimeleft < 0) { + setTimeleft(0); + onEnd === null || onEnd === void 0 ? void 0 : onEnd(); + } + else { + onCount === null || onCount === void 0 ? void 0 : onCount(timeleft); + varTimeout = setTimeout(function () { + setTimeleft(newTimeleft); + }, step); + } + }, [timeleft]); + var result = { + timeleft: timeleft || 0, + start: start, + }; + return result; +} +exports.default = useCountdown; diff --git a/src/__tests__/index.ts b/src/__tests__/index.ts new file mode 100644 index 0000000..7d68344 --- /dev/null +++ b/src/__tests__/index.ts @@ -0,0 +1,7 @@ +function sumTest() { + return 1 + 4; +} + +test(`test sum with 1 + 4 -> 5`, () => { + expect(1 + 4).toBe(5); +}); diff --git a/tsconfig.json b/tsconfig.json index 09f142e..69a674f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -64,7 +64,7 @@ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ }, "include": [ - "src", "__test__" + "src", "src/__tests__" ], "exclude": ["node_modules", "**/*.spec.ts"]