|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import starlightGiscus from '../index'; |
| 3 | +import type { StarlightGiscusUserConfig } from '../index'; |
| 4 | + |
| 5 | +describe('starlightGiscus plugin', () => { |
| 6 | + const validConfig: StarlightGiscusUserConfig = { |
| 7 | + repo: 'user/repo', |
| 8 | + repoId: 'R_123', |
| 9 | + category: 'General', |
| 10 | + categoryId: 'DIC_123', |
| 11 | + }; |
| 12 | + |
| 13 | + it('should create a plugin with correct name', () => { |
| 14 | + const plugin = starlightGiscus(validConfig); |
| 15 | + expect(plugin.name).toBe('starlight-giscus'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should have config:setup hook', () => { |
| 19 | + const plugin = starlightGiscus(validConfig); |
| 20 | + expect(plugin.hooks).toBeDefined(); |
| 21 | + expect(plugin.hooks['config:setup']).toBeDefined(); |
| 22 | + expect(typeof plugin.hooks['config:setup']).toBe('function'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should throw error for invalid config', () => { |
| 26 | + const invalidConfig = { repo: 'user/repo' } as StarlightGiscusUserConfig; |
| 27 | + expect(() => starlightGiscus(invalidConfig)).toThrow(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should call updateConfig in hook', () => { |
| 31 | + const plugin = starlightGiscus(validConfig); |
| 32 | + const updateConfig = vi.fn(); |
| 33 | + const addIntegration = vi.fn(); |
| 34 | + const logger = { |
| 35 | + warn: vi.fn(), |
| 36 | + info: vi.fn(), |
| 37 | + error: vi.fn(), |
| 38 | + debug: vi.fn(), |
| 39 | + label: 'test', |
| 40 | + fork: vi.fn(), |
| 41 | + } as any; |
| 42 | + |
| 43 | + const configSetup = plugin.hooks['config:setup']; |
| 44 | + expect(configSetup).toBeDefined(); |
| 45 | + |
| 46 | + configSetup!({ |
| 47 | + logger, |
| 48 | + config: { title: 'Test', components: {} } as any, |
| 49 | + updateConfig, |
| 50 | + addIntegration, |
| 51 | + command: 'dev', |
| 52 | + isRestart: false, |
| 53 | + } as any); |
| 54 | + |
| 55 | + expect(updateConfig).toHaveBeenCalled(); |
| 56 | + expect(addIntegration).toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should add Pagination component override', () => { |
| 60 | + const plugin = starlightGiscus(validConfig); |
| 61 | + const updateConfig = vi.fn(); |
| 62 | + const addIntegration = vi.fn(); |
| 63 | + const logger = { |
| 64 | + warn: vi.fn(), |
| 65 | + info: vi.fn(), |
| 66 | + error: vi.fn(), |
| 67 | + debug: vi.fn(), |
| 68 | + label: 'test', |
| 69 | + fork: vi.fn(), |
| 70 | + } as any; |
| 71 | + |
| 72 | + const configSetup = plugin.hooks['config:setup']; |
| 73 | + expect(configSetup).toBeDefined(); |
| 74 | + |
| 75 | + configSetup!({ |
| 76 | + logger, |
| 77 | + config: { title: 'Test', components: {} } as any, |
| 78 | + updateConfig, |
| 79 | + addIntegration, |
| 80 | + command: 'dev', |
| 81 | + isRestart: false, |
| 82 | + } as any); |
| 83 | + |
| 84 | + const updateCall = updateConfig.mock.calls[0][0]; |
| 85 | + expect(updateCall.components).toBeDefined(); |
| 86 | + expect(updateCall.components.Pagination).toBe( |
| 87 | + 'starlight-giscus/overrides/Pagination.astro' |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should add astro:config:setup integration', () => { |
| 92 | + const plugin = starlightGiscus(validConfig); |
| 93 | + const updateConfig = vi.fn(); |
| 94 | + const addIntegration = vi.fn(); |
| 95 | + const logger = { |
| 96 | + warn: vi.fn(), |
| 97 | + info: vi.fn(), |
| 98 | + error: vi.fn(), |
| 99 | + debug: vi.fn(), |
| 100 | + label: 'test', |
| 101 | + fork: vi.fn(), |
| 102 | + } as any; |
| 103 | + |
| 104 | + const configSetup = plugin.hooks['config:setup']; |
| 105 | + expect(configSetup).toBeDefined(); |
| 106 | + |
| 107 | + configSetup!({ |
| 108 | + logger, |
| 109 | + config: { title: 'Test', components: {} } as any, |
| 110 | + updateConfig, |
| 111 | + addIntegration, |
| 112 | + command: 'dev', |
| 113 | + isRestart: false, |
| 114 | + } as any); |
| 115 | + |
| 116 | + expect(addIntegration).toHaveBeenCalledWith( |
| 117 | + expect.objectContaining({ |
| 118 | + name: 'starlight-giscus-integration', |
| 119 | + hooks: expect.objectContaining({ |
| 120 | + 'astro:config:setup': expect.any(Function), |
| 121 | + }), |
| 122 | + }) |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should configure vite plugin in astro:config:setup hook', () => { |
| 127 | + const plugin = starlightGiscus(validConfig); |
| 128 | + const updateConfig = vi.fn(); |
| 129 | + const addIntegration = vi.fn(); |
| 130 | + const logger = { |
| 131 | + warn: vi.fn(), |
| 132 | + info: vi.fn(), |
| 133 | + error: vi.fn(), |
| 134 | + debug: vi.fn(), |
| 135 | + label: 'test', |
| 136 | + fork: vi.fn(), |
| 137 | + } as any; |
| 138 | + |
| 139 | + const configSetup = plugin.hooks['config:setup']; |
| 140 | + configSetup!({ |
| 141 | + logger, |
| 142 | + config: { title: 'Test', components: {} } as any, |
| 143 | + updateConfig, |
| 144 | + addIntegration, |
| 145 | + command: 'dev', |
| 146 | + isRestart: false, |
| 147 | + } as any); |
| 148 | + |
| 149 | + // Get the integration that was added |
| 150 | + const integration = addIntegration.mock.calls[0][0]; |
| 151 | + const astroConfigSetup = integration.hooks['astro:config:setup']; |
| 152 | + |
| 153 | + // Call the astro:config:setup hook |
| 154 | + const astroUpdateConfig = vi.fn(); |
| 155 | + astroConfigSetup({ updateConfig: astroUpdateConfig } as any); |
| 156 | + |
| 157 | + // Verify it called updateConfig with vite plugins |
| 158 | + expect(astroUpdateConfig).toHaveBeenCalledWith( |
| 159 | + expect.objectContaining({ |
| 160 | + vite: expect.objectContaining({ |
| 161 | + plugins: expect.any(Array), |
| 162 | + }), |
| 163 | + }) |
| 164 | + ); |
| 165 | + }); |
| 166 | +}); |
0 commit comments