about summary refs log tree commit diff
path: root/__tests__/state/models/navigation.test.ts
blob: bc49d2ee3ce93d4ebbffa298b00f3680fddf9eab (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import {
  NavigationModel,
  NavigationTabModel,
} from './../../../src/state/models/navigation'
import * as flags from '../../../src/build-flags'

describe('NavigationModel', () => {
  let model: NavigationModel

  beforeEach(() => {
    model = new NavigationModel()
    model.setTitle([0, 0], 'title')
  })

  afterAll(() => {
    jest.clearAllMocks()
  })

  it('should clear() to the correct base state', async () => {
    await model.clear()
    expect(model.tabCount).toBe(2)
    expect(model.tab).toEqual({
      fixedTabPurpose: 0,
      history: [
        {
          id: expect.anything(),
          ts: expect.anything(),
          url: '/',
        },
      ],
      id: expect.anything(),
      index: 0,
      isNewTab: false,
    })
  })

  it('should call the navigate method', async () => {
    const navigateSpy = jest.spyOn(model.tab, 'navigate')
    await model.navigate('testurl', 'teststring')
    expect(navigateSpy).toHaveBeenCalledWith('testurl', 'teststring')
  })

  it('should call the refresh method', async () => {
    const refreshSpy = jest.spyOn(model.tab, 'refresh')
    await model.refresh()
    expect(refreshSpy).toHaveBeenCalled()
  })

  it('should call the isCurrentScreen method', () => {
    expect(model.isCurrentScreen(11, 0)).toEqual(false)
  })

  it('should call the tab getter', () => {
    expect(model.tab).toEqual({
      fixedTabPurpose: 0,
      history: [
        {
          id: expect.anything(),
          ts: expect.anything(),
          url: '/',
        },
      ],
      id: expect.anything(),
      index: 0,
      isNewTab: false,
    })
  })

  it('should call the tabCount getter', () => {
    expect(model.tabCount).toBe(2)
  })

  describe('tabs not enabled', () => {
    jest.mock('../../../src/build-flags', () => ({
      TABS_ENABLED: false,
    }))

    afterAll(() => {
      jest.clearAllMocks()
    })

    it('should not create new tabs', () => {
      // @ts-expect-error
      flags.TABS_ENABLED = false
      model.newTab('testurl')
      expect(model.tab.isNewTab).toBe(false)
      expect(model.tabIndex).toBe(0)
    })

    it('should not change the active tab', () => {
      // @ts-expect-error
      flags.TABS_ENABLED = false
      model.setActiveTab(2)
      expect(model.tabIndex).toBe(0)
    })

    it('should note close tabs', () => {
      // @ts-expect-error
      flags.TABS_ENABLED = false
      model.closeTab(0)
      expect(model.tabCount).toBe(2)
    })
  })

  describe('tabs enabled', () => {
    jest.mock('../../../src/build-flags', () => ({
      TABS_ENABLED: true,
    }))

    afterAll(() => {
      jest.clearAllMocks()
    })

    it('should create new tabs', () => {
      // @ts-expect-error
      flags.TABS_ENABLED = true

      model.newTab('testurl', 'title')
      expect(model.tab.isNewTab).toBe(true)
      expect(model.tabIndex).toBe(2)
    })

    it('should change the current tab', () => {
      // @ts-expect-error
      flags.TABS_ENABLED = true

      model.setActiveTab(0)
      expect(model.tabIndex).toBe(0)
    })

    it('should close tabs', () => {
      // @ts-expect-error
      flags.TABS_ENABLED = true

      model.closeTab(0)
      expect(model.tabs).toEqual([
        {
          fixedTabPurpose: 1,
          history: [
            {
              id: expect.anything(),
              ts: expect.anything(),
              url: '/notifications',
            },
          ],
          id: expect.anything(),
          index: 0,
          isNewTab: false,
        },
      ])
      expect(model.tabIndex).toBe(0)
    })
  })
})