forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserState.test.js
More file actions
128 lines (109 loc) · 3.75 KB
/
userState.test.js
File metadata and controls
128 lines (109 loc) · 3.75 KB
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
import React, { useState } from 'react'
import { render, waitFor } from '@testing-library/react'
import { MockedProvider } from '@apollo/client/testing'
import { makeVar } from '@apollo/client'
import userEvent from '@testing-library/user-event'
import { UserVarProvider, useUserVar } from '../userState'
const UserStateExample = () => {
const { login, logout, currentUser, isLoggedIn } = useUserVar()
const [newJWT, setNewJWT] = useState('')
const newJWTHandler = (e) => {
setNewJWT(e.target.value)
}
return (
<div>
<p>Current JWT: {currentUser.jwt}</p>
<p>Is logged in: {isLoggedIn().toString()}</p>
<label htmlFor="jwt">New JWT: </label>
<input type="text" id="jwt" value={newJWT} onChange={newJWTHandler} />
<button onClick={() => login({ jwt: newJWT })}>Login</button>
<button onClick={logout}>Logout</button>
</div>
)
}
describe('UserVarProvider', () => {
it('provides the UserState context via a Hook', async () => {
let userState
function Foo() {
userState = useUserVar()
return <p>asdf</p>
}
render(
<MockedProvider>
<UserVarProvider
userVar={makeVar({ jwt: null, tfaSendMethod: null, userName: null })}
>
<Foo />
</UserVarProvider>
</MockedProvider>,
)
await waitFor(() =>
expect(userState).toMatchObject({
currentUser: { jwt: null, tfaSendMethod: null, userName: null },
isLoggedIn: expect.any(Function),
login: expect.any(Function),
logout: expect.any(Function),
}),
)
})
})
describe('<UserVarProvider/>', () => {
it('is logged out - can login', () => {
const { getByText, getByRole } = render(
// MockedProvider is required as userState is capable of clearing apollo cache
<MockedProvider>
<UserVarProvider
userVar={makeVar({ jwt: null, tfaSendMethod: null, userName: null })}
>
<UserStateExample />
</UserVarProvider>
</MockedProvider>,
)
const currentJWTParagraph = getByText(/Current JWT:/)
const isLoggedInParagraph = getByText(/Is logged in/)
const newJWTInput = getByRole('textbox', { name: /New JWT:/ })
const loginButton = getByRole('button', { name: /Login/ })
const newJWTValue = 'some-new-JWT'
// Expect logged out
expect(currentJWTParagraph).toHaveTextContent(/^Current JWT:\s*$/)
expect(isLoggedInParagraph).toHaveTextContent(/false/)
// Log in
userEvent.type(newJWTInput, newJWTValue)
userEvent.click(loginButton)
// Expect logged in
expect(currentJWTParagraph).toHaveTextContent(
new RegExp(`Current JWT: ${newJWTValue}`),
)
expect(isLoggedInParagraph).toHaveTextContent(/true/)
})
it('is logged in - can logout', () => {
const defaultJWT = 'defaultJWT'
const { getByText, getByRole } = render(
// MockedProvider is required as userState is capable of clearing apollo cache
<MockedProvider>
<UserVarProvider
userVar={makeVar({
jwt: defaultJWT,
tfaSendMethod: null,
userName: null,
})}
>
<UserStateExample />
</UserVarProvider>
</MockedProvider>,
)
const currentJWTParagraph = getByText(/Current JWT:/)
const isLoggedInParagraph = getByText(/Is logged in/)
const logoutButton = getByRole('button', { name: /Logout/ })
// Expect logged in
expect(currentJWTParagraph).toHaveTextContent(
new RegExp(`Current JWT: ${defaultJWT}`),
)
expect(isLoggedInParagraph).toHaveTextContent(/true/)
// Log out
userEvent.click(logoutButton)
// Expect logged out
expect(currentJWTParagraph).toHaveTextContent(/^Current JWT:\s*$/)
expect(isLoggedInParagraph).toHaveTextContent(/false/)
})
})