Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: delay loading spinner display #2521

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions packages/portal/src/components/generic/LoadingSpinner.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<template>
<span
role="status"
data-qa="loading spinner"
>
<span class="visually-hidden">
{{ statusMessage || $t('loading') }}
</span>
<!-- outer span needed to permit parent component to set its own v-show
without overwriting the local one -->
<span>
<span
class="spinner-border"
:class="`spinner-border-${size}`"
aria-hidden="true"
/>
v-show="visible"
role="status"
data-qa="loading spinner"
>
<span class="visually-hidden">
{{ statusMessage || $t('loading') }}
</span>
<span
class="spinner-border"
:class="`spinner-border-${size}`"
aria-hidden="true"
/>
</span>
</span>
</template>

Expand All @@ -19,6 +24,13 @@
name: 'LoadingSpinner',

props: {
/**
* Delay to wait before showing the spinner, in ms
*/
delay: {
type: Number,
default: 500
},
statusMessage: {
type: String,
default: null
Expand All @@ -31,6 +43,26 @@
type: String,
default: 'sm'
}
},

data() {
return {
timeout: null,
visible: this.delay === 0
};
},

beforeDestroy() {
this.timeout = null;
},

mounted() {
if (this.delay > 0) {
this.timeout = setTimeout(() => {
this.timeout = null;
this.visible = true;
}, this.delay);
}
}
};
</script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
import { shallowMount } from '@vue/test-utils';
import LoadingSpinner from '@/components/generic/LoadingSpinner.vue';

const factory = () => shallowMount(LoadingSpinner, {
const factory = ({ propsData } = {}) => shallowMount(LoadingSpinner, {
propsData,
mocks: {
$t: (key) => key
}
});

describe('components/generic/LoadingSpinner', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});

it('uses the fallback message when none has been passed', () => {
const wrapper = factory();

const spinner = wrapper.find('[data-qa="loading spinner"]');

expect(spinner.text()).toBe('loading');
});

describe('delay', () => {
it('prevents the spinner showing immediately', async() => {
const wrapper = factory({ propsData: { delay: 500 } });

const spinner = wrapper.find('[data-qa="loading spinner"]');
await wrapper.vm.$nextTick();

expect(spinner.isVisible()).toBe(false);
});

it('shows the spinner after delay has elapsed', async() => {
const wrapper = factory({ propsData: { delay: 500 } });

const spinner = wrapper.find('[data-qa="loading spinner"]');
jest.runAllTimers();
await wrapper.vm.$nextTick();

expect(spinner.isVisible()).toBe(true);
});
});
});
Loading