UNPKG

altair-graphql-core

Version:

Several of the core logic for altair graphql client

103 lines 5.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const context_1 = require("./context"); const types_1 = require("./types"); (0, vitest_1.describe)('Script Context', () => { let mockHandlers; let mockData; (0, vitest_1.beforeEach)(() => { mockHandlers = { setCookie: vitest_1.vi.fn(), request: vitest_1.vi.fn(), getStorageItem: vitest_1.vi.fn(), setStorageItem: vitest_1.vi.fn(), }; mockData = { headers: [], variables: '{}', operationName: '', query: '', url: '', environment: { baseUrl: 'https://api.example.com', user: { name: 'John Doe', profile: { email: 'john@example.com', settings: { theme: 'dark', }, }, }, apiKey: 'secret-key', }, requestScriptLogs: [], requestType: types_1.RequestType.QUERY, }; }); (0, vitest_1.describe)('getEnvironment helper', () => { (0, vitest_1.it)('should get top-level environment variables', () => { const context = (0, context_1.getGlobalContext)(mockData, mockHandlers); (0, vitest_1.expect)(context.helpers.getEnvironment('baseUrl')).toBe('https://api.example.com'); (0, vitest_1.expect)(context.helpers.getEnvironment('apiKey')).toBe('secret-key'); }); (0, vitest_1.it)('should get nested environment variables using dot notation', () => { const context = (0, context_1.getGlobalContext)(mockData, mockHandlers); (0, vitest_1.expect)(context.helpers.getEnvironment('user.name')).toBe('John Doe'); (0, vitest_1.expect)(context.helpers.getEnvironment('user.profile.email')).toBe('john@example.com'); (0, vitest_1.expect)(context.helpers.getEnvironment('user.profile.settings.theme')).toBe('dark'); }); (0, vitest_1.it)('should return undefined for non-existent paths', () => { const context = (0, context_1.getGlobalContext)(mockData, mockHandlers); (0, vitest_1.expect)(context.helpers.getEnvironment('nonexistent')).toBeUndefined(); (0, vitest_1.expect)(context.helpers.getEnvironment('user.nonexistent')).toBeUndefined(); (0, vitest_1.expect)(context.helpers.getEnvironment('user.profile.nonexistent')).toBeUndefined(); }); (0, vitest_1.it)('should handle empty or invalid paths gracefully', () => { const context = (0, context_1.getGlobalContext)(mockData, mockHandlers); (0, vitest_1.expect)(context.helpers.getEnvironment('')).toBe(mockData.environment); (0, vitest_1.expect)(context.helpers.getEnvironment('user.')).toBeUndefined(); (0, vitest_1.expect)(context.helpers.getEnvironment('.user')).toBeUndefined(); }); }); (0, vitest_1.describe)('setEnvironment helper', () => { (0, vitest_1.it)('should set top-level environment variables', () => { const context = (0, context_1.getGlobalContext)(mockData, mockHandlers); context.helpers.setEnvironment('newVar', 'newValue'); (0, vitest_1.expect)(context.helpers.getEnvironment('newVar')).toBe('newValue'); }); (0, vitest_1.it)('should set nested environment variables using dot notation', () => { const context = (0, context_1.getGlobalContext)(mockData, mockHandlers); context.helpers.setEnvironment('user.age', 30); (0, vitest_1.expect)(context.helpers.getEnvironment('user.age')).toBe(30); context.helpers.setEnvironment('user.profile.settings.language', 'en'); (0, vitest_1.expect)(context.helpers.getEnvironment('user.profile.settings.language')).toBe('en'); }); (0, vitest_1.it)('should create nested structure when setting deep paths', () => { const context = (0, context_1.getGlobalContext)(mockData, mockHandlers); context.helpers.setEnvironment('new.nested.path', 'value'); (0, vitest_1.expect)(context.helpers.getEnvironment('new.nested.path')).toBe('value'); (0, vitest_1.expect)(context.helpers.getEnvironment('new.nested')).toEqual({ path: 'value', }); }); (0, vitest_1.it)('should set activeEnvironment when activeEnvironment flag is true', () => { const context = (0, context_1.getGlobalContext)(mockData, mockHandlers); context.helpers.setEnvironment('testVar', 'testValue', true); (0, vitest_1.expect)(context.data.__toSetActiveEnvironment).toEqual({ testVar: 'testValue', }); context.helpers.setEnvironment('nested.var', 'nestedValue', true); (0, vitest_1.expect)(context.data.__toSetActiveEnvironment?.nested).toEqual({ var: 'nestedValue', }); }); (0, vitest_1.it)('should not set activeEnvironment when activeEnvironment flag is false', () => { const context = (0, context_1.getGlobalContext)(mockData, mockHandlers); context.helpers.setEnvironment('testVar', 'testValue', false); (0, vitest_1.expect)(context.data.__toSetActiveEnvironment).toBeUndefined(); }); }); }); //# sourceMappingURL=context.spec.js.map