about summary refs log tree commit diff
path: root/__tests__/accounts.test.tsx
blob: f3ecb6af437602de7d548796dc371abde2c83f05 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import React from 'react'
import {MobileShell} from '../src/view/shell/mobile'
import {cleanup, fireEvent, render, waitFor} from '../jest/test-utils'
import {createServer, TestPDS} from '../jest/test-pds'
import {RootStoreModel, setupState} from '../src/state'

const WAIT_OPTS = {timeout: 5e3}

describe('Account flows', () => {
  let pds: TestPDS | undefined
  let rootStore: RootStoreModel | undefined
  beforeAll(async () => {
    jest.useFakeTimers()
    pds = await createServer()
    rootStore = await setupState(pds.pdsUrl)
  })

  afterAll(async () => {
    jest.clearAllMocks()
    cleanup()
    await pds?.close()
  })

  it('renders initial screen', () => {
    const {getByTestId} = render(<MobileShell />, rootStore)
    const signUpScreen = getByTestId('signinOrCreateAccount')

    expect(signUpScreen).toBeTruthy()
  })

  it('completes signin to the server', async () => {
    const {getByTestId} = render(<MobileShell />, rootStore)

    // move to signin view
    fireEvent.press(getByTestId('signInButton'))
    expect(getByTestId('signIn')).toBeTruthy()
    expect(getByTestId('loginForm')).toBeTruthy()

    // input the target server
    expect(getByTestId('loginSelectServiceButton')).toBeTruthy()
    fireEvent.press(getByTestId('loginSelectServiceButton'))
    expect(getByTestId('serverInputModal')).toBeTruthy()
    fireEvent.changeText(
      getByTestId('customServerTextInput'),
      pds?.pdsUrl || '',
    )
    fireEvent.press(getByTestId('customServerSelectBtn'))
    await waitFor(() => {
      expect(getByTestId('loginUsernameInput')).toBeTruthy()
    }, WAIT_OPTS)

    // enter username & pass
    fireEvent.changeText(getByTestId('loginUsernameInput'), 'alice')
    fireEvent.changeText(getByTestId('loginPasswordInput'), 'hunter2')
    await waitFor(() => {
      expect(getByTestId('loginNextButton')).toBeTruthy()
    }, WAIT_OPTS)
    fireEvent.press(getByTestId('loginNextButton'))

    // signed in
    await waitFor(() => {
      expect(getByTestId('homeFeed')).toBeTruthy()
      expect(rootStore?.me?.displayName).toBe('Alice')
      expect(rootStore?.me?.handle).toBe('alice.test')
      expect(rootStore?.session.accounts.length).toBe(1)
    }, WAIT_OPTS)
    expect(rootStore?.me?.displayName).toBe('Alice')
    expect(rootStore?.me?.handle).toBe('alice.test')
    expect(rootStore?.session.accounts.length).toBe(1)
  })

  it('opens the login screen when "add account" is pressed', async () => {
    const {getByTestId, getAllByTestId} = render(<MobileShell />, rootStore)
    await waitFor(() => expect(getByTestId('homeFeed')).toBeTruthy(), WAIT_OPTS)

    // open side menu
    fireEvent.press(getAllByTestId('viewHeaderBackOrMenuBtn')[0])
    await waitFor(() => expect(getByTestId('menuView')).toBeTruthy(), WAIT_OPTS)

    // nav to settings
    fireEvent.press(getByTestId('menuItemButton-Settings'))
    await waitFor(
      () => expect(getByTestId('settingsScreen')).toBeTruthy(),
      WAIT_OPTS,
    )

    // press '+ new account' in switcher
    fireEvent.press(getByTestId('switchToNewAccountBtn'))
    await waitFor(
      () => expect(getByTestId('signinOrCreateAccount')).toBeTruthy(),
      WAIT_OPTS,
    )
  })

  it('shows the "choose account" form when a previous session has been created', async () => {
    const {getByTestId} = render(<MobileShell />, rootStore)

    // move to signin view
    fireEvent.press(getByTestId('signInButton'))
    expect(getByTestId('signIn')).toBeTruthy()
    expect(getByTestId('chooseAccountForm')).toBeTruthy()
  })

  it('logs directly into the account due to still possessing session tokens', async () => {
    const {getByTestId} = render(<MobileShell />, rootStore)

    // move to signin view
    fireEvent.press(getByTestId('signInButton'))
    expect(getByTestId('signIn')).toBeTruthy()
    expect(getByTestId('chooseAccountForm')).toBeTruthy()

    // select the previous account
    fireEvent.press(getByTestId('chooseAccountBtn-alice.test'))

    // signs in immediately
    await waitFor(() => {
      expect(getByTestId('homeFeed')).toBeTruthy()
      expect(rootStore?.me?.displayName).toBe('Alice')
      expect(rootStore?.me?.handle).toBe('alice.test')
      expect(rootStore?.session.accounts.length).toBe(1)
    }, WAIT_OPTS)
    expect(rootStore?.me?.displayName).toBe('Alice')
    expect(rootStore?.me?.handle).toBe('alice.test')
    expect(rootStore?.session.accounts.length).toBe(1)
  })

  it('logs into a second account via the switcher', async () => {
    const {getByTestId, getAllByTestId} = render(<MobileShell />, rootStore)
    await waitFor(() => expect(getByTestId('homeFeed')).toBeTruthy(), WAIT_OPTS)

    // open side menu
    fireEvent.press(getAllByTestId('viewHeaderBackOrMenuBtn')[0])
    await waitFor(() => expect(getByTestId('menuView')).toBeTruthy(), WAIT_OPTS)

    // nav to settings
    fireEvent.press(getByTestId('menuItemButton-Settings'))
    await waitFor(
      () => expect(getByTestId('settingsScreen')).toBeTruthy(),
      WAIT_OPTS,
    )

    // press '+ new account' in switcher
    fireEvent.press(getByTestId('switchToNewAccountBtn'))
    await waitFor(
      () => expect(getByTestId('signinOrCreateAccount')).toBeTruthy(),
      WAIT_OPTS,
    )

    // move to signin view
    fireEvent.press(getByTestId('signInButton'))
    expect(getByTestId('signIn')).toBeTruthy()
    expect(getByTestId('chooseAccountForm')).toBeTruthy()

    // select a new account
    fireEvent.press(getByTestId('chooseNewAccountBtn'))
    expect(getByTestId('loginForm')).toBeTruthy()

    // input the target server
    expect(getByTestId('loginSelectServiceButton')).toBeTruthy()
    fireEvent.press(getByTestId('loginSelectServiceButton'))
    expect(getByTestId('serverInputModal')).toBeTruthy()
    fireEvent.changeText(
      getByTestId('customServerTextInput'),
      pds?.pdsUrl || '',
    )
    fireEvent.press(getByTestId('customServerSelectBtn'))
    await waitFor(
      () => expect(getByTestId('loginUsernameInput')).toBeTruthy(),
      WAIT_OPTS,
    )

    // enter username & pass
    fireEvent.changeText(getByTestId('loginUsernameInput'), 'bob')
    fireEvent.changeText(getByTestId('loginPasswordInput'), 'hunter2')
    await waitFor(
      () => expect(getByTestId('loginNextButton')).toBeTruthy(),
      WAIT_OPTS,
    )
    fireEvent.press(getByTestId('loginNextButton'))

    // signed in
    await waitFor(() => {
      expect(getByTestId('settingsScreen')).toBeTruthy() // we go back to settings in this situation
      expect(rootStore?.me?.displayName).toBe('Bob')
      expect(rootStore?.me?.handle).toBe('bob.test')
      expect(rootStore?.session.accounts.length).toBe(2)
    }, WAIT_OPTS)
    expect(rootStore?.me?.displayName).toBe('Bob')
    expect(rootStore?.me?.handle).toBe('bob.test')
    expect(rootStore?.session.accounts.length).toBe(2)
  })

  it('can instantly switch between accounts', async () => {
    const {getByTestId} = render(<MobileShell />, rootStore)
    await waitFor(
      () => expect(getByTestId('settingsScreen')).toBeTruthy(),
      WAIT_OPTS,
    )

    // select the alice account
    fireEvent.press(getByTestId('switchToAccountBtn-alice.test'))

    // swapped account
    await waitFor(() => {
      expect(rootStore?.me?.displayName).toBe('Alice')
      expect(rootStore?.me?.handle).toBe('alice.test')
      expect(rootStore?.session.accounts.length).toBe(2)
    }, WAIT_OPTS)
    expect(rootStore?.me?.displayName).toBe('Alice')
    expect(rootStore?.me?.handle).toBe('alice.test')
    expect(rootStore?.session.accounts.length).toBe(2)
  })

  it('will prompt for a password if you sign out', async () => {
    const {getByTestId} = render(<MobileShell />, rootStore)
    await waitFor(
      () => expect(getByTestId('settingsScreen')).toBeTruthy(),
      WAIT_OPTS,
    )

    // press the sign out button
    fireEvent.press(getByTestId('signOutBtn'))

    // in the logged out state
    await waitFor(
      () => expect(getByTestId('signinOrCreateAccount')).toBeTruthy(),
      WAIT_OPTS,
    )

    // move to signin view
    fireEvent.press(getByTestId('signInButton'))
    expect(getByTestId('signIn')).toBeTruthy()
    expect(getByTestId('chooseAccountForm')).toBeTruthy()

    // select an existing account
    fireEvent.press(getByTestId('chooseAccountBtn-alice.test'))

    // goes to login screen instead of straight back to settings
    expect(getByTestId('loginForm')).toBeTruthy()
  })
})