Skip to content

Commit

Permalink
Add tests for useKShow and fix a bug
Browse files Browse the repository at this point in the history
when the instance wasn't frozen
as expected in some cases due to missing
initial `shouldShow` default value
  • Loading branch information
MisRob committed Oct 16, 2023
1 parent dd5ba27 commit 6e09027
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions jest.conf/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -26,6 +27,7 @@ global.afterEach(() => {

// Register Vue plugins and components
Vue.use(VueRouter);
Vue.use(VueCompositionAPI);
Vue.use(KThemePlugin);
Vue.use(VueIntl);

Expand Down
42 changes: 42 additions & 0 deletions lib/composables/useKShow/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import useKShow from '../index';

const { show } = useKShow();

describe('useKShow', () => {
beforeAll(() => {
// https://github.com/jestjs/jest/issues/11607
jest.useFakeTimers('legacy');
});
afterAll(() => {
jest.useRealTimers();
});

it(`'show' returns 'true' at least for a specified duration after an initial trigger`, () => {
// Test that 'show1' instance evaluates to true
// for at least 5 seconds after an initial trigger
// Test 'show2' instance in the same way but for 2 seconds
// (this is to test that there are no clashes between more
// 'show' instances)
expect(show('show1', true, 5000)).toBe(true);
expect(show('show2', true, 2000)).toBe(true);

// change 'shouldShow' condition to falsy
expect(show('show1', false, 5000)).toBe(true);
expect(show('show2', false, 2000)).toBe(true);

// after 1 second in total
jest.advanceTimersByTime(1000);
expect(show('show1', false, 5000)).toBe(true);
expect(show('show2', false, 2000)).toBe(true);

// after 2.5 seconds in total
jest.advanceTimersByTime(1500);
expect(show('show1', false, 5000)).toBe(true);
expect(show('show2', false, 2000)).toBe(false);

// after 5.5 seconds in total
jest.advanceTimersByTime(3000);
expect(show('show1', false, 5000)).toBe(false);
expect(show('show2', false, 2000)).toBe(false);
});
});
2 changes: 1 addition & 1 deletion lib/composables/useKShow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function useKShow() {
return shouldShow;
}
itemsMap[key] = {
shouldShow,
shouldShow: false,
minVisibleTime,
isFrozen: ref(false),
timeoutId: null,
Expand Down

0 comments on commit 6e09027

Please sign in to comment.