diff --git a/superglue/lib/action_creators/requests.ts b/superglue/lib/action_creators/requests.ts index a40b087f..d6d93fc9 100644 --- a/superglue/lib/action_creators/requests.ts +++ b/superglue/lib/action_creators/requests.ts @@ -152,7 +152,7 @@ export function visit( placeholderKey, beforeSave = (prevPage, receivedPage) => receivedPage, revisit = false, - action = 'push', + action, } = {} ) { path = withoutBusters(path) @@ -210,11 +210,7 @@ export function visit( const isGet = fetchArgs[1].method === 'GET' - if (action !== undefined) { - meta.suggestedAction = action - } else { - meta.suggestedAction = 'push' - } + meta.suggestedAction = 'push' if (!rsp.redirected && !isGet) { meta.suggestedAction = 'replace' @@ -228,6 +224,10 @@ export function visit( } } + if (action) { + meta.suggestedAction = action + } + pageKey = urlToPageKey(rsp.url) if (!isGet && !rsp.redirected) { diff --git a/superglue/spec/lib/utils/ujs.spec.js b/superglue/spec/lib/utils/ujs.spec.js index a80f0830..e07a142d 100644 --- a/superglue/spec/lib/utils/ujs.spec.js +++ b/superglue/spec/lib/utils/ujs.spec.js @@ -79,6 +79,9 @@ describe('ujs', () => { if(attr === 'href') { return '/foo' } + if(attr === 'data-visit') { + return true + } if(attr === 'data-replace') { return true } @@ -169,6 +172,32 @@ describe('ujs', () => { expect(remote).toHaveBeenCalledWith('/foo', {method: 'GET'}) }) + it('calls visit with replace action if link has data-replace attribute', () => { + const ujsAttributePrefix = 'data' + const visit = jest.fn() + const navigatorRef = { + current: { + navigateTo: () => {}, + }, + } + const store = {} + + const builder = new HandlerBuilder({ + ujsAttributePrefix, + store, + visit, + navigatorRef, + }) + + const { onClick } = builder.handlers() + onClick(createFakeReplaceEvent()) + + expect(visit).toHaveBeenCalledWith('/foo', { + method: 'GET', + suggestedAction: 'replace', + }) + }) + it('does not call visit on a link that does not have the visit attribute data-visit', () => { const store = {} const ujsAttributePrefix = 'data' @@ -400,7 +429,59 @@ describe('ujs', () => { expect(visit).not.toHaveBeenCalledWith('/foo', { method: 'POST', - body: {some: 'Body'} + body: { some: 'Body' }, + }) + }) + + it('calls visit with replace action if form has data-replace attribute', () => { + const store = {} + const ujsAttributePrefix = 'data' + const visit = jest.fn() + const navigatorRef = { + current: { + navigateTo: () => {}, + }, + } + + const builder = new HandlerBuilder({ + ujsAttributePrefix, + store, + visit, + navigatorRef, + }) + global.FormData = () => {} + jest + .spyOn(global, 'FormData') + .mockImplementation(() => ({ some: 'Body' })) + + const { onSubmit } = builder.handlers() + const fakeFormEvent = createFakeFormEvent() + fakeFormEvent.target.getAttribute = (attr) => { + if (attr === 'action') { + return '/foo' + } + if (attr === 'method') { + return 'POST' + } + if (attr === 'data-visit') { + return true + } + if (attr === 'data-replace') { + return true + } + } + onSubmit(fakeFormEvent) + + expect(global.FormData).toHaveBeenCalledWith( + fakeFormEvent.target + ) + expect(visit).toHaveBeenCalledWith('/foo', { + method: 'POST', + headers: { + 'content-type': null, + }, + suggestedAction: 'replace', + body: { some: 'Body' }, }) }) })