From c293ceefcf213c17731719fe27d7848f7eb5ae71 Mon Sep 17 00:00:00 2001 From: devendra-bruno Date: Mon, 26 May 2025 16:37:28 +0530 Subject: [PATCH] Refactored fetch-gql-schema-handler.spec.js --- .../network/fetch-gql-schema-handler.spec.js | 241 +++--------------- 1 file changed, 30 insertions(+), 211 deletions(-) diff --git a/packages/bruno-electron/tests/network/fetch-gql-schema-handler.spec.js b/packages/bruno-electron/tests/network/fetch-gql-schema-handler.spec.js index 064f78daf..3104807e8 100644 --- a/packages/bruno-electron/tests/network/fetch-gql-schema-handler.spec.js +++ b/packages/bruno-electron/tests/network/fetch-gql-schema-handler.spec.js @@ -1,39 +1,45 @@ -const prepareGqlIntrospectionRequest = require('../../src/ipc/network/prepare-gql-introspection-request'); -const { fetchGqlSchemaHandler } = require('../../src/ipc/network'); -const { getTreePathFromCollectionToItem } = require('../../src/utils/collection'); - -// Mock the module -jest.mock('../../src/ipc/network/prepare-gql-introspection-request', () => { - return jest.fn().mockReturnValue({ - method: 'POST', - url: 'https://example.com/', - headers: {}, - data: '{}' - }); -}); - -// Mock the collection utils +// Mock the modules first, before requiring them jest.mock('../../src/utils/collection', () => { const original = jest.requireActual('../../src/utils/collection'); return { ...original, - getTreePathFromCollectionToItem: jest.fn(), - mergeVars: jest.fn((collection, request, treePath) => { - // Simulate the behavior of mergeVars by keeping folderVariables if they exist - // This is a simplified mock that just ensures that folder variables are preserved - if (request.folderVariables) { - // We don't need to modify the request, just ensure folderVariables remain - } + getTreePathFromCollectionToItem: jest.fn().mockReturnValue([]), + mergeVars: jest.fn(), + getEnvVars: jest.fn(env => { + if (!env || !env.variables) return {}; + return env.variables.reduce((acc, variable) => { + if (variable.enabled) { + acc[variable.name] = variable.value; + } + return acc; + }, {}); }) }; }); -describe('Prepare GQL Introspection Request', () => { +jest.mock('../../src/ipc/network/prepare-gql-introspection-request', () => { + return jest.fn().mockImplementation((endpoint, vars, request, root) => { + return { + url: endpoint, + method: 'POST', + headers: request?.headers || {}, + data: { + query: '{ __schema { types { name } } }' + } + }; + }); +}); + +const prepareGqlIntrospectionRequest = require('../../src/ipc/network/prepare-gql-introspection-request'); +const { fetchGqlSchemaHandler } = require('../../src/ipc/network'); +const { getTreePathFromCollectionToItem } = require('../../src/utils/collection'); + +describe('fetchGqlSchemaHandler', () => { beforeEach(() => { jest.clearAllMocks(); }); - it('should receive combined variables from fetchGqlSchema', async () => { + it('should receive combined variables from fetchGqlSchemaHandler', async () => { const endpoint = 'https://example.com/'; const environment = { variables: [ @@ -289,26 +295,6 @@ describe('Prepare GQL Introspection Request', () => { } }; - // Make sure our mock properly returns the folder variables - prepareGqlIntrospectionRequest.mockImplementationOnce((endpoint, resolvedVars, req, root) => { - // In a real scenario, the resolvedVars would include the folder variables - // Simulate the correct merge of variables - const combinedVars = { - ...resolvedVars, - SHARED_VAR: 'folder-value' // This simulates the correct precedence - }; - - return { - method: 'POST', - url: endpoint, - headers: {}, - data: JSON.stringify(combinedVars) - }; - }); - - // Set up empty tree path - getTreePathFromCollectionToItem.mockReturnValue([]); - await fetchGqlSchemaHandler(null, endpoint, environment, request, collection); expect(prepareGqlIntrospectionRequest).toHaveBeenCalledWith( @@ -377,173 +363,6 @@ describe('Prepare GQL Introspection Request', () => { collection.root ); }); - - it('should properly respect the complete variable precedence hierarchy', async () => { - const endpoint = 'https://example.com/'; - const environment = { - variables: [ - { name: 'ENV_VAR', value: 'env-value', enabled: true }, - { name: 'SHARED_VAR', value: 'env-value', enabled: true } - ] - }; - const request = { - vars: { - REQUEST_VAR: 'request-value', - SHARED_VAR: 'request-value' - }, - folderVariables: { - FOLDER_VAR: 'folder-value', - SHARED_VAR: 'folder-value' - } - }; - const collection = { - uid: 'test-collection', - pathname: '/test', - runtimeVariables: { - RUNTIME_VAR: 'runtime-value', - SHARED_VAR: 'runtime-value' - }, - globalEnvironmentVariables: { - GLOBAL_VAR: 'global-value', - SHARED_VAR: 'global-value' - }, - root: { - request: { - headers: [] - } - } - }; - - // Make sure our mock returns the variables with correct precedence - prepareGqlIntrospectionRequest.mockImplementationOnce((endpoint, resolvedVars, req, root) => { - // Manually apply the correct precedence for this test - const correctVars = { - GLOBAL_VAR: 'global-value', - ENV_VAR: 'env-value', - RUNTIME_VAR: 'runtime-value', - FOLDER_VAR: 'folder-value', - REQUEST_VAR: 'request-value', - SHARED_VAR: 'request-value' // Highest precedence wins - }; - - return { - method: 'POST', - url: endpoint, - headers: {}, - data: JSON.stringify(correctVars) - }; - }); - - // Set up empty tree path - getTreePathFromCollectionToItem.mockReturnValue([]); - - await fetchGqlSchemaHandler(null, endpoint, environment, request, collection); - - expect(prepareGqlIntrospectionRequest).toHaveBeenCalledWith( - endpoint, - expect.objectContaining({ - GLOBAL_VAR: 'global-value', - ENV_VAR: 'env-value', - RUNTIME_VAR: 'runtime-value', - FOLDER_VAR: 'folder-value', - REQUEST_VAR: 'request-value', - SHARED_VAR: 'request-value' // Shows highest precedence wins - }), - request, - collection.root - ); - }); }); -describe('GraphQL Schema Handler Header Tests', () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - const createBasicSetup = () => ({ - endpoint: 'https://example.com/', - environment: { variables: [] }, - request: { vars: {}, headers: [] }, - collection: { - uid: 'test-collection', - pathname: '/test', - runtimeVariables: {}, - globalEnvironmentVariables: {}, - root: { - request: { - headers: [] - } - } - } - }); - - it('should pass root headers to request', async () => { - const setup = createBasicSetup(); - setup.collection.root.request.headers = [ - { name: 'X-Root-Header', value: 'root-value', enabled: true } - ]; - - await fetchGqlSchemaHandler(null, setup.endpoint, setup.environment, setup.request, setup.collection); - - expect(prepareGqlIntrospectionRequest).toHaveBeenCalledWith( - setup.endpoint, - expect.any(Object), - setup.request, - setup.collection.root - ); - }); - - it('should pass request headers to request', async () => { - const setup = createBasicSetup(); - setup.request.headers = [ - { name: 'X-Request-Header', value: 'request-value', enabled: true } - ]; - - await fetchGqlSchemaHandler(null, setup.endpoint, setup.environment, setup.request, setup.collection); - - expect(prepareGqlIntrospectionRequest).toHaveBeenCalledWith( - setup.endpoint, - expect.any(Object), - setup.request, - setup.collection.root - ); - }); - - it('should handle environment variables in headers', async () => { - const setup = createBasicSetup(); - setup.environment.variables = [ - { name: 'AUTH_TOKEN', value: 'token-value', enabled: true } - ]; - setup.request.headers = [ - { name: 'Authorization', value: 'Bearer {{AUTH_TOKEN}}', enabled: true } - ]; - - await fetchGqlSchemaHandler(null, setup.endpoint, setup.environment, setup.request, setup.collection); - - expect(prepareGqlIntrospectionRequest).toHaveBeenCalledWith( - setup.endpoint, - expect.objectContaining({ - AUTH_TOKEN: 'token-value' - }), - setup.request, - setup.collection.root - ); - }); - - it('should handle enabled and disabled headers', async () => { - const setup = createBasicSetup(); - setup.request.headers = [ - { name: 'X-Enabled', value: 'enabled', enabled: true }, - { name: 'X-Disabled', value: 'disabled', enabled: false } - ]; - - await fetchGqlSchemaHandler(null, setup.endpoint, setup.environment, setup.request, setup.collection); - - expect(prepareGqlIntrospectionRequest).toHaveBeenCalledWith( - setup.endpoint, - expect.any(Object), - setup.request, - setup.collection.root - ); - }); -});