Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/lib/graphql_spec.js')
-rw-r--r--spec/frontend/lib/graphql_spec.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/spec/frontend/lib/graphql_spec.js b/spec/frontend/lib/graphql_spec.js
new file mode 100644
index 00000000000..a39ce2ffd99
--- /dev/null
+++ b/spec/frontend/lib/graphql_spec.js
@@ -0,0 +1,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);
+ });
+});