Skip to content

Commit

Permalink
Merge pull request #472 from MisRob/usekshow-tests
Browse files Browse the repository at this point in the history
Add tests for useKShow and fix a bug
  • Loading branch information
MisRob authored Oct 19, 2023
2 parents 942d6e2 + beee9de commit 776c0a6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ Changelog is rather internal in nature. See release notes for the public overvie

<!-- All new changelog items should come here -->

- [#472]
- **Description:** Fix useKShow bug and add tests
- **Products impact:** bugfix
- **Addresses:** -
- **Components:** useKShow
- **Breaking:** no
- **Impacts a11y:** no
- **Guidance:** -

[#472]: https://github.com/learningequality/kolibri-design-system/pull/472

- [#463]
- **Description:** Add deprecation warning for KResponsiveWindowMixin
- **Products impact:** updated API
Expand Down
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);
});
});
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 776c0a6

Please sign in to comment.