Skip to content

Commit

Permalink
comments fixes & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kholdrex committed Jun 25, 2024
1 parent c379297 commit 2bb8ce0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
12 changes: 6 additions & 6 deletions superglue/lib/action_creators/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export function visit(
placeholderKey,
beforeSave = (prevPage, receivedPage) => receivedPage,
revisit = false,
action = 'push',
action,
} = {}
) {
path = withoutBusters(path)
Expand Down Expand Up @@ -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'
Expand All @@ -228,6 +224,10 @@ export function visit(
}
}

if (action) {
meta.suggestedAction = action
}

pageKey = urlToPageKey(rsp.url)

if (!isGet && !rsp.redirected) {
Expand Down
83 changes: 82 additions & 1 deletion superglue/spec/lib/utils/ujs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ describe('ujs', () => {
if(attr === 'href') {
return '/foo'
}
if(attr === 'data-visit') {
return true
}
if(attr === 'data-replace') {
return true
}
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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' },
})
})
})
Expand Down

0 comments on commit 2bb8ce0

Please sign in to comment.