Skip to content

Commit

Permalink
fix: remove doubleslash for new map download
Browse files Browse the repository at this point in the history
  • Loading branch information
jenniferarnesen committed Jan 11, 2024
1 parent 0df3cf8 commit d23c1f9
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 7 deletions.
80 changes: 80 additions & 0 deletions src/util/__tests__/history.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import queryString from 'query-string'
import { getHashUrlParams } from '../history.js'

// jest.mock('query-string')
jest.mock('query-string', () => ({
parse: jest.fn(() => {}),
}))

describe('getHashUrlParams', () => {
it('should return isDownload=true when pathname is /download', () => {
queryString.parse.mockImplementationOnce(() => ({}))
const loc = { search: '', pathname: '/download' }
expect(getHashUrlParams(loc)).toEqual({ isDownload: true, mapId: '' })
})

it('should return mapId="currentAnalyticalObject" when pathname is /currentAnalyticalObject', () => {
queryString.parse.mockImplementationOnce(() => ({}))
const loc = { search: '', pathname: '/currentAnalyticalObject' }
expect(getHashUrlParams(loc)).toEqual({
mapId: 'currentAnalyticalObject',
})
})

it('should return param and mapId when search is ?param=true and pathname is /xyzpdq', () => {
queryString.parse.mockImplementationOnce(() => ({ param: true }))
const loc = { search: '?param=true', pathname: '/xyzpdq' }
expect(getHashUrlParams(loc)).toEqual({ param: true, mapId: 'xyzpdq' })
})

it('should return param, mapId, and isDownload when search is ?param=false and pathname is /xyzpdq/download', () => {
queryString.parse.mockImplementationOnce(() => ({ param: false }))
const loc = { search: '?param=false', pathname: '/xyzpdq/download' }
expect(getHashUrlParams(loc)).toEqual({
param: false,
mapId: 'xyzpdq',
isDownload: true,
})
})

it('should return interpretationId and isDownload when search is ?interpretationId=xyzpdq and pathname is /download', () => {
queryString.parse.mockImplementationOnce(() => ({
interpretationId: 'xyzpdq',
}))
const loc = {
search: '?interpretationId=xyzpdq',
pathname: '/download',
}
expect(getHashUrlParams(loc)).toEqual({
interpretationId: 'xyzpdq',
isDownload: true,
mapId: '',
})
})

it('should return interpretationId and mapId when search is ?interpretationId=xyzpdq and pathname is /xyzpdq', () => {
queryString.parse.mockImplementationOnce(() => ({
interpretationId: 'xyzpdq',
}))
const loc = { search: '?interpretationId=xyzpdq', pathname: '/xyzpdq' }
expect(getHashUrlParams(loc)).toEqual({
interpretationId: 'xyzpdq',
mapId: 'xyzpdq',
})
})

it('should return interpretationId, mapId, and isDownload when search is ?interpretationId=xyzpdq and pathname is /xyzpdq/download', () => {
queryString.parse.mockImplementationOnce(() => ({
interpretationId: 'xyzpdq',
}))
const loc = {
search: '?interpretationId=xyzpdq',
pathname: '/xyzpdq/download',
}
expect(getHashUrlParams(loc)).toEqual({
interpretationId: 'xyzpdq',
mapId: 'xyzpdq',
isDownload: true,
})
})
})
28 changes: 21 additions & 7 deletions src/util/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,36 @@ const getHashUrlParams = (loc) => {

const pathParts = loc.pathname.slice(1).split('/')
if (pathParts.length > 0) {
params.mapId = pathParts[0]

if (pathParts[1] === DOWNLOAD) {
if (pathParts[0] === DOWNLOAD) {
params.mapId = ''
params.isDownload = true
} else {
params.mapId = pathParts[0]

if (pathParts[1] === DOWNLOAD) {
params.isDownload = true
}
}
}

return params
}

const openDownloadMode = () =>
history.push(`${history.location.pathname}/${DOWNLOAD}`)
const openDownloadMode = () => {
if (history.location.pathname === '/') {
history.push(`/${DOWNLOAD}`)
} else {
history.push(`${history.location.pathname}/${DOWNLOAD}`)
}
}

const closeDownloadMode = () => {
const rootPath = history.location.pathname.split(`/${DOWNLOAD}`)[0]
history.push(rootPath)
if (history.location.pathname === `/${DOWNLOAD}`) {
history.push('/')
} else {
const rootPath = history.location.pathname.split(`/${DOWNLOAD}`)[0]
history.push(rootPath)
}
}

export {
Expand Down

0 comments on commit d23c1f9

Please sign in to comment.