Skip to content

Commit

Permalink
fix: #61 failing requests for queries with multiple parameters (#63)
Browse files Browse the repository at this point in the history
* fix: failing requests for queries with multiple parameters

* fix: add unit test to verify request with two params for get request to runPersistedQuery

* chore: remove comment

* chore: remove semicolon to please linter

* fix: fix unit test and implementation

* fix: extend tests to cover use case with no variables and add check for variableString being empty
  • Loading branch information
colinscz authored Jul 10, 2024
1 parent 45dad1a commit 8b52715
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class AEMHeadless {
return `;${key}=${val}`
}).join(''))

if (method === 'GET' && variablesString) {
variablesString += ';'
}

if (method === 'POST') {
body = JSON.stringify({ variables })
variablesString = ''
Expand Down
22 changes: 21 additions & 1 deletion test/shared/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,27 @@ test('API: runPersistedQuery with variables API Success', () => {
test('API: runPersistedQuery GET with variables API Success', () => {
// check success response
const promise = sdk.runPersistedQuery(persistedName, { name: 'test' })
expect(fetch).toHaveBeenCalledWith(`http://localhost/graphql/execute.json/${persistedName}%3Bname%3Dtest`, expect.anything(), expect.anything())
expect(fetch).toHaveBeenCalledWith(`http://localhost/graphql/execute.json/${persistedName}%3Bname%3Dtest;`, expect.anything(), expect.anything())
return expect(promise).resolves.toBeTruthy()
})

test('API: runPersistedQuery GET with two variables API Success', () => {
// check success response
const promise = sdk.runPersistedQuery(persistedName, { name: 'test', locale: 'en' })
expect(fetch).toHaveBeenCalledWith(`http://localhost/graphql/execute.json/${persistedName}%3Bname%3Dtest%3Blocale%3Den;`, expect.anything(), expect.anything())
return expect(promise).resolves.toBeTruthy()
})

test('API: runPersistedQuery GET with no variables API Success', () => {
// check success response
const promise = sdk.runPersistedQuery(persistedName)
expect(fetch).toHaveBeenCalledWith(`http://localhost/graphql/execute.json/${persistedName}`, expect.anything(), expect.anything())
return expect(promise).resolves.toBeTruthy()
})

test('API: runPersistedQuery GET with no variables API Success and empty variables', () => {
const promise = sdk.runPersistedQuery(persistedName, {})
expect(fetch).toHaveBeenCalledWith(`http://localhost/graphql/execute.json/${persistedName}`, expect.anything(), expect.anything())
return expect(promise).resolves.toBeTruthy()
})

Expand Down

0 comments on commit 8b52715

Please sign in to comment.