From 5792f5912782c7c54a5e0dbd5818fc0dd0f04570 Mon Sep 17 00:00:00 2001 From: MisRob Date: Thu, 12 Oct 2023 14:06:52 +0200 Subject: [PATCH] wip --- jest.conf/setup.js | 2 ++ .../useKShow/__tests__/index.spec.js | 30 +++++++++++++++++++ lib/composables/useKShow/index.js | 6 +++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 lib/composables/useKShow/__tests__/index.spec.js diff --git a/jest.conf/setup.js b/jest.conf/setup.js index 23894afcb..b41acc4eb 100644 --- a/jest.conf/setup.js +++ b/jest.conf/setup.js @@ -6,6 +6,7 @@ import * as AphroditeNoImportant from 'aphrodite/no-important'; import Vue from 'vue'; import VueRouter from 'vue-router'; import VueIntl from 'vue-intl'; +import VueCompositionAPI from '@vue/composition-api'; import KThemePlugin from '../lib/KThemePlugin'; global.beforeEach(() => { @@ -26,6 +27,7 @@ global.afterEach(() => { // Register Vue plugins and components Vue.use(VueRouter); +Vue.use(VueCompositionAPI); Vue.use(KThemePlugin); Vue.use(VueIntl); diff --git a/lib/composables/useKShow/__tests__/index.spec.js b/lib/composables/useKShow/__tests__/index.spec.js new file mode 100644 index 000000000..ac4a8e862 --- /dev/null +++ b/lib/composables/useKShow/__tests__/index.spec.js @@ -0,0 +1,30 @@ +import useKShow from "../index"; + +const { show } = useKShow() + +// https://github.com/jestjs/jest/issues/11607 +describe('useKShow', () => { + beforeAll(() => { + jest.useFakeTimers('legacy'); + }) + afterAll(() => { + jest.useRealTimers() + }) + + it('TODO', () => { + let isShowed = show('show-1', true, 5000) + expect(isShowed).toBe(true) + + jest.advanceTimersByTime(1000); + isShowed = show('show-1', false, 5000) + expect(isShowed).toBe(true) + + jest.advanceTimersByTime(2000); + isShowed = show('show-1', false, 5000) + expect(isShowed).toBe(true) + + jest.advanceTimersByTime(3000); + isShowed = show('show-1', false, 5000) + expect(isShowed).toBe(false) + }) +}) diff --git a/lib/composables/useKShow/index.js b/lib/composables/useKShow/index.js index fc5399235..3658aab94 100644 --- a/lib/composables/useKShow/index.js +++ b/lib/composables/useKShow/index.js @@ -16,6 +16,7 @@ export default function useKShow() { } function _freezeItem(key) { + console.log('_freezeItem') const item = itemsMap[key]; item.isFrozen.value = true; item.timeoutId = setTimeout(() => { @@ -63,7 +64,7 @@ export default function useKShow() { return shouldShow; } itemsMap[key] = { - shouldShow, + shouldShow: false, minVisibleTime, isFrozen: ref(false), timeoutId: null, @@ -74,6 +75,8 @@ export default function useKShow() { const previousShouldShow = item.shouldShow; const previousMinVisibleTime = item.minVisibleTime; + console.log('previousShouldShow', previousShouldShow) + console.log('shouldShow', shouldShow) if (previousShouldShow !== shouldShow) { item.shouldShow = shouldShow; // freeze when changing from false to true @@ -92,6 +95,7 @@ export default function useKShow() { _freezeItem(key); } + console.log(item.isFrozen.value) return shouldShow || item.isFrozen.value; }