@k8o/arte-odyssey 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/components/accordion/accordion-button.d.ts +2 -0
- package/dist/components/accordion/accordion-button.js +40 -0
- package/dist/components/accordion/accordion-item.d.ts +4 -0
- package/dist/components/accordion/accordion-item.js +11 -0
- package/dist/components/accordion/accordion-panel.d.ts +2 -0
- package/dist/components/accordion/accordion-panel.js +21 -0
- package/dist/components/accordion/accordion.d.ts +2 -0
- package/dist/components/accordion/accordion.js +17 -0
- package/dist/components/accordion/accordion.stories.js +89 -0
- package/dist/components/accordion/context.d.ts +10 -0
- package/dist/components/accordion/context.js +38 -0
- package/dist/components/accordion/index.d.ts +4 -0
- package/dist/components/accordion/index.js +4 -0
- package/dist/components/alert/alert.d.ts +8 -0
- package/dist/components/alert/alert.js +46 -0
- package/dist/components/alert/alert.stories.js +51 -0
- package/dist/components/alert/index.d.ts +1 -0
- package/dist/components/alert/index.js +1 -0
- package/dist/components/anchor/anchor.d.ts +14 -0
- package/dist/components/anchor/anchor.js +31 -0
- package/dist/components/anchor/anchor.stories.js +23 -0
- package/dist/components/anchor/index.d.ts +1 -0
- package/dist/components/anchor/index.js +1 -0
- package/dist/components/baseline-status/baseline-status.d.ts +16 -0
- package/dist/components/baseline-status/baseline-status.js +39 -0
- package/dist/components/baseline-status/baseline-status.stories.js +15 -0
- package/dist/components/baseline-status/index.d.ts +1 -0
- package/dist/components/baseline-status/index.js +1 -0
- package/dist/components/breadcrumb/breadcrumb.d.ts +18 -0
- package/dist/components/breadcrumb/breadcrumb.js +36 -0
- package/dist/components/breadcrumb/breadcrumb.stories.js +40 -0
- package/dist/components/breadcrumb/index.d.ts +1 -0
- package/dist/components/breadcrumb/index.js +1 -0
- package/dist/components/button/button.d.ts +10 -0
- package/dist/components/button/button.js +55 -0
- package/dist/components/button/button.stories.js +96 -0
- package/dist/components/button/index.d.ts +1 -0
- package/dist/components/button/index.js +1 -0
- package/dist/components/card/card.d.ts +3 -0
- package/dist/components/card/card.js +26 -0
- package/dist/components/card/card.stories.js +38 -0
- package/dist/components/card/index.d.ts +2 -0
- package/dist/components/card/index.js +2 -0
- package/dist/components/card/interactive-card.d.ts +3 -0
- package/dist/components/card/interactive-card.js +34 -0
- package/dist/components/card/type.d.ts +6 -0
- package/dist/components/card/type.js +0 -0
- package/dist/components/code/code.d.ts +4 -0
- package/dist/components/code/code.js +38 -0
- package/dist/components/code/code.stories.js +60 -0
- package/dist/components/code/index.d.ts +1 -0
- package/dist/components/code/index.js +4 -0
- package/dist/components/dialog/dialog.d.ts +17 -0
- package/dist/components/dialog/dialog.js +85 -0
- package/dist/components/dialog/dialog.stories.js +98 -0
- package/dist/components/dialog/index.d.ts +1 -0
- package/dist/components/dialog/index.js +1 -0
- package/dist/components/drawer/drawer.d.ts +6 -0
- package/dist/components/drawer/drawer.js +56 -0
- package/dist/components/drawer/drawer.stories.js +25 -0
- package/dist/components/drawer/index.d.ts +1 -0
- package/dist/components/drawer/index.js +1 -0
- package/dist/components/dropdown-menu/dropdown-menu.d.ts +24 -0
- package/dist/components/dropdown-menu/dropdown-menu.js +130 -0
- package/dist/components/dropdown-menu/dropdown-menu.stories.js +111 -0
- package/dist/components/dropdown-menu/hooks.d.ts +23 -0
- package/dist/components/dropdown-menu/hooks.js +56 -0
- package/dist/components/dropdown-menu/index.d.ts +1 -0
- package/dist/components/dropdown-menu/index.js +1 -0
- package/dist/components/error-boundary/index.d.ts +1 -0
- package/dist/components/error-boundary/index.js +4 -0
- package/dist/components/form/autocomplete/autocomplete.d.ts +14 -0
- package/dist/components/form/autocomplete/autocomplete.js +225 -0
- package/dist/components/form/autocomplete/autocomplete.stories.js +59 -0
- package/dist/components/form/autocomplete/index.d.ts +1 -0
- package/dist/components/form/autocomplete/index.js +1 -0
- package/dist/components/form/checkbox/checkbox.d.ts +8 -0
- package/dist/components/form/checkbox/checkbox.js +40 -0
- package/dist/components/form/checkbox/checkbox.stories.js +27 -0
- package/dist/components/form/checkbox/index.d.ts +1 -0
- package/dist/components/form/checkbox/index.js +1 -0
- package/dist/components/form/form-control/form-control.d.ts +20 -0
- package/dist/components/form/form-control/form-control.js +53 -0
- package/dist/components/form/form-control/form-control.stories.js +94 -0
- package/dist/components/form/form-control/index.d.ts +1 -0
- package/dist/components/form/form-control/index.js +1 -0
- package/dist/components/form/number-field/index.d.ts +1 -0
- package/dist/components/form/number-field/index.js +1 -0
- package/dist/components/form/number-field/number-field.d.ts +17 -0
- package/dist/components/form/number-field/number-field.js +150 -0
- package/dist/components/form/number-field/number-field.stories.js +106 -0
- package/dist/components/form/radio/index.d.ts +1 -0
- package/dist/components/form/radio/index.js +1 -0
- package/dist/components/form/radio/radio.d.ts +11 -0
- package/dist/components/form/radio/radio.js +51 -0
- package/dist/components/form/radio/radio.stories.js +55 -0
- package/dist/components/form/range-field/index.d.ts +1 -0
- package/dist/components/form/range-field/index.js +4 -0
- package/dist/components/form/range-field/range-field.d.ts +17 -0
- package/dist/components/form/range-field/range-field.js +54 -0
- package/dist/components/form/range-field/range-field.stories.js +162 -0
- package/dist/components/form/select/index.d.ts +1 -0
- package/dist/components/form/select/index.js +1 -0
- package/dist/components/form/select/select.d.ts +14 -0
- package/dist/components/form/select/select.js +39 -0
- package/dist/components/form/select/select.stories.js +58 -0
- package/dist/components/form/text-field/index.d.ts +1 -0
- package/dist/components/form/text-field/index.js +1 -0
- package/dist/components/form/text-field/text-field.d.ts +15 -0
- package/dist/components/form/text-field/text-field.js +40 -0
- package/dist/components/form/text-field/text-field.stories.js +61 -0
- package/dist/components/form/textarea/index.d.ts +1 -0
- package/dist/components/form/textarea/index.js +1 -0
- package/dist/components/form/textarea/textarea.d.ts +18 -0
- package/dist/components/form/textarea/textarea.js +56 -0
- package/dist/components/form/textarea/textarea.stories.js +92 -0
- package/dist/components/heading/heading.d.ts +8 -0
- package/dist/components/heading/heading.js +77 -0
- package/dist/components/heading/heading.stories.js +52 -0
- package/dist/components/heading/index.d.ts +1 -0
- package/dist/components/heading/index.js +1 -0
- package/dist/components/icon-button/icon-button.d.ts +8 -0
- package/dist/components/icon-button/icon-button.js +38 -0
- package/dist/components/icon-button/icon-button.stories.js +54 -0
- package/dist/components/icon-button/index.d.ts +1 -0
- package/dist/components/icon-button/index.js +1 -0
- package/dist/components/icon-link/icon-link.d.ts +17 -0
- package/dist/components/icon-link/icon-link.js +37 -0
- package/dist/components/icon-link/icon-link.stories.js +40 -0
- package/dist/components/icon-link/index.d.ts +1 -0
- package/dist/components/icon-link/index.js +1 -0
- package/dist/components/icons/arte-odyssey.d.ts +3 -0
- package/dist/components/icons/arte-odyssey.js +463 -0
- package/dist/components/icons/base.d.ts +9 -0
- package/dist/components/icons/base.js +13 -0
- package/dist/components/icons/github-mark.d.ts +3 -0
- package/dist/components/icons/github-mark.js +33 -0
- package/dist/components/icons/icons.stories.js +276 -0
- package/dist/components/icons/index.d.ts +7 -0
- package/dist/components/icons/index.js +7 -0
- package/dist/components/icons/logo.d.ts +6 -0
- package/dist/components/icons/logo.js +50 -0
- package/dist/components/icons/lucide.d.ts +50 -0
- package/dist/components/icons/lucide.js +569 -0
- package/dist/components/icons/qiita.d.ts +3 -0
- package/dist/components/icons/qiita.js +48 -0
- package/dist/components/icons/twitter.d.ts +3 -0
- package/dist/components/icons/twitter.js +34 -0
- package/dist/components/icons/zenn.d.ts +6 -0
- package/dist/components/icons/zenn.js +41 -0
- package/dist/components/index.d.ts +37 -0
- package/dist/components/index.js +37 -0
- package/dist/components/link-button/index.d.ts +1 -0
- package/dist/components/link-button/index.js +1 -0
- package/dist/components/link-button/link-button.d.ts +19 -0
- package/dist/components/link-button/link-button.js +46 -0
- package/dist/components/link-button/link-button.stories.js +51 -0
- package/dist/components/list-box/hooks.d.ts +37 -0
- package/dist/components/list-box/hooks.js +70 -0
- package/dist/components/list-box/index.d.ts +1 -0
- package/dist/components/list-box/index.js +1 -0
- package/dist/components/list-box/list-box.d.ts +22 -0
- package/dist/components/list-box/list-box.js +145 -0
- package/dist/components/list-box/list-box.stories.js +69 -0
- package/dist/components/modal/index.d.ts +1 -0
- package/dist/components/modal/index.js +1 -0
- package/dist/components/modal/modal.d.ts +8 -0
- package/dist/components/modal/modal.js +118 -0
- package/dist/components/modal/modal.stories.js +22 -0
- package/dist/components/popover/hooks.d.ts +65 -0
- package/dist/components/popover/hooks.js +187 -0
- package/dist/components/popover/index.d.ts +1 -0
- package/dist/components/popover/index.js +1 -0
- package/dist/components/popover/popover.d.ts +18 -0
- package/dist/components/popover/popover.js +141 -0
- package/dist/components/popover/popover.stories.js +45 -0
- package/dist/components/progress/index.d.ts +1 -0
- package/dist/components/progress/index.js +1 -0
- package/dist/components/progress/progress.d.ts +7 -0
- package/dist/components/progress/progress.js +21 -0
- package/dist/components/progress/progress.stories.js +16 -0
- package/dist/components/providers/component-provider.d.ts +2 -0
- package/dist/components/providers/component-provider.js +9 -0
- package/dist/components/providers/index.d.ts +2 -0
- package/dist/components/providers/index.js +2 -0
- package/dist/components/providers/portal-root.d.ts +5 -0
- package/dist/components/providers/portal-root.js +17 -0
- package/dist/components/scroll-linked/index.d.ts +1 -0
- package/dist/components/scroll-linked/index.js +1 -0
- package/dist/components/scroll-linked/scroll-linked.d.ts +2 -0
- package/dist/components/scroll-linked/scroll-linked.js +21 -0
- package/dist/components/scroll-linked/scroll-linked.stories.js +18 -0
- package/dist/components/separator/index.d.ts +1 -0
- package/dist/components/separator/index.js +1 -0
- package/dist/components/separator/separator.d.ts +4 -0
- package/dist/components/separator/separator.js +20 -0
- package/dist/components/separator/separator.stories.js +27 -0
- package/dist/components/tabs/index.d.ts +1 -0
- package/dist/components/tabs/index.js +1 -0
- package/dist/components/tabs/tabs.d.ts +16 -0
- package/dist/components/tabs/tabs.js +145 -0
- package/dist/components/tabs/tabs.stories.js +49 -0
- package/dist/components/text-tag/index.d.ts +1 -0
- package/dist/components/text-tag/index.js +1 -0
- package/dist/components/text-tag/text-tag.d.ts +5 -0
- package/dist/components/text-tag/text-tag.js +23 -0
- package/dist/components/text-tag/text-tag.stories.js +30 -0
- package/dist/components/toast/index.d.ts +1 -0
- package/dist/components/toast/index.js +5 -0
- package/dist/components/toast/provider.d.ts +11 -0
- package/dist/components/toast/provider.js +125 -0
- package/dist/components/toast/toast.d.ts +9 -0
- package/dist/components/toast/toast.js +19 -0
- package/dist/components/toast/toast.stories.js +45 -0
- package/dist/components/tooltip/index.d.ts +1 -0
- package/dist/components/tooltip/index.js +1 -0
- package/dist/components/tooltip/tooltip.d.ts +13 -0
- package/dist/components/tooltip/tooltip.js +60 -0
- package/dist/components/tooltip/tooltip.stories.js +40 -0
- package/dist/helpers/cn.d.ts +2 -0
- package/dist/helpers/cn.js +8 -0
- package/dist/helpers/color/find-all-colors.d.ts +12 -0
- package/dist/helpers/color/find-all-colors.js +210 -0
- package/dist/helpers/is-internal-route.d.ts +1 -0
- package/dist/helpers/is-internal-route.js +12 -0
- package/dist/helpers/number/between.d.ts +1 -0
- package/dist/helpers/number/between.js +19 -0
- package/dist/helpers/number/cast.d.ts +1 -0
- package/dist/helpers/number/cast.js +41 -0
- package/dist/helpers/number/commalize.d.ts +1 -0
- package/dist/helpers/number/commalize.js +22 -0
- package/dist/helpers/number/index.d.ts +4 -0
- package/dist/helpers/number/index.js +10 -0
- package/dist/helpers/number/to-precision.d.ts +1 -0
- package/dist/helpers/number/to-precision.js +17 -0
- package/dist/helpers/uuid-v4.d.ts +1 -0
- package/dist/helpers/uuid-v4.js +32 -0
- package/dist/hooks/click-away/index.d.ts +2 -0
- package/dist/hooks/click-away/index.js +24 -0
- package/dist/hooks/click-away/index.test.js +31 -0
- package/dist/hooks/client/index.d.ts +1 -0
- package/dist/hooks/client/index.js +12 -0
- package/dist/hooks/clipboard/index.d.ts +4 -0
- package/dist/hooks/clipboard/index.js +17 -0
- package/dist/hooks/clipboard/index.test.js +35 -0
- package/dist/hooks/hash/index.d.ts +1 -0
- package/dist/hooks/hash/index.js +28 -0
- package/dist/hooks/hash/index.test.js +46 -0
- package/dist/hooks/index.d.ts +10 -0
- package/dist/hooks/index.js +10 -0
- package/dist/hooks/interval/index.d.ts +1 -0
- package/dist/hooks/interval/index.js +17 -0
- package/dist/hooks/interval/index.test.js +32 -0
- package/dist/hooks/local-storage/index.d.ts +1 -0
- package/dist/hooks/local-storage/index.js +48 -0
- package/dist/hooks/local-storage/index.test.js +82 -0
- package/dist/hooks/scroll-direction/index.d.ts +6 -0
- package/dist/hooks/scroll-direction/index.js +40 -0
- package/dist/hooks/scroll-direction/index.test.js +174 -0
- package/dist/hooks/step/index.d.ts +13 -0
- package/dist/hooks/step/index.js +39 -0
- package/dist/hooks/step/index.test.js +85 -0
- package/dist/hooks/timeout/index.d.ts +1 -0
- package/dist/hooks/timeout/index.js +17 -0
- package/dist/hooks/timeout/index.test.js +31 -0
- package/dist/hooks/window-size/index.d.ts +6 -0
- package/dist/hooks/window-size/index.js +22 -0
- package/dist/hooks/window-size/index.test.js +18 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/package.json +6 -0
- package/dist/styles/index.css +394 -0
- package/dist/types/variables.d.ts +6 -0
- package/dist/types/variables.js +0 -0
- package/package.json +110 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
const useInterval = (callback, timeout) => {
|
|
3
|
+
useEffect(() => {
|
|
4
|
+
let intervalId = null;
|
|
5
|
+
intervalId = window.setInterval(() => {
|
|
6
|
+
callback();
|
|
7
|
+
}, timeout);
|
|
8
|
+
return () => {
|
|
9
|
+
if (intervalId) {
|
|
10
|
+
window.clearInterval(intervalId);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
}, [callback, timeout]);
|
|
14
|
+
};
|
|
15
|
+
export {
|
|
16
|
+
useInterval
|
|
17
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react";
|
|
2
|
+
import { useInterval } from ".";
|
|
3
|
+
describe("useInterval", () => {
|
|
4
|
+
it("\u6307\u5B9A\u6642\u9593\u3054\u3068\u306B\u5B9F\u884C\u3055\u308C\u308B", () => {
|
|
5
|
+
const fn = vi.fn();
|
|
6
|
+
vi.useFakeTimers();
|
|
7
|
+
renderHook(() => {
|
|
8
|
+
useInterval(fn, 1e3);
|
|
9
|
+
});
|
|
10
|
+
vi.advanceTimersByTime(2e3);
|
|
11
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
12
|
+
});
|
|
13
|
+
it("\u6307\u5B9A\u6642\u9593\u3092\u904E\u304E\u306A\u3044\u3068\u5B9F\u884C\u3055\u308C\u306A\u3044", () => {
|
|
14
|
+
const fn = vi.fn();
|
|
15
|
+
vi.useFakeTimers();
|
|
16
|
+
renderHook(() => {
|
|
17
|
+
useInterval(fn, 1e3);
|
|
18
|
+
});
|
|
19
|
+
vi.advanceTimersByTime(10);
|
|
20
|
+
expect(fn).not.toHaveBeenCalled();
|
|
21
|
+
});
|
|
22
|
+
it("\u30A2\u30F3\u30DE\u30A6\u30F3\u30C8\u5F8C\u306F\u5B9F\u884C\u3055\u308C\u306A\u3044", () => {
|
|
23
|
+
const fn = vi.fn();
|
|
24
|
+
vi.useFakeTimers();
|
|
25
|
+
const { unmount } = renderHook(() => {
|
|
26
|
+
useInterval(fn, 1e3);
|
|
27
|
+
});
|
|
28
|
+
unmount();
|
|
29
|
+
vi.advanceTimersByTime(2e3);
|
|
30
|
+
expect(fn).not.toHaveBeenCalled();
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useLocalStorage: <T>(key: string, initialValue: T) => readonly [T, (value: T) => void, () => void];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useCallback, useMemo, useSyncExternalStore } from "react";
|
|
2
|
+
const dispatchStorageEvent = (key, newValue) => window.dispatchEvent(new StorageEvent("storage", { key, newValue }));
|
|
3
|
+
const getLocalStorageItem = (key) => window.localStorage.getItem(key);
|
|
4
|
+
const localStorageSubscribe = (cb) => {
|
|
5
|
+
window.addEventListener("storage", cb);
|
|
6
|
+
return () => {
|
|
7
|
+
window.removeEventListener("storage", cb);
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
const useLocalStorage = (key, initialValue) => {
|
|
11
|
+
const getSnapshot = () => getLocalStorageItem(key);
|
|
12
|
+
const store = useSyncExternalStore(
|
|
13
|
+
localStorageSubscribe,
|
|
14
|
+
getSnapshot,
|
|
15
|
+
() => null
|
|
16
|
+
);
|
|
17
|
+
const current = useMemo(() => {
|
|
18
|
+
try {
|
|
19
|
+
if (store === null) {
|
|
20
|
+
return initialValue;
|
|
21
|
+
}
|
|
22
|
+
return JSON.parse(store);
|
|
23
|
+
} catch (e) {
|
|
24
|
+
console.error(e);
|
|
25
|
+
return initialValue;
|
|
26
|
+
}
|
|
27
|
+
}, [store, initialValue]);
|
|
28
|
+
const handleRemove = useCallback(() => {
|
|
29
|
+
window.localStorage.removeItem(key);
|
|
30
|
+
dispatchStorageEvent(key, null);
|
|
31
|
+
}, [key]);
|
|
32
|
+
const setState = useCallback(
|
|
33
|
+
(value) => {
|
|
34
|
+
if (value === void 0 || value === null) {
|
|
35
|
+
handleRemove();
|
|
36
|
+
} else {
|
|
37
|
+
const stringifiedValue = JSON.stringify(value);
|
|
38
|
+
window.localStorage.setItem(key, stringifiedValue);
|
|
39
|
+
dispatchStorageEvent(key, stringifiedValue);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
[key, handleRemove]
|
|
43
|
+
);
|
|
44
|
+
return [current, setState, handleRemove];
|
|
45
|
+
};
|
|
46
|
+
export {
|
|
47
|
+
useLocalStorage
|
|
48
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { useLocalStorage } from "./index";
|
|
3
|
+
const consoleErrorMock = vi.spyOn(console, "error").mockImplementation(() => void 0);
|
|
4
|
+
describe("useLocalStorage", () => {
|
|
5
|
+
const key = "testKey";
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
localStorage.clear();
|
|
8
|
+
});
|
|
9
|
+
afterAll(() => {
|
|
10
|
+
consoleErrorMock.mockReset();
|
|
11
|
+
});
|
|
12
|
+
it("localStorage\u306B\u5024\u304C\u306A\u3051\u308C\u3070\u521D\u671F\u5024\u3092\u8FD4\u3059", () => {
|
|
13
|
+
const { result } = renderHook(() => useLocalStorage(key, "defaultValue"));
|
|
14
|
+
expect(result.current[0]).toBe("defaultValue");
|
|
15
|
+
});
|
|
16
|
+
it("localStorage\u306B\u5024\u304C\u5B58\u5728\u3042\u308C\u3070\u305D\u306E\u5024\u3092\u8FD4\u3059", () => {
|
|
17
|
+
localStorage.setItem(key, JSON.stringify("storedValue"));
|
|
18
|
+
const { result } = renderHook(() => useLocalStorage(key, "defaultValue"));
|
|
19
|
+
expect(result.current[0]).toBe("storedValue");
|
|
20
|
+
});
|
|
21
|
+
it("\u66F4\u65B0\u51E6\u7406\u3067\u306FlocalStorage\u3068state\u306E\u4E21\u65B9\u3092\u66F4\u65B0\u3059\u308B", () => {
|
|
22
|
+
const { result } = renderHook(() => useLocalStorage(key, "defaultValue"));
|
|
23
|
+
act(() => {
|
|
24
|
+
result.current[1]("newValue");
|
|
25
|
+
});
|
|
26
|
+
expect(localStorage.getItem(key)).toBe(JSON.stringify("newValue"));
|
|
27
|
+
expect(result.current[0]).toBe("newValue");
|
|
28
|
+
});
|
|
29
|
+
it("\u524A\u9664\u51E6\u7406\u3067\u306FlocalStorage\u306F\u5024\u3092\u524A\u9664\u3055\u308C\u3001state\u306F\u521D\u671F\u5024\u306B\u306A\u308B", () => {
|
|
30
|
+
localStorage.setItem(key, JSON.stringify("storedValue"));
|
|
31
|
+
const { result } = renderHook(() => useLocalStorage(key, "defaultValue"));
|
|
32
|
+
act(() => {
|
|
33
|
+
result.current[2]();
|
|
34
|
+
});
|
|
35
|
+
expect(localStorage.getItem(key)).toBeNull();
|
|
36
|
+
expect(result.current[0]).toBe("defaultValue");
|
|
37
|
+
});
|
|
38
|
+
it("null\u3067\u66F4\u65B0\u3057\u305F\u5834\u5408\u306Fremove\u3068\u540C\u3058\u7D50\u679C\u306B\u306A\u308B", () => {
|
|
39
|
+
const { result } = renderHook(
|
|
40
|
+
() => useLocalStorage(key, {
|
|
41
|
+
lang: ["ja", "en"]
|
|
42
|
+
})
|
|
43
|
+
);
|
|
44
|
+
act(() => {
|
|
45
|
+
result.current[1](null);
|
|
46
|
+
});
|
|
47
|
+
expect(localStorage.getItem(key)).toBeNull();
|
|
48
|
+
expect(result.current[0]).toEqual({ lang: ["ja", "en"] });
|
|
49
|
+
});
|
|
50
|
+
it("storage\u30A4\u30D9\u30F3\u30C8\u306E\u767A\u706B\u306B\u5FDC\u3058\u3066\u72B6state\u304C\u66F4\u65B0\u3055\u308C\u308B", () => {
|
|
51
|
+
const { result } = renderHook(() => useLocalStorage(key, "defaultValue"));
|
|
52
|
+
act(() => {
|
|
53
|
+
localStorage.setItem(key, JSON.stringify("updatedValue"));
|
|
54
|
+
window.dispatchEvent(
|
|
55
|
+
new StorageEvent("storage", {
|
|
56
|
+
key,
|
|
57
|
+
newValue: JSON.stringify("updatedValue")
|
|
58
|
+
})
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
expect(result.current[0]).toBe("updatedValue");
|
|
62
|
+
});
|
|
63
|
+
it("\u7570\u306A\u308B\u30AD\u30FC\u306Estorage\u30A4\u30D9\u30F3\u30C8\u306Fstate\u3092\u66F4\u65B0\u3057\u306A\u3044", () => {
|
|
64
|
+
const { result } = renderHook(() => useLocalStorage(key, "defaultValue"));
|
|
65
|
+
act(() => {
|
|
66
|
+
localStorage.setItem("otherKey", JSON.stringify("otherValue"));
|
|
67
|
+
window.dispatchEvent(
|
|
68
|
+
new StorageEvent("storage", {
|
|
69
|
+
key: "otherKey",
|
|
70
|
+
newValue: JSON.stringify("otherValue")
|
|
71
|
+
})
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
expect(result.current[0]).toBe("defaultValue");
|
|
75
|
+
});
|
|
76
|
+
it("JSON\u3092\u30D1\u30FC\u30B9\u3067\u304D\u306A\u3044\u6642\u306F\u30A8\u30E9\u30FC\u3092\u5410\u3044\u3066\u521D\u671F\u5024\u3092\u8FD4\u3059", () => {
|
|
77
|
+
localStorage.setItem(key, "{invalidJSON");
|
|
78
|
+
const { result } = renderHook(() => useLocalStorage(key, "defaultValue"));
|
|
79
|
+
expect(result.current[0]).toBe("defaultValue");
|
|
80
|
+
expect(consoleErrorMock).toHaveBeenCalledOnce();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
const useScrollDirection = (threshold = 50) => {
|
|
4
|
+
const [scrollDirection, setScrollDirection] = useState({
|
|
5
|
+
x: "right",
|
|
6
|
+
y: "up"
|
|
7
|
+
});
|
|
8
|
+
const [prevScrollY, setPrevScrollY] = useState(0);
|
|
9
|
+
const [prevScrollX, setPrevScrollX] = useState(0);
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
const handleScroll = () => {
|
|
12
|
+
const currentScrollY = window.scrollY;
|
|
13
|
+
const currentScrollX = window.scrollX;
|
|
14
|
+
const newDirection = { ...scrollDirection };
|
|
15
|
+
if (currentScrollY > prevScrollY && currentScrollY > threshold) {
|
|
16
|
+
newDirection.y = "down";
|
|
17
|
+
} else if (currentScrollY < prevScrollY) {
|
|
18
|
+
newDirection.y = "up";
|
|
19
|
+
}
|
|
20
|
+
if (currentScrollX > prevScrollX && currentScrollX > threshold) {
|
|
21
|
+
newDirection.x = "right";
|
|
22
|
+
} else if (currentScrollX < prevScrollX) {
|
|
23
|
+
newDirection.x = "left";
|
|
24
|
+
}
|
|
25
|
+
setScrollDirection(newDirection);
|
|
26
|
+
setPrevScrollY(currentScrollY);
|
|
27
|
+
setPrevScrollX(currentScrollX);
|
|
28
|
+
};
|
|
29
|
+
window.addEventListener("scroll", handleScroll, {
|
|
30
|
+
passive: true
|
|
31
|
+
});
|
|
32
|
+
return () => {
|
|
33
|
+
window.removeEventListener("scroll", handleScroll);
|
|
34
|
+
};
|
|
35
|
+
}, [prevScrollY, prevScrollX, scrollDirection, threshold]);
|
|
36
|
+
return scrollDirection;
|
|
37
|
+
};
|
|
38
|
+
export {
|
|
39
|
+
useScrollDirection
|
|
40
|
+
};
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { useScrollDirection } from ".";
|
|
3
|
+
describe("useScrollDirection", () => {
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
Object.defineProperty(window, "scrollY", {
|
|
6
|
+
writable: true,
|
|
7
|
+
value: 0
|
|
8
|
+
});
|
|
9
|
+
Object.defineProperty(window, "scrollX", {
|
|
10
|
+
writable: true,
|
|
11
|
+
value: 0
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
it("\u521D\u671F\u72B6\u614B\u3067\u306Fx: right, y: up\u3092\u8FD4\u3059", () => {
|
|
15
|
+
const { result } = renderHook(() => useScrollDirection());
|
|
16
|
+
expect(result.current).toEqual({ x: "right", y: "up" });
|
|
17
|
+
});
|
|
18
|
+
describe("Vertical scroll", () => {
|
|
19
|
+
it("100px\u4EE5\u4E0A\u4E0B\u306B\u30B9\u30AF\u30ED\u30FC\u30EB\u3059\u308B\u3068y: down\u3092\u8FD4\u3059", () => {
|
|
20
|
+
const { result } = renderHook(() => useScrollDirection());
|
|
21
|
+
act(() => {
|
|
22
|
+
Object.defineProperty(window, "scrollY", { value: 150 });
|
|
23
|
+
window.dispatchEvent(new Event("scroll"));
|
|
24
|
+
});
|
|
25
|
+
expect(result.current.y).toBe("down");
|
|
26
|
+
});
|
|
27
|
+
it("100px\u672A\u6E80\u306E\u30B9\u30AF\u30ED\u30FC\u30EB\u3067\u306Fy: up\u306E\u307E\u307E", () => {
|
|
28
|
+
const { result } = renderHook(() => useScrollDirection());
|
|
29
|
+
act(() => {
|
|
30
|
+
Object.defineProperty(window, "scrollY", { value: 50 });
|
|
31
|
+
window.dispatchEvent(new Event("scroll"));
|
|
32
|
+
});
|
|
33
|
+
expect(result.current.y).toBe("up");
|
|
34
|
+
});
|
|
35
|
+
it("\u4E0A\u306B\u30B9\u30AF\u30ED\u30FC\u30EB\u3059\u308B\u3068y: up\u3092\u8FD4\u3059", () => {
|
|
36
|
+
const { result } = renderHook(() => useScrollDirection());
|
|
37
|
+
act(() => {
|
|
38
|
+
Object.defineProperty(window, "scrollY", { value: 200 });
|
|
39
|
+
window.dispatchEvent(new Event("scroll"));
|
|
40
|
+
});
|
|
41
|
+
expect(result.current.y).toBe("down");
|
|
42
|
+
act(() => {
|
|
43
|
+
Object.defineProperty(window, "scrollY", { value: 100 });
|
|
44
|
+
window.dispatchEvent(new Event("scroll"));
|
|
45
|
+
});
|
|
46
|
+
expect(result.current.y).toBe("up");
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
describe("Horizontal scroll", () => {
|
|
50
|
+
it("100px\u4EE5\u4E0A\u53F3\u306B\u30B9\u30AF\u30ED\u30FC\u30EB\u3059\u308B\u3068x: right\u3092\u8FD4\u3059", () => {
|
|
51
|
+
const { result } = renderHook(() => useScrollDirection());
|
|
52
|
+
act(() => {
|
|
53
|
+
Object.defineProperty(window, "scrollX", { value: 150 });
|
|
54
|
+
window.dispatchEvent(new Event("scroll"));
|
|
55
|
+
});
|
|
56
|
+
expect(result.current.x).toBe("right");
|
|
57
|
+
});
|
|
58
|
+
it("100px\u672A\u6E80\u306E\u30B9\u30AF\u30ED\u30FC\u30EB\u3067\u306Fx: right\u306E\u307E\u307E", () => {
|
|
59
|
+
const { result } = renderHook(() => useScrollDirection());
|
|
60
|
+
act(() => {
|
|
61
|
+
Object.defineProperty(window, "scrollX", { value: 50 });
|
|
62
|
+
window.dispatchEvent(new Event("scroll"));
|
|
63
|
+
});
|
|
64
|
+
expect(result.current.x).toBe("right");
|
|
65
|
+
});
|
|
66
|
+
it("\u5DE6\u306B\u30B9\u30AF\u30ED\u30FC\u30EB\u3059\u308B\u3068x: left\u3092\u8FD4\u3059", () => {
|
|
67
|
+
const { result } = renderHook(() => useScrollDirection());
|
|
68
|
+
act(() => {
|
|
69
|
+
Object.defineProperty(window, "scrollX", { value: 200 });
|
|
70
|
+
window.dispatchEvent(new Event("scroll"));
|
|
71
|
+
});
|
|
72
|
+
expect(result.current.x).toBe("right");
|
|
73
|
+
act(() => {
|
|
74
|
+
Object.defineProperty(window, "scrollX", { value: 100 });
|
|
75
|
+
window.dispatchEvent(new Event("scroll"));
|
|
76
|
+
});
|
|
77
|
+
expect(result.current.x).toBe("left");
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
describe("Combined scroll", () => {
|
|
81
|
+
it("\u7E26\u6A2A\u540C\u6642\u306B\u30B9\u30AF\u30ED\u30FC\u30EB\u3057\u305F\u5834\u5408\u3001\u4E21\u65B9\u5411\u3092\u6B63\u3057\u304F\u691C\u77E5\u3059\u308B", () => {
|
|
82
|
+
const { result } = renderHook(() => useScrollDirection());
|
|
83
|
+
act(() => {
|
|
84
|
+
Object.defineProperty(window, "scrollY", { value: 150 });
|
|
85
|
+
Object.defineProperty(window, "scrollX", { value: 150 });
|
|
86
|
+
window.dispatchEvent(new Event("scroll"));
|
|
87
|
+
});
|
|
88
|
+
expect(result.current).toEqual({ x: "right", y: "down" });
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
it("\u30A2\u30F3\u30DE\u30A6\u30F3\u30C8\u5F8C\u306F\u30A4\u30D9\u30F3\u30C8\u30EA\u30B9\u30CA\u30FC\u304C\u524A\u9664\u3055\u308C\u308B", () => {
|
|
92
|
+
const removeEventListenerSpy = vi.spyOn(window, "removeEventListener");
|
|
93
|
+
const { unmount } = renderHook(() => useScrollDirection());
|
|
94
|
+
unmount();
|
|
95
|
+
expect(removeEventListenerSpy).toHaveBeenCalledWith(
|
|
96
|
+
"scroll",
|
|
97
|
+
expect.any(Function)
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
it("\u30B9\u30AF\u30ED\u30FC\u30EB\u30A4\u30D9\u30F3\u30C8\u304Cpassive: true\u3067\u767B\u9332\u3055\u308C\u308B", () => {
|
|
101
|
+
const addEventListenerSpy = vi.spyOn(window, "addEventListener");
|
|
102
|
+
renderHook(() => useScrollDirection());
|
|
103
|
+
expect(addEventListenerSpy).toHaveBeenCalledWith(
|
|
104
|
+
"scroll",
|
|
105
|
+
expect.any(Function),
|
|
106
|
+
{ passive: true }
|
|
107
|
+
);
|
|
108
|
+
});
|
|
109
|
+
describe("Threshold parameter", () => {
|
|
110
|
+
it("threshold\u304C100\u306E\u5834\u5408\u3001100px\u4EE5\u4E0B\u306E\u30B9\u30AF\u30ED\u30FC\u30EB\u3067\u306F\u65B9\u5411\u304C\u5909\u308F\u3089\u306A\u3044", () => {
|
|
111
|
+
const { result } = renderHook(() => useScrollDirection(100));
|
|
112
|
+
act(() => {
|
|
113
|
+
Object.defineProperty(window, "scrollY", { value: 100 });
|
|
114
|
+
window.dispatchEvent(new Event("scroll"));
|
|
115
|
+
});
|
|
116
|
+
expect(result.current.y).toBe("up");
|
|
117
|
+
act(() => {
|
|
118
|
+
Object.defineProperty(window, "scrollX", { value: 100 });
|
|
119
|
+
window.dispatchEvent(new Event("scroll"));
|
|
120
|
+
});
|
|
121
|
+
expect(result.current.x).toBe("right");
|
|
122
|
+
});
|
|
123
|
+
it("threshold\u304C100\u306E\u5834\u5408\u3001101px\u4EE5\u4E0A\u306E\u30B9\u30AF\u30ED\u30FC\u30EB\u3067\u65B9\u5411\u304C\u5909\u308F\u308B", () => {
|
|
124
|
+
const { result } = renderHook(() => useScrollDirection(100));
|
|
125
|
+
act(() => {
|
|
126
|
+
Object.defineProperty(window, "scrollY", { value: 101 });
|
|
127
|
+
window.dispatchEvent(new Event("scroll"));
|
|
128
|
+
});
|
|
129
|
+
expect(result.current.y).toBe("down");
|
|
130
|
+
act(() => {
|
|
131
|
+
Object.defineProperty(window, "scrollX", { value: 101 });
|
|
132
|
+
window.dispatchEvent(new Event("scroll"));
|
|
133
|
+
});
|
|
134
|
+
expect(result.current.x).toBe("right");
|
|
135
|
+
});
|
|
136
|
+
it("threshold\u304C10\u306E\u5834\u5408\u300110px\u4EE5\u4E0B\u306E\u30B9\u30AF\u30ED\u30FC\u30EB\u3067\u306F\u65B9\u5411\u304C\u5909\u308F\u3089\u306A\u3044", () => {
|
|
137
|
+
const { result } = renderHook(() => useScrollDirection(10));
|
|
138
|
+
act(() => {
|
|
139
|
+
Object.defineProperty(window, "scrollY", { value: 10 });
|
|
140
|
+
window.dispatchEvent(new Event("scroll"));
|
|
141
|
+
});
|
|
142
|
+
expect(result.current.y).toBe("up");
|
|
143
|
+
});
|
|
144
|
+
it("threshold\u304C10\u306E\u5834\u5408\u300111px\u4EE5\u4E0A\u306E\u30B9\u30AF\u30ED\u30FC\u30EB\u3067\u65B9\u5411\u304C\u5909\u308F\u308B", () => {
|
|
145
|
+
const { result } = renderHook(() => useScrollDirection(10));
|
|
146
|
+
act(() => {
|
|
147
|
+
Object.defineProperty(window, "scrollY", { value: 11 });
|
|
148
|
+
window.dispatchEvent(new Event("scroll"));
|
|
149
|
+
});
|
|
150
|
+
expect(result.current.y).toBe("down");
|
|
151
|
+
});
|
|
152
|
+
it("threshold\u304C0\u306E\u5834\u5408\u30011px\u4EE5\u4E0A\u306E\u30B9\u30AF\u30ED\u30FC\u30EB\u3067\u65B9\u5411\u304C\u5909\u308F\u308B", () => {
|
|
153
|
+
const { result } = renderHook(() => useScrollDirection(0));
|
|
154
|
+
act(() => {
|
|
155
|
+
Object.defineProperty(window, "scrollY", { value: 1 });
|
|
156
|
+
window.dispatchEvent(new Event("scroll"));
|
|
157
|
+
});
|
|
158
|
+
expect(result.current.y).toBe("down");
|
|
159
|
+
});
|
|
160
|
+
it("\u30C7\u30D5\u30A9\u30EB\u30C8\u5024\uFF08threshold\u672A\u6307\u5B9A\uFF09\u3067\u306F50px\u306E\u95BE\u5024\u304C\u9069\u7528\u3055\u308C\u308B", () => {
|
|
161
|
+
const { result } = renderHook(() => useScrollDirection());
|
|
162
|
+
act(() => {
|
|
163
|
+
Object.defineProperty(window, "scrollY", { value: 50 });
|
|
164
|
+
window.dispatchEvent(new Event("scroll"));
|
|
165
|
+
});
|
|
166
|
+
expect(result.current.y).toBe("up");
|
|
167
|
+
act(() => {
|
|
168
|
+
Object.defineProperty(window, "scrollY", { value: 51 });
|
|
169
|
+
window.dispatchEvent(new Event("scroll"));
|
|
170
|
+
});
|
|
171
|
+
expect(result.current.y).toBe("down");
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
type Step = {
|
|
2
|
+
count: number;
|
|
3
|
+
next: () => void;
|
|
4
|
+
back: () => void;
|
|
5
|
+
isDisabledBack: boolean;
|
|
6
|
+
isDisabledNext: boolean;
|
|
7
|
+
};
|
|
8
|
+
type UseStep = (props: {
|
|
9
|
+
initialCount: number;
|
|
10
|
+
maxCount: number;
|
|
11
|
+
}) => Step;
|
|
12
|
+
export declare const useStep: UseStep;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useState } from "react";
|
|
3
|
+
const useStep = ({ initialCount, maxCount }) => {
|
|
4
|
+
const [count, setCount] = useState(initialCount);
|
|
5
|
+
const isDisabledBack = count === initialCount;
|
|
6
|
+
const isDisabledNext = count === maxCount;
|
|
7
|
+
const back = useCallback(() => {
|
|
8
|
+
setCount((prev) => {
|
|
9
|
+
if (prev === initialCount) return prev;
|
|
10
|
+
return prev - 1;
|
|
11
|
+
});
|
|
12
|
+
}, [initialCount]);
|
|
13
|
+
const next = useCallback(() => {
|
|
14
|
+
setCount((prev) => {
|
|
15
|
+
if (prev === maxCount) return prev;
|
|
16
|
+
return prev + 1;
|
|
17
|
+
});
|
|
18
|
+
}, [maxCount]);
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
const listener = (e) => {
|
|
21
|
+
if (e.key === "ArrowLeft") back();
|
|
22
|
+
if (e.key === "ArrowRight") next();
|
|
23
|
+
};
|
|
24
|
+
window.addEventListener("keydown", listener);
|
|
25
|
+
return () => {
|
|
26
|
+
window.removeEventListener("keydown", listener);
|
|
27
|
+
};
|
|
28
|
+
}, [back, next]);
|
|
29
|
+
return {
|
|
30
|
+
count,
|
|
31
|
+
next,
|
|
32
|
+
back,
|
|
33
|
+
isDisabledBack,
|
|
34
|
+
isDisabledNext
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
export {
|
|
38
|
+
useStep
|
|
39
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { userEvent } from "@vitest/browser/context";
|
|
3
|
+
import { useStep } from ".";
|
|
4
|
+
describe("useStep", () => {
|
|
5
|
+
it("\u521D\u671F\u72B6\u614B", () => {
|
|
6
|
+
const initialCount = 1;
|
|
7
|
+
const maxCount = 10;
|
|
8
|
+
const { result } = renderHook(() => useStep({ initialCount, maxCount }));
|
|
9
|
+
expect(result.current.count).toBe(initialCount);
|
|
10
|
+
expect(result.current.isDisabledBack).toBeTruthy();
|
|
11
|
+
expect(result.current.isDisabledNext).toBeFalsy();
|
|
12
|
+
});
|
|
13
|
+
it("next\u3067initialCount\u304B\u30891\u9032\u3080", () => {
|
|
14
|
+
const initialCount = 1;
|
|
15
|
+
const maxCount = 10;
|
|
16
|
+
const { result } = renderHook(() => useStep({ initialCount, maxCount }));
|
|
17
|
+
act(() => {
|
|
18
|
+
result.current.next();
|
|
19
|
+
});
|
|
20
|
+
expect(result.current.count).toBe(initialCount + 1);
|
|
21
|
+
expect(result.current.isDisabledBack).toBeFalsy();
|
|
22
|
+
expect(result.current.isDisabledNext).toBeFalsy();
|
|
23
|
+
});
|
|
24
|
+
it("initialCount\u304B\u3089\u306Fback\u3067\u304D\u306A\u3044", () => {
|
|
25
|
+
const initialCount = 1;
|
|
26
|
+
const maxCount = 10;
|
|
27
|
+
const { result } = renderHook(() => useStep({ initialCount, maxCount }));
|
|
28
|
+
act(() => {
|
|
29
|
+
result.current.back();
|
|
30
|
+
});
|
|
31
|
+
expect(result.current.count).toBe(initialCount);
|
|
32
|
+
expect(result.current.isDisabledBack).toBeTruthy();
|
|
33
|
+
expect(result.current.isDisabledNext).toBeFalsy();
|
|
34
|
+
});
|
|
35
|
+
it("maxCount\u307E\u3067\u9032\u3080", () => {
|
|
36
|
+
const initialCount = 1;
|
|
37
|
+
const maxCount = 3;
|
|
38
|
+
const { result } = renderHook(() => useStep({ initialCount, maxCount }));
|
|
39
|
+
act(() => {
|
|
40
|
+
result.current.next();
|
|
41
|
+
result.current.next();
|
|
42
|
+
});
|
|
43
|
+
expect(result.current.count).toBe(maxCount);
|
|
44
|
+
expect(result.current.isDisabledBack).toBeFalsy();
|
|
45
|
+
expect(result.current.isDisabledNext).toBeTruthy();
|
|
46
|
+
});
|
|
47
|
+
it("maxCount\u4EE5\u4E0A\u306F\u9032\u3081\u306A\u3044", () => {
|
|
48
|
+
const initialCount = 1;
|
|
49
|
+
const maxCount = 3;
|
|
50
|
+
const { result } = renderHook(() => useStep({ initialCount, maxCount }));
|
|
51
|
+
act(() => {
|
|
52
|
+
result.current.next();
|
|
53
|
+
result.current.next();
|
|
54
|
+
result.current.next();
|
|
55
|
+
});
|
|
56
|
+
expect(result.current.count).toBe(maxCount);
|
|
57
|
+
expect(result.current.isDisabledBack).toBeFalsy();
|
|
58
|
+
expect(result.current.isDisabledNext).toBeTruthy();
|
|
59
|
+
});
|
|
60
|
+
it("next\u3068back\u3092\u7D44\u307F\u5408\u308F\u305B\u3066\u5229\u7528\u3067\u304D\u308B", () => {
|
|
61
|
+
const initialCount = 1;
|
|
62
|
+
const maxCount = 3;
|
|
63
|
+
const { result } = renderHook(() => useStep({ initialCount, maxCount }));
|
|
64
|
+
act(() => {
|
|
65
|
+
result.current.next();
|
|
66
|
+
result.current.back();
|
|
67
|
+
});
|
|
68
|
+
expect(result.current.count).toBe(initialCount);
|
|
69
|
+
expect(result.current.isDisabledBack).toBeTruthy();
|
|
70
|
+
expect(result.current.isDisabledNext).toBeFalsy();
|
|
71
|
+
});
|
|
72
|
+
it("\u5DE6\u53F3\u30AD\u30FC\u3067\u64CD\u4F5C\u3067\u304D\u308B", async () => {
|
|
73
|
+
const initialCount = 1;
|
|
74
|
+
const maxCount = 3;
|
|
75
|
+
const { result } = renderHook(() => useStep({ initialCount, maxCount }));
|
|
76
|
+
await act(async () => {
|
|
77
|
+
await userEvent.keyboard("{arrowright}");
|
|
78
|
+
});
|
|
79
|
+
expect(result.current.count).toBe(initialCount + 1);
|
|
80
|
+
await act(async () => {
|
|
81
|
+
await userEvent.keyboard("{arrowleft}");
|
|
82
|
+
});
|
|
83
|
+
expect(result.current.count).toBe(initialCount);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useTimeout: (callback: () => void, delay: number) => void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
const useTimeout = (callback, delay) => {
|
|
3
|
+
useEffect(() => {
|
|
4
|
+
let timeoutId = null;
|
|
5
|
+
timeoutId = window.setTimeout(() => {
|
|
6
|
+
callback();
|
|
7
|
+
}, delay);
|
|
8
|
+
return () => {
|
|
9
|
+
if (timeoutId) {
|
|
10
|
+
window.clearTimeout(timeoutId);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
}, [callback, delay]);
|
|
14
|
+
};
|
|
15
|
+
export {
|
|
16
|
+
useTimeout
|
|
17
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react";
|
|
2
|
+
import { useTimeout } from ".";
|
|
3
|
+
describe("useTimeout", () => {
|
|
4
|
+
it("\u6307\u5B9A\u6642\u9593\u5F8C\u306B\u5B9F\u884C\u3055\u308C\u308B", () => {
|
|
5
|
+
const fn = vi.fn();
|
|
6
|
+
vi.useFakeTimers();
|
|
7
|
+
renderHook(() => {
|
|
8
|
+
useTimeout(fn, 1e3);
|
|
9
|
+
});
|
|
10
|
+
vi.advanceTimersByTime(1e3);
|
|
11
|
+
expect(fn).toHaveBeenCalledOnce();
|
|
12
|
+
});
|
|
13
|
+
it("\u6307\u5B9A\u6642\u9593\u524D\u306B\u5B9F\u884C\u3055\u308C\u306A\u3044", () => {
|
|
14
|
+
const fn = vi.fn();
|
|
15
|
+
vi.useFakeTimers();
|
|
16
|
+
renderHook(() => {
|
|
17
|
+
useTimeout(fn, 1e3);
|
|
18
|
+
});
|
|
19
|
+
vi.advanceTimersByTime(10);
|
|
20
|
+
expect(fn).not.toHaveBeenCalled();
|
|
21
|
+
});
|
|
22
|
+
it("\u6307\u5B9A\u6642\u9593\u524D\u306B\u30A2\u30F3\u30DE\u30A6\u30F3\u30C8\u3055\u308C\u306A\u3044\u5834\u5408\u306F\u5B9F\u884C\u3055\u308C\u306A\u3044", () => {
|
|
23
|
+
const fn = vi.fn();
|
|
24
|
+
vi.useFakeTimers();
|
|
25
|
+
const { unmount } = renderHook(() => {
|
|
26
|
+
useTimeout(fn, 1e3);
|
|
27
|
+
});
|
|
28
|
+
unmount();
|
|
29
|
+
expect(fn).not.toHaveBeenCalled();
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useLayoutEffect, useState } from "react";
|
|
3
|
+
const useWindowSize = () => {
|
|
4
|
+
const [size, setSize] = useState({ width: 0, height: 0 });
|
|
5
|
+
useLayoutEffect(() => {
|
|
6
|
+
const handleResize = () => {
|
|
7
|
+
setSize({
|
|
8
|
+
width: window.innerWidth,
|
|
9
|
+
height: window.innerHeight
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
handleResize();
|
|
13
|
+
window.addEventListener("resize", handleResize);
|
|
14
|
+
return () => {
|
|
15
|
+
window.removeEventListener("resize", handleResize);
|
|
16
|
+
};
|
|
17
|
+
}, []);
|
|
18
|
+
return size;
|
|
19
|
+
};
|
|
20
|
+
export {
|
|
21
|
+
useWindowSize
|
|
22
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { useWindowSize } from ".";
|
|
3
|
+
describe("useWindowSize", () => {
|
|
4
|
+
it("window\u30B5\u30A4\u30BA\u306E\u5909\u66F4\u306B\u5408\u308F\u305B\u3066\u73FE\u5728\u306Ewindow\u30B5\u30A4\u30BA\u3092\u53D6\u5F97\u3059\u308B", () => {
|
|
5
|
+
const initWindowSize = { width: 0, height: 0 };
|
|
6
|
+
const resizedWindowSize = { width: 1e3, height: 1e3 };
|
|
7
|
+
window.innerWidth = initWindowSize.width;
|
|
8
|
+
window.innerHeight = initWindowSize.height;
|
|
9
|
+
const { result } = renderHook(() => useWindowSize());
|
|
10
|
+
expect(result.current).toEqual(initWindowSize);
|
|
11
|
+
window.innerWidth = resizedWindowSize.width;
|
|
12
|
+
window.innerHeight = resizedWindowSize.height;
|
|
13
|
+
act(() => {
|
|
14
|
+
window.dispatchEvent(new Event("resize"));
|
|
15
|
+
});
|
|
16
|
+
expect(result.current).toEqual(resizedWindowSize);
|
|
17
|
+
});
|
|
18
|
+
});
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED