@sanity/client 6.8.0-pink-lizard.12 → 6.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,259 +0,0 @@
1
- import {expect, test, vi} from 'vitest'
2
-
3
- import type {ContentSourceMap} from '../types'
4
- import {encodeIntoResult} from './encodeIntoResult'
5
-
6
- const encodeTestCases: {
7
- name: string
8
- queryResult: {
9
- result: unknown
10
- resultSourceMap: ContentSourceMap
11
- }
12
- expected: {
13
- encoderCalls: number
14
- encoderArgs: unknown[][]
15
- }
16
- }[] = [
17
- {
18
- name: 'resolves exact mappings to source',
19
- queryResult: {
20
- result: [
21
- {
22
- _id: 'foo',
23
- this: 'that',
24
- },
25
- ],
26
- resultSourceMap: {
27
- documents: [
28
- {
29
- _id: 'foo',
30
- _type: 'bar',
31
- },
32
- ],
33
- paths: ["$['this']"],
34
- mappings: {
35
- "$[0]['this']": {
36
- source: {
37
- document: 0,
38
- path: 0,
39
- type: 'documentValue',
40
- },
41
- type: 'value',
42
- },
43
- },
44
- },
45
- },
46
- expected: {
47
- encoderCalls: 1,
48
- encoderArgs: [
49
- [
50
- {
51
- sourcePath: ['this'],
52
- sourceDocument: {
53
- _id: 'foo',
54
- _type: 'bar',
55
- },
56
- resultPath: [0, 'this'],
57
- value: 'that',
58
- },
59
- ],
60
- ],
61
- },
62
- },
63
- {
64
- name: 'resolves aggregated mappings to source',
65
- queryResult: {
66
- result: [
67
- {
68
- _id: 'foo',
69
- nested: {
70
- object: {
71
- this: 'that',
72
- },
73
- },
74
- },
75
- ],
76
- resultSourceMap: {
77
- documents: [
78
- {
79
- _id: 'foo',
80
- _type: 'bar',
81
- },
82
- ],
83
- paths: ["$['something']['nested']"],
84
- mappings: {
85
- "$[0]['nested']": {
86
- source: {
87
- document: 0,
88
- path: 0,
89
- type: 'documentValue',
90
- },
91
- type: 'value',
92
- },
93
- },
94
- },
95
- },
96
- expected: {
97
- encoderCalls: 1,
98
- encoderArgs: [
99
- [
100
- {
101
- sourcePath: ['something', 'nested', 'object', 'this'],
102
- sourceDocument: {
103
- _id: 'foo',
104
- _type: 'bar',
105
- },
106
- resultPath: [0, 'nested', 'object', 'this'],
107
- value: 'that',
108
- },
109
- ],
110
- ],
111
- },
112
- },
113
- {
114
- name: 'plucks out _key to use as path segment',
115
- queryResult: {
116
- result: [
117
- {
118
- _id: 'foo',
119
- nested: {
120
- arr: [
121
- {
122
- _key: 'im_a_key',
123
- value: 'that',
124
- },
125
- ],
126
- },
127
- },
128
- ],
129
- resultSourceMap: {
130
- documents: [
131
- {
132
- _id: 'foo',
133
- _type: 'bar',
134
- },
135
- ],
136
- paths: ["$['projected']['nested']"],
137
- mappings: {
138
- "$[0]['nested']": {
139
- source: {
140
- document: 0,
141
- path: 0,
142
- type: 'documentValue',
143
- },
144
- type: 'value',
145
- },
146
- },
147
- },
148
- },
149
- expected: {
150
- encoderCalls: 2,
151
- encoderArgs: [
152
- [
153
- {
154
- sourcePath: ['projected', 'nested', 'arr', {_key: 'im_a_key', _index: 0}, '_key'],
155
- sourceDocument: {
156
- _id: 'foo',
157
- _type: 'bar',
158
- },
159
- resultPath: [0, 'nested', 'arr', {_key: 'im_a_key', _index: 0}, '_key'],
160
- value: 'im_a_key',
161
- },
162
- ],
163
- [
164
- {
165
- sourcePath: ['projected', 'nested', 'arr', {_key: 'im_a_key', _index: 0}, 'value'],
166
- sourceDocument: {
167
- _id: 'foo',
168
- _type: 'bar',
169
- },
170
- resultPath: [0, 'nested', 'arr', {_key: 'im_a_key', _index: 0}, 'value'],
171
- value: 'that',
172
- },
173
- ],
174
- ],
175
- },
176
- },
177
- {
178
- name: 'handles _key array filter selectors in source paths',
179
- queryResult: {
180
- result: [
181
- {
182
- _id: 'foo',
183
- arr: [
184
- {
185
- _key: 'im_a_key',
186
- value: 'that',
187
- },
188
- ],
189
- },
190
- ],
191
- resultSourceMap: {
192
- documents: [
193
- {
194
- _id: 'foo',
195
- _type: 'bar',
196
- },
197
- ],
198
- paths: ["$['projected'][?(@._key=='fooKey')]"],
199
- mappings: {
200
- "$[0]['arr']": {
201
- source: {
202
- document: 0,
203
- path: 0,
204
- type: 'documentValue',
205
- },
206
- type: 'value',
207
- },
208
- },
209
- },
210
- },
211
- expected: {
212
- encoderCalls: 2,
213
- encoderArgs: [
214
- [
215
- {
216
- sourcePath: [
217
- 'projected',
218
- {_key: 'fooKey', _index: -1},
219
- {_key: 'im_a_key', _index: 0},
220
- '_key',
221
- ],
222
- sourceDocument: {
223
- _id: 'foo',
224
- _type: 'bar',
225
- },
226
- resultPath: [0, 'arr', {_key: 'im_a_key', _index: 0}, '_key'],
227
- value: 'im_a_key',
228
- },
229
- ],
230
- [
231
- {
232
- sourcePath: [
233
- 'projected',
234
- {_key: 'fooKey', _index: -1},
235
- {_key: 'im_a_key', _index: 0},
236
- 'value',
237
- ],
238
- sourceDocument: {
239
- _id: 'foo',
240
- _type: 'bar',
241
- },
242
- resultPath: [0, 'arr', {_key: 'im_a_key', _index: 0}, 'value'],
243
- value: 'that',
244
- },
245
- ],
246
- ],
247
- },
248
- },
249
- ]
250
-
251
- test.each(encodeTestCases)('encode $name', ({queryResult, expected}) => {
252
- const mockTranscoder = vi.fn().mockImplementation((input: string) => input)
253
- encodeIntoResult(queryResult.result, queryResult.resultSourceMap, mockTranscoder)
254
-
255
- expect(mockTranscoder).toBeCalledTimes(expected.encoderCalls)
256
- for (const args of expected.encoderArgs) {
257
- expect(mockTranscoder).toBeCalledWith(...args)
258
- }
259
- })