All files gqlTest.js

41.51% Statements 22/53
14.81% Branches 4/27
85.71% Functions 6/7
42.31% Lines 22/52
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91    1x   1x 1x 1x 1x 1x 1x 1x 1x                                                                                                 1x 1x 1x               1x 1x 1x 1x 1x 1x     1x 1x         1x   1x        
import createApolloClient from './createApolloClient';
 
const { describe, test, expect } = global;
 
const runGQLTest = async (testToRun) => {
  describe('GraphQL Tests', async () => {
    let data = {};
    const executeGQLCommand = async (executionPlan) => {
      const apolloClient = createApolloClient(data);
      const testInfo = executionPlan.pop();
      Eif (!testInfo.gql) {
        throw Error('No GQL operation provided to the runGQLTest.');
      }
      if (testInfo.gql.definitions.length !== 1) {
        throw Error('Only one GQL operation is allowed in a runGQLTest.');
      }
      const definition = testInfo.gql.definitions[0];
      let result = null;
      switch (definition.operation) {
        case 'mutation': {
          const params = { mutation: testInfo.gql };
          if (testInfo.vars) {
            params.variables = await testInfo.vars(data);
          }
          result = await apolloClient.mutate(params);
 
          if (testInfo.result) {
            data = { ...data, ...(await testInfo.result(result.data)) };
          }
          if (testInfo.test) {
            expect(testInfo.test(data)).toBe(true);
          } else {
            throw Error('On test defined for the runGQLTest.');
          }
          break;
        }
        case 'query': {
          const params = { query: testInfo.gql };
          if (testInfo.vars) {
            params.variables = await testInfo.vars(data);
          }
          result = await apolloClient.query(params);
 
          if (testInfo.result) {
            data = { ...data, ...(await testInfo.result(result.data)) };
          }
          if (testInfo.test) {
            expect(testInfo.test(data)).toBe(true);
          } else {
            throw Error('On test defined for the runGQLTest.');
          }
          break;
        }
        default:
          throw Error(`Unknown GQL operation ${definition.operation}.`);
      }
 
      if (executionPlan.length) await executeGQLCommand(executionPlan);
    };
 
    const getExecutionPlan = (testInfo, executionPlan) => {
      executionPlan.push(testInfo);
      Iif (testInfo.previous) {
        const prev = [...testInfo.previous];
        while (prev.length) {
          getExecutionPlan(prev.pop(), executionPlan);
        }
      }
    };
 
    const executeTest = async (testInfo, num) => {
      const executionPlan = [];
      getExecutionPlan(testInfo, executionPlan);
      test(`${testInfo.name}${num !== undefined ? ` ${num}` : ''}`, async () => {
        expect.assertions(executionPlan.length);
        await executeGQLCommand(executionPlan);
      });
    };
    data.endPoint = testToRun.endPoint;
    Iif (testToRun.repeat) {
      await Promise.all(
        new Array(testToRun.repeat).fill(0).map((_, i) => executeTest(testToRun, i + 1)),
      );
    } else {
      await executeTest(testToRun);
    }
    return data;
  });
};
export default runGQLTest;