summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/graphql_spec.js
blob: a39ce2ffd99354273b3064b4a302c096b24810d3 (plain)
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
import getPipelineDetails from 'shared_queries/pipelines/get_pipeline_details.query.graphql';
import { stripWhitespaceFromQuery } from '~/lib/graphql';
import { queryToObject } from '~/lib/utils/url_utility';

describe('stripWhitespaceFromQuery', () => {
  const operationName = 'getPipelineDetails';
  const variables = `{
    projectPath: 'root/abcd-dag',
    iid: '44'
  }`;

  const testQuery = getPipelineDetails.loc.source.body;
  const defaultPath = '/api/graphql';
  const encodedVariables = encodeURIComponent(variables);

  it('shortens the query argument by replacing multiple spaces and newlines with a single space', () => {
    const testString = `${defaultPath}?query=${encodeURIComponent(testQuery)}`;
    expect(testString.length > stripWhitespaceFromQuery(testString, defaultPath).length).toBe(true);
  });

  it('does not contract a single space', () => {
    const simpleSingleString = `${defaultPath}?query=${encodeURIComponent('fragment Nonsense')}`;
    expect(stripWhitespaceFromQuery(simpleSingleString, defaultPath)).toEqual(simpleSingleString);
  });

  it('works with a non-default path', () => {
    const newPath = 'another/graphql/path';
    const newPathSingleString = `${newPath}?query=${encodeURIComponent('fragment Nonsense')}`;
    expect(stripWhitespaceFromQuery(newPathSingleString, newPath)).toEqual(newPathSingleString);
  });

  it('does not alter other arguments', () => {
    const bareParams = `?query=${encodeURIComponent(
      testQuery,
    )}&operationName=${operationName}&variables=${encodedVariables}`;
    const testLongString = `${defaultPath}${bareParams}`;

    const processed = stripWhitespaceFromQuery(testLongString, defaultPath);
    const decoded = decodeURIComponent(processed);
    const params = queryToObject(decoded);

    expect(params.operationName).toBe(operationName);
    expect(params.variables).toBe(variables);
  });

  it('works when there are no query params', () => {
    expect(stripWhitespaceFromQuery(defaultPath, defaultPath)).toEqual(defaultPath);
  });

  it('works when the params do not include a query', () => {
    const paramsWithoutQuery = `${defaultPath}&variables=${encodedVariables}`;
    expect(stripWhitespaceFromQuery(paramsWithoutQuery, defaultPath)).toEqual(paramsWithoutQuery);
  });
});