asma-core-helpers 0.26.0 → 0.27.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.
- package/lib/collaboration/applyOperation.d.ts.map +1 -1
- package/lib/collaboration/applyOperation.js +354 -69
- package/lib/collaboration/applyOperation.js.map +1 -1
- package/lib/collaboration/index.d.ts +1 -0
- package/lib/collaboration/index.d.ts.map +1 -1
- package/lib/collaboration/index.js +1 -0
- package/lib/collaboration/index.js.map +1 -1
- package/lib/collaboration/operations.d.ts +35 -1
- package/lib/collaboration/operations.d.ts.map +1 -1
- package/lib/collaboration/operations.js +7 -0
- package/lib/collaboration/operations.js.map +1 -1
- package/lib/collaboration/schemas.d.ts +125 -2
- package/lib/collaboration/schemas.d.ts.map +1 -1
- package/lib/collaboration/schemas.js +135 -10
- package/lib/collaboration/schemas.js.map +1 -1
- package/lib/collaboration/templateDocument.d.ts +28 -1
- package/lib/collaboration/templateDocument.d.ts.map +1 -1
- package/lib/collaboration/templateDocument.js.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.d.ts +4 -0
- package/lib/collaboration/testHelpers/documentGenerator.d.ts.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.js +100 -4
- package/lib/collaboration/testHelpers/documentGenerator.js.map +1 -1
- package/package.json +1 -1
- package/src/collaboration/applyOperation.test.ts +379 -12
- package/src/collaboration/applyOperation.ts +411 -76
- package/src/collaboration/index.ts +1 -0
- package/src/collaboration/operations.ts +35 -0
- package/src/collaboration/property.test.ts +24 -1
- package/src/collaboration/schemas.test.ts +136 -1
- package/src/collaboration/schemas.ts +167 -5
- package/src/collaboration/templateDocument.ts +27 -1
- package/src/collaboration/testHelpers/documentGenerator.ts +101 -5
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
applyOperation,
|
|
9
9
|
} from './applyOperation.js'
|
|
10
10
|
import type { TemplateOp } from './operations.js'
|
|
11
|
-
import { emptyTemplateDocument } from './templateDocument.js'
|
|
11
|
+
import { emptyTemplateDocument, type QnrTemplateDocument } from './templateDocument.js'
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Guards the invariants a consumer would notice if a publish broke them. The exhaustive
|
|
@@ -59,6 +59,199 @@ describe('applyOperation', () => {
|
|
|
59
59
|
assert.deepEqual(moved.questionOrder, ['q-2', 'q-3', 'q-1'])
|
|
60
60
|
})
|
|
61
61
|
|
|
62
|
+
it('creates an ordered grid-owned column without adding it to the top-level order', () => {
|
|
63
|
+
const grid = apply([{ type: 'question.create', questionId: 'g-1', questionType: 'QuestionGrid' }])
|
|
64
|
+
|
|
65
|
+
const withColumns = apply(
|
|
66
|
+
[
|
|
67
|
+
{
|
|
68
|
+
type: 'gridColumn.create',
|
|
69
|
+
questionId: 'g-1',
|
|
70
|
+
columnQuestionId: 'c-1',
|
|
71
|
+
questionType: 'TextShort',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
type: 'gridColumn.create',
|
|
75
|
+
questionId: 'g-1',
|
|
76
|
+
columnQuestionId: 'c-2',
|
|
77
|
+
questionType: 'DateField',
|
|
78
|
+
atIndex: 0,
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
grid,
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
assert.deepEqual(withColumns.questionOrder, ['g-1'])
|
|
85
|
+
assert.deepEqual(withColumns.questionsById?.['g-1']?.grid?.columnIds, ['c-2', 'c-1'])
|
|
86
|
+
assert.deepEqual(withColumns.questionsById?.['c-1'], { type: 'TextShort' })
|
|
87
|
+
assert.deepEqual(withColumns.questionsById?.['c-2'], { type: 'DateField' })
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('refuses every grid-column operation through a non-grid question', () => {
|
|
91
|
+
const ordinary = apply([{ type: 'question.create', questionId: 'q-1', questionType: 'TextShort' }])
|
|
92
|
+
|
|
93
|
+
assert.throws(
|
|
94
|
+
() =>
|
|
95
|
+
applyOperation(ordinary, {
|
|
96
|
+
type: 'gridColumn.create',
|
|
97
|
+
questionId: 'q-1',
|
|
98
|
+
columnQuestionId: 'c-1',
|
|
99
|
+
questionType: 'TextShort',
|
|
100
|
+
}),
|
|
101
|
+
OperationConflictError,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
const malformed = {
|
|
105
|
+
...ordinary,
|
|
106
|
+
questionsById: {
|
|
107
|
+
'q-1': { type: 'TextShort', grid: { columnIds: ['c-1'] } },
|
|
108
|
+
'c-1': { type: 'TextShort' },
|
|
109
|
+
},
|
|
110
|
+
} satisfies QnrTemplateDocument
|
|
111
|
+
|
|
112
|
+
assert.throws(
|
|
113
|
+
() =>
|
|
114
|
+
applyOperation(malformed, {
|
|
115
|
+
type: 'gridColumn.move',
|
|
116
|
+
questionId: 'q-1',
|
|
117
|
+
columnQuestionId: 'c-1',
|
|
118
|
+
toIndex: 0,
|
|
119
|
+
}),
|
|
120
|
+
OperationConflictError,
|
|
121
|
+
)
|
|
122
|
+
assert.throws(
|
|
123
|
+
() =>
|
|
124
|
+
applyOperation(malformed, {
|
|
125
|
+
type: 'gridColumn.setLayout',
|
|
126
|
+
questionId: 'q-1',
|
|
127
|
+
columnQuestionId: 'c-1',
|
|
128
|
+
placement: { row: 0, cell: 0 },
|
|
129
|
+
}),
|
|
130
|
+
OperationConflictError,
|
|
131
|
+
)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('moves a column only inside its grid and refuses to inject it into the top-level order', () => {
|
|
135
|
+
const withColumns = apply([
|
|
136
|
+
{ type: 'question.create', questionId: 'g-1', questionType: 'QuestionGrid' },
|
|
137
|
+
{
|
|
138
|
+
type: 'gridColumn.create',
|
|
139
|
+
questionId: 'g-1',
|
|
140
|
+
columnQuestionId: 'c-1',
|
|
141
|
+
questionType: 'TextShort',
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
type: 'gridColumn.create',
|
|
145
|
+
questionId: 'g-1',
|
|
146
|
+
columnQuestionId: 'c-2',
|
|
147
|
+
questionType: 'DateField',
|
|
148
|
+
},
|
|
149
|
+
])
|
|
150
|
+
|
|
151
|
+
const moved = applyOperation(withColumns, {
|
|
152
|
+
type: 'gridColumn.move',
|
|
153
|
+
questionId: 'g-1',
|
|
154
|
+
columnQuestionId: 'c-1',
|
|
155
|
+
toIndex: 1,
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
assert.deepEqual(moved.questionsById?.['g-1']?.grid?.columnIds, ['c-2', 'c-1'])
|
|
159
|
+
assert.deepEqual(moved.questionOrder, ['g-1'])
|
|
160
|
+
assert.throws(
|
|
161
|
+
() => applyOperation(moved, { type: 'question.move', questionId: 'c-1', toIndex: 0 }),
|
|
162
|
+
OperationConflictError,
|
|
163
|
+
)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('sets and clears a grid column layout as one atomic placement', () => {
|
|
167
|
+
const withColumn = apply([
|
|
168
|
+
{ type: 'question.create', questionId: 'g-1', questionType: 'QuestionGrid' },
|
|
169
|
+
{
|
|
170
|
+
type: 'gridColumn.create',
|
|
171
|
+
questionId: 'g-1',
|
|
172
|
+
columnQuestionId: 'c-1',
|
|
173
|
+
questionType: 'TextShort',
|
|
174
|
+
},
|
|
175
|
+
])
|
|
176
|
+
|
|
177
|
+
const placed = applyOperation(withColumn, {
|
|
178
|
+
type: 'gridColumn.setLayout',
|
|
179
|
+
questionId: 'g-1',
|
|
180
|
+
columnQuestionId: 'c-1',
|
|
181
|
+
placement: { row: 2, cell: 1, keepCellSize: true },
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
assert.deepEqual(placed.questionsById?.['g-1']?.presentation?.rowEditor, {
|
|
185
|
+
layoutByQuestionId: { 'c-1': { row: 2, cell: 1, keepCellSize: true } },
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
const cleared = applyOperation(placed, {
|
|
189
|
+
type: 'gridColumn.setLayout',
|
|
190
|
+
questionId: 'g-1',
|
|
191
|
+
columnQuestionId: 'c-1',
|
|
192
|
+
placement: null,
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
assert.equal(cleared.questionsById?.['g-1']?.presentation, undefined)
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it('deletes a column from grid cells and every convention-named presentation map', () => {
|
|
199
|
+
const withColumns = apply([
|
|
200
|
+
{ type: 'question.create', questionId: 'g-1', questionType: 'QuestionGrid' },
|
|
201
|
+
{
|
|
202
|
+
type: 'gridColumn.create',
|
|
203
|
+
questionId: 'g-1',
|
|
204
|
+
columnQuestionId: 'c-1',
|
|
205
|
+
questionType: 'TextShort',
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
type: 'gridColumn.create',
|
|
209
|
+
questionId: 'g-1',
|
|
210
|
+
columnQuestionId: 'c-2',
|
|
211
|
+
questionType: 'TextShort',
|
|
212
|
+
},
|
|
213
|
+
{ type: 'gridRow.create', questionId: 'g-1', rowId: 'r-1' },
|
|
214
|
+
{ type: 'gridRow.updateCell', questionId: 'g-1', rowId: 'r-1', columnQuestionId: 'c-1', value: 'one' },
|
|
215
|
+
{ type: 'gridRow.updateCell', questionId: 'g-1', rowId: 'r-1', columnQuestionId: 'c-2', value: 'two' },
|
|
216
|
+
])
|
|
217
|
+
const authored = {
|
|
218
|
+
...withColumns,
|
|
219
|
+
questionsById: {
|
|
220
|
+
...withColumns.questionsById,
|
|
221
|
+
'g-1': {
|
|
222
|
+
...withColumns.questionsById?.['g-1'],
|
|
223
|
+
presentation: {
|
|
224
|
+
rowEditor: {
|
|
225
|
+
layoutByQuestionId: {
|
|
226
|
+
'c-1': { row: 0, cell: 0 },
|
|
227
|
+
'c-2': { row: 0, cell: 1 },
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
defaultColumnWidthsByQuestionId: { 'c-1': 120, 'c-2': 160 },
|
|
231
|
+
soloByQuestionId: { 'c-1': 'remove-the-empty-map' },
|
|
232
|
+
toolbar: {
|
|
233
|
+
labelsByQuestionId: { 'c-1': 'First', 'c-2': 'Second' },
|
|
234
|
+
theme: 'compact',
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const deleted = applyOperation(authored, { type: 'question.delete', questionId: 'c-1' })
|
|
242
|
+
const presentation = deleted.questionsById?.['g-1']?.presentation
|
|
243
|
+
|
|
244
|
+
assert.deepEqual(deleted.questionsById?.['g-1']?.grid?.columnIds, ['c-2'])
|
|
245
|
+
assert.deepEqual(deleted.gridRowsById?.['r-1']?.cells, { 'c-2': 'two' })
|
|
246
|
+
assert.deepEqual(presentation?.rowEditor?.layoutByQuestionId, { 'c-2': { row: 0, cell: 1 } })
|
|
247
|
+
assert.deepEqual(presentation?.defaultColumnWidthsByQuestionId, { 'c-2': 160 })
|
|
248
|
+
assert.equal(presentation?.soloByQuestionId, undefined)
|
|
249
|
+
assert.deepEqual(presentation?.toolbar, {
|
|
250
|
+
labelsByQuestionId: { 'c-2': 'Second' },
|
|
251
|
+
theme: 'compact',
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
|
|
62
255
|
it('drops the bindings that targeted a deleted question, keeping the shared node', () => {
|
|
63
256
|
const bound = apply([
|
|
64
257
|
{ type: 'question.create', questionId: 'q-1', questionType: 'TextShort' },
|
|
@@ -282,6 +475,14 @@ describe('applyOperation', () => {
|
|
|
282
475
|
{ type: 'question.create', questionId: 'q-2', questionType: 'TextShort' },
|
|
283
476
|
{ type: 'visibilityRule.set', ruleId: 'vr-1', questionId: 'q-1', condition: { sourceQuestionId: 'q-2', value: 'x' } },
|
|
284
477
|
{ type: 'highlightRule.set', ruleId: 'hr-1', questionId: 'q-1', condition: { sourceQuestionId: 'q-2', value: 'y' } },
|
|
478
|
+
{ type: 'narrativeRule.set', ruleId: 'nr-1', questionId: 'q-1', condition: { sourceQuestionId: 'q-2' } },
|
|
479
|
+
{
|
|
480
|
+
type: 'qnrRule.set',
|
|
481
|
+
ruleId: 'qr-1',
|
|
482
|
+
questionId: 'q-1',
|
|
483
|
+
condition: { sourceQuestionId: 'q-1' },
|
|
484
|
+
templateFamilyId: 'family-1',
|
|
485
|
+
},
|
|
285
486
|
])
|
|
286
487
|
|
|
287
488
|
const deleted = applyOperation(doc, { type: 'question.delete', questionId: 'q-1' })
|
|
@@ -290,6 +491,44 @@ describe('applyOperation', () => {
|
|
|
290
491
|
assert.equal(deleted.visibilityRuleOrderByQuestionId, undefined)
|
|
291
492
|
assert.equal(deleted.highlightRulesById, undefined)
|
|
292
493
|
assert.equal(deleted.highlightRuleOrderByQuestionId, undefined)
|
|
494
|
+
assert.equal(deleted.narrativeRulesById, undefined)
|
|
495
|
+
assert.equal(deleted.narrativeRuleOrderByQuestionId, undefined)
|
|
496
|
+
assert.equal(deleted.qnrRulesById, undefined)
|
|
497
|
+
assert.equal(deleted.qnrRuleOrderByQuestionId, undefined)
|
|
498
|
+
})
|
|
499
|
+
|
|
500
|
+
it('stores narrative and qnr rules in root collections with per-question primitive order', () => {
|
|
501
|
+
const doc = apply([
|
|
502
|
+
{ type: 'question.create', questionId: 'q-1', questionType: 'TextShort' },
|
|
503
|
+
{ type: 'question.create', questionId: 'q-2', questionType: 'TextShort' },
|
|
504
|
+
{ type: 'narrativeRule.set', ruleId: 'nr-1', questionId: 'q-1', condition: { sourceQuestionId: 'q-2' } },
|
|
505
|
+
{
|
|
506
|
+
type: 'qnrRule.set',
|
|
507
|
+
ruleId: 'qr-1',
|
|
508
|
+
questionId: 'q-1',
|
|
509
|
+
condition: { sourceQuestionId: 'q-1', value: 'yes' },
|
|
510
|
+
templateFamilyId: 'family-1',
|
|
511
|
+
},
|
|
512
|
+
])
|
|
513
|
+
|
|
514
|
+
assert.deepEqual(doc.narrativeRulesById, {
|
|
515
|
+
'nr-1': { condition: { sourceQuestionId: 'q-2' } },
|
|
516
|
+
})
|
|
517
|
+
assert.deepEqual(doc.narrativeRuleOrderByQuestionId, { 'q-1': ['nr-1'] })
|
|
518
|
+
assert.deepEqual(doc.qnrRulesById, {
|
|
519
|
+
'qr-1': {
|
|
520
|
+
condition: { sourceQuestionId: 'q-1', value: 'yes' },
|
|
521
|
+
templateFamilyId: 'family-1',
|
|
522
|
+
},
|
|
523
|
+
})
|
|
524
|
+
assert.deepEqual(doc.qnrRuleOrderByQuestionId, { 'q-1': ['qr-1'] })
|
|
525
|
+
|
|
526
|
+
const withoutNarrative = applyOperation(doc, { type: 'narrativeRule.delete', ruleId: 'nr-1' })
|
|
527
|
+
const withoutEither = applyOperation(withoutNarrative, { type: 'qnrRule.delete', ruleId: 'qr-1' })
|
|
528
|
+
assert.equal(withoutEither.narrativeRulesById, undefined)
|
|
529
|
+
assert.equal(withoutEither.narrativeRuleOrderByQuestionId, undefined)
|
|
530
|
+
assert.equal(withoutEither.qnrRulesById, undefined)
|
|
531
|
+
assert.equal(withoutEither.qnrRuleOrderByQuestionId, undefined)
|
|
293
532
|
})
|
|
294
533
|
|
|
295
534
|
it('keeps another question\'s rule that references the deleted question, for validation to surface', () => {
|
|
@@ -432,37 +671,109 @@ describe('applyOperation', () => {
|
|
|
432
671
|
)
|
|
433
672
|
})
|
|
434
673
|
|
|
435
|
-
it('
|
|
436
|
-
// `grid.columnIds`
|
|
437
|
-
//
|
|
674
|
+
it('reserves the column list and the row-editor layout for the grid-column operations', () => {
|
|
675
|
+
// Ownership (`grid.columnIds`) and one-placement-per-column layout are what the atomic
|
|
676
|
+
// grid ops exist to write. `question.updateField` carries a scalar or a whole primitive
|
|
677
|
+
// array, so reaching either path through it could only write the map wholesale — columns
|
|
678
|
+
// left owned by nobody, or every other column's placement dropped in one edit.
|
|
438
679
|
const doc = apply([
|
|
439
680
|
{ type: 'question.create', questionId: 'g-1', questionType: 'QuestionGrid' },
|
|
440
|
-
{ type: 'question.create', questionId: '
|
|
681
|
+
{ type: 'question.create', questionId: 'q-1', questionType: 'TextShort' },
|
|
441
682
|
])
|
|
442
683
|
|
|
443
|
-
|
|
684
|
+
for (const field of [
|
|
685
|
+
'grid',
|
|
686
|
+
'grid.columnIds',
|
|
687
|
+
'grid.columnIds.0',
|
|
688
|
+
'presentation.rowEditor',
|
|
689
|
+
'presentation.rowEditor.layoutByQuestionId',
|
|
690
|
+
'presentation.rowEditor.layoutByQuestionId.c-1.row',
|
|
691
|
+
]) {
|
|
692
|
+
assert.throws(
|
|
693
|
+
() => applyOperation(doc, { type: 'question.updateField', questionId: 'g-1', field, value: 'c-1' }),
|
|
694
|
+
OperationConflictError,
|
|
695
|
+
field,
|
|
696
|
+
)
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// The reservation is those two paths, not the bags that hold them: their siblings stay
|
|
700
|
+
// ordinary authored fields.
|
|
701
|
+
const configured = apply(
|
|
702
|
+
[
|
|
703
|
+
{ type: 'question.updateField', questionId: 'g-1', field: 'grid.singleRow', value: true },
|
|
704
|
+
{ type: 'question.updateField', questionId: 'g-1', field: 'presentation.headerTabId', value: 't-1' },
|
|
705
|
+
],
|
|
706
|
+
doc,
|
|
707
|
+
)
|
|
708
|
+
assert.deepEqual(configured.questionsById?.['g-1'], {
|
|
709
|
+
type: 'QuestionGrid',
|
|
710
|
+
grid: { singleRow: true },
|
|
711
|
+
presentation: { headerTabId: 't-1' },
|
|
712
|
+
})
|
|
713
|
+
|
|
714
|
+
// Grid configuration belongs to a grid: the document schema admits `grid` on no other
|
|
715
|
+
// type, so the reducer must not be the writer that puts it there.
|
|
716
|
+
assert.throws(
|
|
717
|
+
() =>
|
|
718
|
+
applyOperation(doc, {
|
|
719
|
+
type: 'question.updateField',
|
|
720
|
+
questionId: 'q-1',
|
|
721
|
+
field: 'grid.singleRow',
|
|
722
|
+
value: true,
|
|
723
|
+
}),
|
|
724
|
+
OperationConflictError,
|
|
725
|
+
)
|
|
726
|
+
})
|
|
444
727
|
|
|
445
|
-
|
|
728
|
+
it('refuses to write a question\'s type, so a grid cannot be flipped out from under its columns', () => {
|
|
729
|
+
// `type` is the discriminant the document schema branches on: only a `QuestionGrid` may
|
|
730
|
+
// carry `grid`. Flipping a grid that owns columns leaves a `columnIds` no reader accepts,
|
|
731
|
+
// and the columns it names are then owned by nobody — nor do they cascade with the
|
|
732
|
+
// delete, which collects owned columns only from a question still typed as a grid.
|
|
733
|
+
const doc = apply([
|
|
734
|
+
{ type: 'question.create', questionId: 'g-1', questionType: 'QuestionGrid' },
|
|
735
|
+
{ type: 'gridColumn.create', questionId: 'g-1', columnQuestionId: 'c-1', questionType: 'TextShort' },
|
|
736
|
+
{ type: 'question.create', questionId: 'q-1', questionType: 'TextShort' },
|
|
737
|
+
])
|
|
738
|
+
|
|
739
|
+
// Refused wherever it is aimed, and on any path below it: `question.create` is the only
|
|
740
|
+
// op that types a question, so there is no such thing as a legal type write here.
|
|
741
|
+
for (const [questionId, field] of [
|
|
742
|
+
['g-1', 'type'],
|
|
743
|
+
['g-1', 'type.name'],
|
|
744
|
+
['c-1', 'type'],
|
|
745
|
+
['q-1', 'type'],
|
|
746
|
+
] as const) {
|
|
747
|
+
assert.throws(
|
|
748
|
+
() => applyOperation(doc, { type: 'question.updateField', questionId, field, value: 'TextShort' }),
|
|
749
|
+
OperationConflictError,
|
|
750
|
+
`${questionId} ${field}`,
|
|
751
|
+
)
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
// Only `type` itself: a field that merely starts with the same letters is ordinary.
|
|
755
|
+
const typed = applyOperation(doc, { type: 'question.updateField', questionId: 'g-1', field: 'typeHint', value: 'table' })
|
|
756
|
+
assert.equal(typed.questionsById?.['g-1']?.['typeHint'], 'table')
|
|
757
|
+
assert.equal(typed.questionsById?.['g-1']?.type, 'QuestionGrid')
|
|
446
758
|
})
|
|
447
759
|
|
|
448
760
|
it('drops a deleted question from every grid\'s column list', () => {
|
|
449
761
|
const doc = apply([
|
|
450
762
|
{ type: 'question.create', questionId: 'g-1', questionType: 'QuestionGrid' },
|
|
451
|
-
{ type: '
|
|
452
|
-
{ type: '
|
|
453
|
-
{ type: 'question.updateField', questionId: 'g-1', field: 'grid.columnIds', value: ['c-1', 'c-2'] },
|
|
763
|
+
{ type: 'gridColumn.create', questionId: 'g-1', columnQuestionId: 'c-1', questionType: 'TextShort' },
|
|
764
|
+
{ type: 'gridColumn.create', questionId: 'g-1', columnQuestionId: 'c-2', questionType: 'DateField' },
|
|
454
765
|
])
|
|
455
766
|
|
|
456
767
|
const deleted = applyOperation(doc, { type: 'question.delete', questionId: 'c-1' })
|
|
457
768
|
|
|
458
769
|
assert.deepEqual(deleted.questionsById?.['g-1']?.grid?.columnIds, ['c-2'])
|
|
770
|
+
assert.equal(deleted.questionsById?.['c-1'], undefined)
|
|
459
771
|
})
|
|
460
772
|
|
|
461
773
|
it('drops the grid config key when its last field goes with a deleted column', () => {
|
|
462
774
|
const doc = apply([
|
|
463
775
|
{ type: 'question.create', questionId: 'g-1', questionType: 'QuestionGrid' },
|
|
464
|
-
{ type: '
|
|
465
|
-
{ type: 'question.updateField', questionId: 'g-1', field: 'grid.columnIds', value: ['c-1'] },
|
|
776
|
+
{ type: 'gridColumn.create', questionId: 'g-1', columnQuestionId: 'c-1', questionType: 'TextShort' },
|
|
466
777
|
])
|
|
467
778
|
|
|
468
779
|
const deleted = applyOperation(doc, { type: 'question.delete', questionId: 'c-1' })
|
|
@@ -471,6 +782,62 @@ describe('applyOperation', () => {
|
|
|
471
782
|
assert.deepEqual(deleted.questionsById?.['g-1'], { type: 'QuestionGrid' })
|
|
472
783
|
})
|
|
473
784
|
|
|
785
|
+
it('takes a deleted grid\'s columns and predefined rows with it', () => {
|
|
786
|
+
const doc = apply([
|
|
787
|
+
{ type: 'question.create', questionId: 'q-keep', questionType: 'TextShort' },
|
|
788
|
+
{ type: 'question.create', questionId: 'g-1', questionType: 'QuestionGrid' },
|
|
789
|
+
{ type: 'gridColumn.create', questionId: 'g-1', columnQuestionId: 'c-1', questionType: 'RadioButtons' },
|
|
790
|
+
{ type: 'gridColumn.create', questionId: 'g-1', columnQuestionId: 'c-2', questionType: 'TextShort' },
|
|
791
|
+
// A column carries authored state of its own: it must cascade the same way a
|
|
792
|
+
// top-level question's does, or the delete trades orphan questions for orphan rules.
|
|
793
|
+
{ type: 'alternative.create', questionId: 'c-1', alternativeId: 'a-1', label: 'Ja' },
|
|
794
|
+
{ type: 'visibilityRule.set', ruleId: 'vr-1', questionId: 'c-1', condition: { sourceQuestionId: 'q-keep' } },
|
|
795
|
+
{ type: 'mappingNode.create', nodeId: 'n-1', entityId: 'Actor' },
|
|
796
|
+
{
|
|
797
|
+
type: 'mappingBinding.create',
|
|
798
|
+
bindingId: 'b-1',
|
|
799
|
+
nodeId: 'n-1',
|
|
800
|
+
fieldId: 'Navn',
|
|
801
|
+
target: { kind: 'gridColumn', gridQuestionId: 'g-1', columnQuestionId: 'c-1' },
|
|
802
|
+
},
|
|
803
|
+
{ type: 'gridRow.create', questionId: 'g-1', rowId: 'r-1' },
|
|
804
|
+
{ type: 'gridRow.create', questionId: 'g-1', rowId: 'r-2', label: 'Andre' },
|
|
805
|
+
{ type: 'gridRow.updateCell', questionId: 'g-1', rowId: 'r-1', columnQuestionId: 'c-2', value: 'one' },
|
|
806
|
+
])
|
|
807
|
+
|
|
808
|
+
const deleted = applyOperation(doc, { type: 'question.delete', questionId: 'g-1' })
|
|
809
|
+
|
|
810
|
+
assert.deepEqual(deleted.questionOrder, ['q-keep'])
|
|
811
|
+
assert.deepEqual(Object.keys(deleted.questionsById ?? {}), ['q-keep'])
|
|
812
|
+
// DOC-LAW-2 all the way down: the emptied collections are gone, not stored empty.
|
|
813
|
+
assert.equal(deleted.gridRowsById, undefined)
|
|
814
|
+
assert.equal(deleted.gridRowOrderByQuestionId, undefined)
|
|
815
|
+
assert.equal(deleted.alternativesById, undefined)
|
|
816
|
+
assert.equal(deleted.alternativeOrderByQuestionId, undefined)
|
|
817
|
+
assert.equal(deleted.visibilityRulesById, undefined)
|
|
818
|
+
assert.equal(deleted.mappingBindingsById, undefined)
|
|
819
|
+
// The shared node survives: it is not the grid's to own.
|
|
820
|
+
assert.deepEqual(deleted.mappingNodesById, { 'n-1': { entityId: 'Actor' } })
|
|
821
|
+
})
|
|
822
|
+
|
|
823
|
+
it('deletes a grid whose column list is malformed without recursing forever', () => {
|
|
824
|
+
// Ownership cycles are not authorable, only importable. The cascade must terminate on
|
|
825
|
+
// them rather than blow the stack — a crash here is a 500 on someone's delete click.
|
|
826
|
+
const selfOwning = {
|
|
827
|
+
...doc0,
|
|
828
|
+
questionsById: {
|
|
829
|
+
'g-1': { type: 'QuestionGrid', grid: { columnIds: ['g-1', 'g-2'] } },
|
|
830
|
+
'g-2': { type: 'QuestionGrid', grid: { columnIds: ['g-1'] } },
|
|
831
|
+
},
|
|
832
|
+
questionOrder: ['g-1'],
|
|
833
|
+
} satisfies QnrTemplateDocument
|
|
834
|
+
|
|
835
|
+
const deleted = applyOperation(selfOwning, { type: 'question.delete', questionId: 'g-1' })
|
|
836
|
+
|
|
837
|
+
assert.equal(deleted.questionsById, undefined)
|
|
838
|
+
assert.deepEqual(deleted.questionOrder, [])
|
|
839
|
+
})
|
|
840
|
+
|
|
474
841
|
it('attaches a mapping root and maintains bindingOrder across create and delete', () => {
|
|
475
842
|
const attached = apply([
|
|
476
843
|
{ type: 'question.create', questionId: 'q-1', questionType: 'TextShort' },
|