|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { Channel } from 'storybook/internal/channels'; |
| 5 | + |
| 6 | +import { clearChannel, getChannel as readInstalledChannel } from '../../channels/channel-slot.ts'; |
| 7 | +import { AddonStore } from './addons.ts'; |
| 8 | + |
| 9 | +describe('AddonStore channel', () => { |
| 10 | + beforeEach(() => { |
| 11 | + clearChannel(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + clearChannel(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns a throwaway mock before a channel is installed, without poisoning the shared slot', () => { |
| 19 | + const store = new AddonStore(); |
| 20 | + |
| 21 | + // Reading early must not crash... |
| 22 | + expect(store.getChannel()).toBeDefined(); |
| 23 | + |
| 24 | + // ...but it must not install anything into the shared slot, so the real channel installed later |
| 25 | + // at the runtime entry point can still take over. |
| 26 | + expect(readInstalledChannel()).toBeNull(); |
| 27 | + expect(store.hasChannel()).toBe(false); |
| 28 | + }); |
| 29 | + |
| 30 | + it('lets a real setChannel() take over after an early getChannel() fallback', async () => { |
| 31 | + const store = new AddonStore(); |
| 32 | + |
| 33 | + // Simulate an errant early read (e.g. at manager module-eval before the channel is installed). |
| 34 | + store.getChannel(); |
| 35 | + |
| 36 | + const real = new Channel({}); |
| 37 | + store.setChannel(real); |
| 38 | + |
| 39 | + expect(store.getChannel()).toBe(real); |
| 40 | + expect(readInstalledChannel()).toBe(real); |
| 41 | + await expect(store.ready()).resolves.toBe(real); |
| 42 | + }); |
| 43 | +}); |
0 commit comments