-
Notifications
You must be signed in to change notification settings - Fork 392
/
resolveConfig.test.ts
229 lines (211 loc) · 5.34 KB
/
resolveConfig.test.ts
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
import createClient from '@sanity/client'
import {of} from 'rxjs'
import {bufferTime, first} from 'rxjs/operators'
import {createMockAuthStore} from '../../store'
import {resolveConfig, createWorkspaceFromConfig, createSourceFromConfig} from '../resolveConfig'
describe('resolveConfig', () => {
it('throws on invalid tools property', async () => {
expect.assertions(1)
try {
await resolveConfig({
projectId: 'ppsg7ml5',
dataset: 'production',
// @ts-expect-error should be an array
tools: {},
})
.pipe(first())
.toPromise()
} catch (err) {
expect(err.message).toMatch('Expected `tools` to an array or a function but found object')
}
})
it('returns an observable that emits an array of fully resolved workspaces', async () => {
const projectId = 'ppsg7ml5'
const dataset = 'production'
const client = createClient({
projectId,
apiVersion: '2021-06-07',
dataset,
useCdn: false,
})
const [workspace] = await resolveConfig({
name: 'default',
dataset,
projectId,
auth: createMockAuthStore({client, currentUser: null}),
})
.pipe(first())
.toPromise()
expect(workspace).toMatchObject({
type: 'workspace',
name: 'default',
projectId: 'ppsg7ml5',
dataset: 'production',
unstable_sources: [
{
dataset: 'production',
name: 'default',
projectId: 'ppsg7ml5',
},
],
})
})
it('emits a new value if the auth stores emit a new auth state', async () => {
const projectId = 'ppsg7ml5'
const dataset = 'production'
const client = createClient({
projectId,
apiVersion: '2021-06-07',
dataset,
useCdn: false,
})
const results = await resolveConfig({
name: 'default',
dataset,
projectId,
auth: {
state: of(
{authenticated: true, client, currentUser: null},
{
authenticated: true,
client,
currentUser: {
id: 'test',
name: 'test',
email: 'hello@example.com',
role: '',
roles: [],
},
}
),
},
})
// this will buffer the results emitted in the observable into an array
.pipe(bufferTime(50))
.toPromise()
expect(results).toHaveLength(2)
const [firstResult, secondResult] = results
expect(firstResult).toMatchObject([
{
name: 'default',
projectId: 'ppsg7ml5',
dataset: 'production',
currentUser: null,
unstable_sources: [
{
dataset: 'production',
name: 'default',
projectId: 'ppsg7ml5',
},
],
},
])
expect(secondResult).toMatchObject([
{
type: 'workspace',
name: 'default',
projectId: 'ppsg7ml5',
dataset: 'production',
// note the extra user here
currentUser: {
id: 'test',
name: 'test',
email: 'hello@example.com',
},
unstable_sources: [
{
dataset: 'production',
name: 'default',
projectId: 'ppsg7ml5',
},
],
},
])
})
})
describe('createWorkspaceFromConfig', () => {
it('creates a promise that resolves to a full workspace', async () => {
const projectId = 'ppsg7ml5'
const dataset = 'production'
const workspace = await createWorkspaceFromConfig({
projectId,
dataset,
name: 'default',
})
expect(workspace).toMatchObject({
type: 'workspace',
name: 'default',
projectId: 'ppsg7ml5',
dataset: 'production',
currentUser: null,
unstable_sources: [
{
dataset: 'production',
name: 'default',
projectId: 'ppsg7ml5',
},
],
})
})
it('allows overriding the `currentUser` and `getClient`', async () => {
const projectId = 'ppsg7ml5'
const dataset = 'production'
const getClient = () =>
createClient({
projectId,
apiVersion: '2021-06-07',
dataset,
useCdn: false,
})
const currentUser = {
id: 'test',
name: 'test',
email: 'hello@example.com',
role: '',
roles: [],
}
const workspace = await createWorkspaceFromConfig({
projectId,
dataset,
name: 'default',
getClient,
currentUser,
})
expect(workspace).toMatchObject({
type: 'workspace',
name: 'default',
projectId: 'ppsg7ml5',
dataset: 'production',
currentUser: {
id: 'test',
name: 'test',
email: 'hello@example.com',
},
unstable_sources: [
{
dataset: 'production',
name: 'default',
projectId: 'ppsg7ml5',
},
],
})
})
})
describe('createSourceFromConfig', () => {
it('calls `createWorkspaceFromConfig` and returns the first source', async () => {
const projectId = 'ppsg7ml5'
const dataset = 'production'
const source = await createSourceFromConfig({
projectId,
dataset,
name: 'default',
})
expect(source).toMatchObject({
type: 'source',
name: 'default',
projectId: 'ppsg7ml5',
dataset: 'production',
currentUser: null,
})
})
})