|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +// identity resolveSelectors so we can assert which versioned object flowed through; |
| 4 | +// tagged fallback data so we can tell the bundled dep apart from the fetched bundle |
| 5 | +vi.mock('@grafana/e2e-selectors', () => ({ |
| 6 | + resolveSelectors: vi.fn((versioned: unknown) => versioned), |
| 7 | + versionedComponents: { __source: 'dep-components' }, |
| 8 | + versionedPages: { __source: 'dep-pages' }, |
| 9 | +})); |
| 10 | +vi.mock('../selectors/versionedConstants', () => ({ versionedConstants: { __source: 'local-constants' } })); |
| 11 | +vi.mock('../selectors/versionedAPIs', () => ({ versionedAPIs: { __source: 'local-apis' } })); |
| 12 | + |
| 13 | +import { selectors } from './selectors'; |
| 14 | + |
| 15 | +const VALID_BUNDLE = ` |
| 16 | + exports.versionedComponents = { __source: 'fetched-components' }; |
| 17 | + exports.versionedPages = { __source: 'fetched-pages' }; |
| 18 | +`; |
| 19 | + |
| 20 | +// a fake APIRequestContext.get() result |
| 21 | +function mockResponse({ status = 200, body = '' }: { status?: number; body?: string }) { |
| 22 | + return { status: () => status, ok: () => status >= 200 && status < 300, text: async () => body }; |
| 23 | +} |
| 24 | + |
| 25 | +function mockRequest(get: ReturnType<typeof vi.fn>) { |
| 26 | + // only `get` is exercised by the fixture |
| 27 | + return { get } as never; |
| 28 | +} |
| 29 | + |
| 30 | +// drives the fixture and returns whatever it passes to `use` |
| 31 | +async function runFixture(args: { grafanaVersion: string; request: never }) { |
| 32 | + let captured: unknown; |
| 33 | + await (selectors as unknown as (args: unknown, use: (value: unknown) => Promise<void>) => Promise<void>)( |
| 34 | + args, |
| 35 | + async (value: unknown) => { |
| 36 | + captured = value; |
| 37 | + } |
| 38 | + ); |
| 39 | + return captured as Record<string, unknown>; |
| 40 | +} |
| 41 | + |
| 42 | +describe('selectors fixture', () => { |
| 43 | + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + warnSpy.mockClear(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + delete process.env.PLUGIN_E2E_RUNTIME_SELECTORS; |
| 51 | + }); |
| 52 | + |
| 53 | + it('flag off: uses the bundled dependency and never fetches', async () => { |
| 54 | + const get = vi.fn(); |
| 55 | + const result = await runFixture({ grafanaVersion: '11.0.0-off', request: mockRequest(get) }); |
| 56 | + |
| 57 | + expect(get).not.toHaveBeenCalled(); |
| 58 | + expect(result.components).toEqual({ __source: 'dep-components' }); |
| 59 | + expect(result.constants).toEqual({ __source: 'local-constants' }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('flag on + 200: uses fetched components/pages and local constants/apis', async () => { |
| 63 | + process.env.PLUGIN_E2E_RUNTIME_SELECTORS = 'true'; |
| 64 | + const get = vi.fn().mockResolvedValue(mockResponse({ status: 200, body: VALID_BUNDLE })); |
| 65 | + |
| 66 | + const result = await runFixture({ grafanaVersion: '11.0.0-200', request: mockRequest(get) }); |
| 67 | + |
| 68 | + expect(get).toHaveBeenCalledWith('/public/e2e-selectors.js'); |
| 69 | + expect(result.components).toEqual({ __source: 'fetched-components' }); |
| 70 | + expect(result.pages).toEqual({ __source: 'fetched-pages' }); |
| 71 | + expect(result.constants).toEqual({ __source: 'local-constants' }); |
| 72 | + expect(result.apis).toEqual({ __source: 'local-apis' }); |
| 73 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('flag on + 404: falls back to the dependency quietly', async () => { |
| 77 | + process.env.PLUGIN_E2E_RUNTIME_SELECTORS = 'true'; |
| 78 | + const get = vi.fn().mockResolvedValue(mockResponse({ status: 404 })); |
| 79 | + |
| 80 | + const result = await runFixture({ grafanaVersion: '11.0.0-404', request: mockRequest(get) }); |
| 81 | + |
| 82 | + expect(result.components).toEqual({ __source: 'dep-components' }); |
| 83 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('flag on + 5xx: falls back to the dependency with a warning', async () => { |
| 87 | + process.env.PLUGIN_E2E_RUNTIME_SELECTORS = 'true'; |
| 88 | + const get = vi.fn().mockResolvedValue(mockResponse({ status: 503 })); |
| 89 | + |
| 90 | + const result = await runFixture({ grafanaVersion: '11.0.0-503', request: mockRequest(get) }); |
| 91 | + |
| 92 | + expect(result.components).toEqual({ __source: 'dep-components' }); |
| 93 | + expect(warnSpy).toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('flag on + network error: falls back to the dependency with a warning', async () => { |
| 97 | + process.env.PLUGIN_E2E_RUNTIME_SELECTORS = 'true'; |
| 98 | + const get = vi.fn().mockRejectedValue(new Error('ECONNREFUSED')); |
| 99 | + |
| 100 | + const result = await runFixture({ grafanaVersion: '11.0.0-net', request: mockRequest(get) }); |
| 101 | + |
| 102 | + expect(result.components).toEqual({ __source: 'dep-components' }); |
| 103 | + expect(warnSpy).toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('flag on + malformed bundle (missing exports): falls back to the dependency with a warning', async () => { |
| 107 | + process.env.PLUGIN_E2E_RUNTIME_SELECTORS = 'true'; |
| 108 | + const get = vi.fn().mockResolvedValue(mockResponse({ status: 200, body: 'exports.nope = 1;' })); |
| 109 | + |
| 110 | + const result = await runFixture({ grafanaVersion: '11.0.0-bad', request: mockRequest(get) }); |
| 111 | + |
| 112 | + expect(result.components).toEqual({ __source: 'dep-components' }); |
| 113 | + expect(warnSpy).toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('flag on: concurrent fixtures for the same version share a single fetch', async () => { |
| 117 | + process.env.PLUGIN_E2E_RUNTIME_SELECTORS = 'true'; |
| 118 | + const get = vi.fn().mockResolvedValue(mockResponse({ status: 200, body: VALID_BUNDLE })); |
| 119 | + const request = mockRequest(get); |
| 120 | + |
| 121 | + const [a, b] = await Promise.all([ |
| 122 | + runFixture({ grafanaVersion: '11.0.0-cache', request }), |
| 123 | + runFixture({ grafanaVersion: '11.0.0-cache', request }), |
| 124 | + ]); |
| 125 | + |
| 126 | + expect(get).toHaveBeenCalledTimes(1); |
| 127 | + expect(a.components).toEqual({ __source: 'fetched-components' }); |
| 128 | + expect(b.components).toEqual({ __source: 'fetched-components' }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments