|
| 1 | +/** @jsxImportSource solid-js - we use Solid.js as JSX here */ |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { constructCoreClass } from './class' |
| 4 | + |
| 5 | +const lazyImportMock = vi.fn((fn) => fn()) |
| 6 | +const renderMock = vi.fn() |
| 7 | +const portalMock = vi.fn((props: any) => <div>{props.children}</div>) |
| 8 | + |
| 9 | +vi.mock('solid-js', async () => { |
| 10 | + const actual = await vi.importActual<any>('solid-js') |
| 11 | + return { |
| 12 | + ...actual, |
| 13 | + lazy: lazyImportMock, |
| 14 | + } |
| 15 | +}) |
| 16 | + |
| 17 | +vi.mock('solid-js/web', async () => { |
| 18 | + const actual = await vi.importActual<any>('solid-js/web') |
| 19 | + return { |
| 20 | + ...actual, |
| 21 | + render: renderMock, |
| 22 | + Portal: portalMock, |
| 23 | + } |
| 24 | +}) |
| 25 | + |
| 26 | +describe('constructCoreClass', () => { |
| 27 | + beforeEach(() => { |
| 28 | + vi.clearAllMocks() |
| 29 | + }) |
| 30 | + it('should export DevtoolsCore and NoOpDevtoolsCore classes and make no calls to Solid.js primitives', () => { |
| 31 | + const [DevtoolsCore, NoOpDevtoolsCore] = constructCoreClass(() => ( |
| 32 | + <div>Test Component</div> |
| 33 | + )) |
| 34 | + expect(DevtoolsCore).toBeDefined() |
| 35 | + expect(NoOpDevtoolsCore).toBeDefined() |
| 36 | + expect(lazyImportMock).not.toHaveBeenCalled() |
| 37 | + }) |
| 38 | + |
| 39 | + it('DevtoolsCore should call solid primitives when mount is called', async () => { |
| 40 | + const [DevtoolsCore, _] = constructCoreClass(() => ( |
| 41 | + <div>Test Component</div> |
| 42 | + )) |
| 43 | + const instance = new DevtoolsCore() |
| 44 | + await instance.mount(document.createElement('div'), 'dark') |
| 45 | + expect(renderMock).toHaveBeenCalled() |
| 46 | + }) |
| 47 | + |
| 48 | + it('DevtoolsCore should throw if mount is called twice without unmounting', async () => { |
| 49 | + const [DevtoolsCore, _] = constructCoreClass(() => ( |
| 50 | + <div>Test Component</div> |
| 51 | + )) |
| 52 | + const instance = new DevtoolsCore() |
| 53 | + await instance.mount(document.createElement('div'), 'dark') |
| 54 | + await expect( |
| 55 | + instance.mount(document.createElement('div'), 'dark'), |
| 56 | + ).rejects.toThrow('Devtools is already mounted') |
| 57 | + }) |
| 58 | + |
| 59 | + it('DevtoolsCore should throw if unmount is called before mount', () => { |
| 60 | + const [DevtoolsCore, _] = constructCoreClass(() => ( |
| 61 | + <div>Test Component</div> |
| 62 | + )) |
| 63 | + const instance = new DevtoolsCore() |
| 64 | + expect(() => instance.unmount()).toThrow('Devtools is not mounted') |
| 65 | + }) |
| 66 | + |
| 67 | + it('DevtoolsCore should allow mount after unmount', async () => { |
| 68 | + const [DevtoolsCore, _] = constructCoreClass(() => ( |
| 69 | + <div>Test Component</div> |
| 70 | + )) |
| 71 | + const instance = new DevtoolsCore() |
| 72 | + await instance.mount(document.createElement('div'), 'dark') |
| 73 | + instance.unmount() |
| 74 | + await expect( |
| 75 | + instance.mount(document.createElement('div'), 'dark'), |
| 76 | + ).resolves.not.toThrow() |
| 77 | + }) |
| 78 | + |
| 79 | + it('NoOpDevtoolsCore should not call any solid primitives when mount is called', async () => { |
| 80 | + const [_, NoOpDevtoolsCore] = constructCoreClass(() => ( |
| 81 | + <div>Test Component</div> |
| 82 | + )) |
| 83 | + const noOpInstance = new NoOpDevtoolsCore() |
| 84 | + await noOpInstance.mount(document.createElement('div'), 'dark') |
| 85 | + |
| 86 | + expect(lazyImportMock).not.toHaveBeenCalled() |
| 87 | + expect(renderMock).not.toHaveBeenCalled() |
| 88 | + expect(portalMock).not.toHaveBeenCalled() |
| 89 | + }) |
| 90 | + |
| 91 | + it('NoOpDevtoolsCore should not throw if mount is called multiple times', async () => { |
| 92 | + const [_, NoOpDevtoolsCore] = constructCoreClass(() => ( |
| 93 | + <div>Test Component</div> |
| 94 | + )) |
| 95 | + const noOpInstance = new NoOpDevtoolsCore() |
| 96 | + await noOpInstance.mount(document.createElement('div'), 'dark') |
| 97 | + await expect( |
| 98 | + noOpInstance.mount(document.createElement('div'), 'dark'), |
| 99 | + ).resolves.not.toThrow() |
| 100 | + }) |
| 101 | + |
| 102 | + it('NoOpDevtoolsCore should not throw if unmount is called before mount', () => { |
| 103 | + const [_, NoOpDevtoolsCore] = constructCoreClass(() => ( |
| 104 | + <div>Test Component</div> |
| 105 | + )) |
| 106 | + const noOpInstance = new NoOpDevtoolsCore() |
| 107 | + expect(() => noOpInstance.unmount()).not.toThrow() |
| 108 | + }) |
| 109 | + |
| 110 | + it('NoOpDevtoolsCore should not throw if unmount is called after mount', async () => { |
| 111 | + const [_, NoOpDevtoolsCore] = constructCoreClass(() => ( |
| 112 | + <div>Test Component</div> |
| 113 | + )) |
| 114 | + const noOpInstance = new NoOpDevtoolsCore() |
| 115 | + await noOpInstance.mount(document.createElement('div'), 'dark') |
| 116 | + expect(() => noOpInstance.unmount()).not.toThrow() |
| 117 | + }) |
| 118 | +}) |
0 commit comments