|
| 1 | +import { test, expect, beforeEach } from 'vitest'; |
| 2 | +import { effect, nextTick } from 'vue'; |
| 3 | +import useProgressBar from '@/composables/progress-bar.js'; |
| 4 | + |
| 5 | +const progress = useProgressBar(); |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + // Reset any leftover operations between tests. |
| 9 | + progress.names().slice().forEach((name) => progress.complete(name)); |
| 10 | +}); |
| 11 | + |
| 12 | +test('loading is reported while operations are in flight', () => { |
| 13 | + expect(progress.isComplete()).toBe(true); |
| 14 | + |
| 15 | + progress.loading('a', true); |
| 16 | + expect(progress.isComplete()).toBe(false); |
| 17 | + expect(progress.count()).toBe(1); |
| 18 | + |
| 19 | + progress.loading('a', false); |
| 20 | + expect(progress.isComplete()).toBe(true); |
| 21 | + expect(progress.count()).toBe(0); |
| 22 | +}); |
| 23 | + |
| 24 | +test('reactive consumers are only notified on start and stop transitions', async () => { |
| 25 | + let runs = 0; |
| 26 | + let lastComplete; |
| 27 | + |
| 28 | + effect(() => { |
| 29 | + lastComplete = progress.isComplete(); |
| 30 | + runs++; |
| 31 | + }); |
| 32 | + |
| 33 | + expect(runs).toBe(1); // initial run |
| 34 | + expect(lastComplete).toBe(true); |
| 35 | + |
| 36 | + // Add a large batch of operations synchronously. |
| 37 | + for (let i = 0; i < 200; i++) { |
| 38 | + progress.loading(`op-${i}`, true); |
| 39 | + } |
| 40 | + await nextTick(); |
| 41 | + |
| 42 | + // Only one additional run for the idle -> loading transition. |
| 43 | + expect(runs).toBe(2); |
| 44 | + expect(lastComplete).toBe(false); |
| 45 | + |
| 46 | + // Remove all but the last operation. Still loading, so no new notification. |
| 47 | + for (let i = 0; i < 199; i++) { |
| 48 | + progress.loading(`op-${i}`, false); |
| 49 | + } |
| 50 | + await nextTick(); |
| 51 | + expect(runs).toBe(2); |
| 52 | + |
| 53 | + // Remove the final operation. Now one run for the loading -> idle transition. |
| 54 | + progress.loading('op-199', false); |
| 55 | + await nextTick(); |
| 56 | + expect(runs).toBe(3); |
| 57 | + expect(lastComplete).toBe(true); |
| 58 | +}); |
0 commit comments