File size: 2,395 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { describe, expect, it } from 'vitest'
import { closeDestination, previousEntry, recordLocation, showsPageClose } from './pageClose'

const cur = (search = '', pathname = '/browse-topics/all') => ({ pathname, search })

describe('closeDestination', () => {
  it('goes home keeping the city scope and topic when there is no history', () => {
    expect(closeDestination(cur('?state=AL&city=Tuscaloosa&topic=fluoride&x=1'), null)).toEqual({
      kind: 'to',
      to: '/?state=AL&city=Tuscaloosa&topic=fluoride',
      label: 'home',
    })
  })
  it('keeps the national scope', () => {
    expect(closeDestination(cur('?scope=national'), null)).toMatchObject({ to: '/?scope=national' })
  })
  it('goes home bare when there is no scope', () => {
    expect(closeDestination(cur(''), null)).toMatchObject({ to: '/' })
  })
  it('goes back when the previous entry is ours and the scope matches', () => {
    expect(closeDestination(cur('?state=AL'), { pathname: '/search', search: '?state=AL&q=x' })).toEqual({
      kind: 'back',
      label: 'search results',
    })
  })
  it('goes back when this page carries no scope', () => {
    expect(closeDestination(cur(''), { pathname: '/', search: '?state=AL' }).kind).toBe('back')
  })
  it('applies the current scope when the place changed on this page', () => {
    expect(closeDestination(cur('?state=GA&city=Atlanta'), { pathname: '/search', search: '?state=AL&city=Tuscaloosa&q=x' })).toMatchObject({
      kind: 'to',
      to: '/search?q=x&state=GA&city=Atlanta',
    })
  })
  it('does not go back to the same page', () => {
    expect(closeDestination(cur('?state=AL'), { pathname: '/browse-topics/all', search: '' })).toMatchObject({ to: '/?state=AL' })
  })
})

describe('trail', () => {
  it('knows the entry before an index and nothing before the first', () => {
    recordLocation({ pathname: '/a', search: '?s=1' }, 4)
    expect(previousEntry(5)).toEqual({ pathname: '/a', search: '?s=1' })
    expect(previousEntry(0)).toBeNull()
    expect(previousEntry(9)).toBeNull()
  })
})

describe('showsPageClose', () => {
  it('skips stories and home', () => {
    expect(showsPageClose('/stories/data-centers')).toBe(false)
    expect(showsPageClose('/')).toBe(false)
    expect(showsPageClose('/settings')).toBe(false)
    expect(showsPageClose('/stories')).toBe(true)
    expect(showsPageClose('/decisions/12')).toBe(true)
  })
})