diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-07-20 09:55:51 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-07-20 09:55:51 +0000 |
commit | e8d2c2579383897a1dd7f9debd359abe8ae8373d (patch) | |
tree | c42be41678c2586d49a75cabce89322082698334 /spec/frontend/lib/graphql_spec.js | |
parent | fc845b37ec3a90aaa719975f607740c22ba6a113 (diff) | |
download | gitlab-ce-14.1.0-rc42.tar.gz |
Add latest changes from gitlab-org/gitlab@14-1-stable-eev14.1.0-rc42
Diffstat (limited to 'spec/frontend/lib/graphql_spec.js')
-rw-r--r-- | spec/frontend/lib/graphql_spec.js | 54 |
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); + }); +}); |