@wix/bex-core 2.400.0 → 2.402.0
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/dist/cjs/hooks/useImport.js +7 -0
- package/dist/cjs/hooks/useImport.js.map +1 -1
- package/dist/cjs/state/ImportState/ImportState.js +188 -51
- package/dist/cjs/state/ImportState/ImportState.js.map +1 -1
- package/dist/cjs/state/ImportState/__tests__/ImportState.engine.spec.js +551 -0
- package/dist/cjs/state/ImportState/__tests__/ImportState.engine.spec.js.map +1 -1
- package/dist/cjs/state/ImportState/__tests__/buildImportConfig.spec.js +30 -4
- package/dist/cjs/state/ImportState/__tests__/buildImportConfig.spec.js.map +1 -1
- package/dist/cjs/state/ImportState/__tests__/importFieldCompatibility.spec.js +91 -0
- package/dist/cjs/state/ImportState/__tests__/importFieldCompatibility.spec.js.map +1 -0
- package/dist/cjs/state/ImportState/detectFieldType.js +3 -3
- package/dist/cjs/state/ImportState/detectFieldType.js.map +1 -1
- package/dist/cjs/state/ImportState/findExistingImportIds.js +35 -0
- package/dist/cjs/state/ImportState/findExistingImportIds.js.map +1 -0
- package/dist/cjs/state/ImportState/importFieldCompatibility.js +92 -0
- package/dist/cjs/state/ImportState/importFieldCompatibility.js.map +1 -0
- package/dist/cjs/state/ImportState/importSampling.js +6 -0
- package/dist/cjs/state/ImportState/importSampling.js.map +1 -0
- package/dist/cjs/state/ImportState/index.js.map +1 -1
- package/dist/cjs/types/Field.js.map +1 -1
- package/dist/cjs/types/FieldTypeDefinition.js.map +1 -1
- package/dist/esm/hooks/useImport.js +7 -1
- package/dist/esm/hooks/useImport.js.map +1 -1
- package/dist/esm/state/ImportState/ImportState.js +180 -51
- package/dist/esm/state/ImportState/ImportState.js.map +1 -1
- package/dist/esm/state/ImportState/__tests__/ImportState.engine.spec.js +421 -2
- package/dist/esm/state/ImportState/__tests__/ImportState.engine.spec.js.map +1 -1
- package/dist/esm/state/ImportState/__tests__/buildImportConfig.spec.js +32 -4
- package/dist/esm/state/ImportState/__tests__/buildImportConfig.spec.js.map +1 -1
- package/dist/esm/state/ImportState/__tests__/importFieldCompatibility.spec.js +99 -0
- package/dist/esm/state/ImportState/__tests__/importFieldCompatibility.spec.js.map +1 -0
- package/dist/esm/state/ImportState/detectFieldType.js +3 -3
- package/dist/esm/state/ImportState/detectFieldType.js.map +1 -1
- package/dist/esm/state/ImportState/findExistingImportIds.js +24 -0
- package/dist/esm/state/ImportState/findExistingImportIds.js.map +1 -0
- package/dist/esm/state/ImportState/importFieldCompatibility.js +102 -0
- package/dist/esm/state/ImportState/importFieldCompatibility.js.map +1 -0
- package/dist/esm/state/ImportState/importSampling.js +2 -0
- package/dist/esm/state/ImportState/importSampling.js.map +1 -0
- package/dist/esm/state/ImportState/index.js.map +1 -1
- package/dist/esm/types/FieldTypeDefinition.js.map +1 -1
- package/dist/types/hooks/useImport.d.ts +7 -3
- package/dist/types/hooks/useImport.d.ts.map +1 -1
- package/dist/types/state/ImportState/ImportState.d.ts +20 -8
- package/dist/types/state/ImportState/ImportState.d.ts.map +1 -1
- package/dist/types/state/ImportState/__tests__/importFieldCompatibility.spec.d.ts +2 -0
- package/dist/types/state/ImportState/__tests__/importFieldCompatibility.spec.d.ts.map +1 -0
- package/dist/types/state/ImportState/detectFieldType.d.ts.map +1 -1
- package/dist/types/state/ImportState/findExistingImportIds.d.ts +3 -0
- package/dist/types/state/ImportState/findExistingImportIds.d.ts.map +1 -0
- package/dist/types/state/ImportState/importFieldCompatibility.d.ts +4 -0
- package/dist/types/state/ImportState/importFieldCompatibility.d.ts.map +1 -0
- package/dist/types/state/ImportState/importSampling.d.ts +2 -0
- package/dist/types/state/ImportState/importSampling.d.ts.map +1 -0
- package/dist/types/state/ImportState/index.d.ts +1 -0
- package/dist/types/state/ImportState/index.d.ts.map +1 -1
- package/dist/types/types/Field.d.ts +4 -0
- package/dist/types/types/Field.d.ts.map +1 -1
- package/dist/types/types/FieldTypeDefinition.d.ts +3 -2
- package/dist/types/types/FieldTypeDefinition.d.ts.map +1 -1
- package/package.json +4 -4
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { defineSchemaSource } from '../../../types/SchemaSource';
|
|
2
|
+
import { findExistingImportIds } from '../findExistingImportIds';
|
|
1
3
|
import { TextDecoder, TextEncoder } from 'util';
|
|
2
|
-
import { runInAction } from 'mobx';
|
|
4
|
+
import { observable, runInAction } from 'mobx';
|
|
5
|
+
import { testkit } from '@wix/fe-essentials-standalone/testkit';
|
|
3
6
|
// The jsdom test env doesn't expose these globals, but `onFileSelected` decodes
|
|
4
7
|
// the uploaded bytes with `TextDecoder` (and the test encodes CSV with
|
|
5
8
|
// `TextEncoder`). Polyfill from Node's `util`.
|
|
@@ -35,7 +38,7 @@ function setup(clientOverrides = {}, extra = {}) {
|
|
|
35
38
|
const showToast = jest.fn();
|
|
36
39
|
const downloadCsv = jest.fn();
|
|
37
40
|
const translate = (key, params) => params ? `${key}:${JSON.stringify(params)}` : key;
|
|
38
|
-
const { detectColumnType, config: configOverride, collectionId, destination, updateSchema, createFields, fieldManagement, mergeFields, fields, idFieldKey, backup, jobApplicationErrorMap, reportBi, noSeed, } = extra;
|
|
41
|
+
const { detectColumnType, isValueCompatible, config: configOverride, collectionId, destination, updateSchema, createFields, fieldManagement, mergeFields, fields, idFieldKey, backup, jobApplicationErrorMap, reportBi, noSeed, } = extra;
|
|
39
42
|
const collection = {
|
|
40
43
|
clearResultAndMoveToStart: jest.fn(),
|
|
41
44
|
queryName: collectionId ?? 'Movies',
|
|
@@ -60,6 +63,9 @@ function setup(clientOverrides = {}, extra = {}) {
|
|
|
60
63
|
...(jobApplicationErrorMap && { jobApplicationErrorMap }),
|
|
61
64
|
};
|
|
62
65
|
const props = {
|
|
66
|
+
createFieldValidator: extra.createFieldValidator,
|
|
67
|
+
getExistingIds: extra.getExistingIds,
|
|
68
|
+
fieldsSource: extra.fieldsSource,
|
|
63
69
|
config,
|
|
64
70
|
httpClient: {
|
|
65
71
|
request: jest.fn(),
|
|
@@ -73,6 +79,7 @@ function setup(clientOverrides = {}, extra = {}) {
|
|
|
73
79
|
showToast: showToast,
|
|
74
80
|
downloadCsv,
|
|
75
81
|
detectColumnType,
|
|
82
|
+
isValueCompatible,
|
|
76
83
|
...(reportBi && { reportBi }),
|
|
77
84
|
};
|
|
78
85
|
const state = new ImportState(props);
|
|
@@ -100,6 +107,33 @@ describe('ImportState engine (data-hub job)', () => {
|
|
|
100
107
|
// real even if the preset defaults to fake ones.
|
|
101
108
|
jest.useRealTimers();
|
|
102
109
|
});
|
|
110
|
+
it('uploads duplicate-named columns to their own destination fields', async () => {
|
|
111
|
+
const { state, client } = setup({}, {
|
|
112
|
+
fields: [
|
|
113
|
+
{ id: 'price', header: 'Number', type: 'NUMBER' },
|
|
114
|
+
{ id: 'otherPrice', header: 'Number', type: 'NUMBER' },
|
|
115
|
+
],
|
|
116
|
+
});
|
|
117
|
+
await state.onFileSelected({
|
|
118
|
+
name: 'duplicates.csv',
|
|
119
|
+
arrayBuffer: async () => new TextEncoder().encode('Number,Number\n20,30\n40,50\n').buffer,
|
|
120
|
+
});
|
|
121
|
+
expect(state.mappingByHeader).toEqual({
|
|
122
|
+
Number: { kind: 'existing', fieldId: 'price' },
|
|
123
|
+
'Number (2)': { kind: 'existing', fieldId: 'otherPrice' },
|
|
124
|
+
});
|
|
125
|
+
await state.startImport();
|
|
126
|
+
const uploaded = await new Promise((resolve) => {
|
|
127
|
+
const reader = new FileReader();
|
|
128
|
+
reader.onload = () => resolve(String(reader.result));
|
|
129
|
+
reader.readAsText(client.uploadFile.mock.calls[0][0]);
|
|
130
|
+
});
|
|
131
|
+
expect(uploaded.split(/\r?\n/)).toEqual([
|
|
132
|
+
'price,otherPrice',
|
|
133
|
+
'20,30',
|
|
134
|
+
'40,50',
|
|
135
|
+
]);
|
|
136
|
+
});
|
|
103
137
|
it('uploads the aligned file, submits a job, and reports success', async () => {
|
|
104
138
|
const { state, client, importModalState, collection } = setup();
|
|
105
139
|
await state.startImport();
|
|
@@ -670,6 +704,30 @@ describe('ImportState platformized destination (non–Wix-Data)', () => {
|
|
|
670
704
|
// The merge runs alongside the data refetch on the success path.
|
|
671
705
|
expect(collection.clearResultAndMoveToStart).toHaveBeenCalled();
|
|
672
706
|
});
|
|
707
|
+
it('validates new field settings before merging them on completion', async () => {
|
|
708
|
+
const mergeFields = jest.fn();
|
|
709
|
+
const updateSchema = async () => ({
|
|
710
|
+
color: {
|
|
711
|
+
id: 'color',
|
|
712
|
+
displayName: 'Color',
|
|
713
|
+
type: 'SHORT_TEXT',
|
|
714
|
+
validation: { required: true },
|
|
715
|
+
},
|
|
716
|
+
});
|
|
717
|
+
const { state, downloadCsv } = setup({}, { updateSchema, mergeFields });
|
|
718
|
+
withNewFieldMapping(state);
|
|
719
|
+
runInAction(() => {
|
|
720
|
+
state.csvRows = [
|
|
721
|
+
['Apple', ''],
|
|
722
|
+
['Pear', 'Green'],
|
|
723
|
+
];
|
|
724
|
+
});
|
|
725
|
+
await state.startImport();
|
|
726
|
+
await state.downloadFailedRows();
|
|
727
|
+
expect(state.result.failed).toBe(1);
|
|
728
|
+
expect(downloadCsv).toHaveBeenCalledWith(expect.stringContaining('Apple,'), 'failed-data.csv');
|
|
729
|
+
expect(mergeFields).toHaveBeenCalledTimes(1);
|
|
730
|
+
});
|
|
673
731
|
it('does not call mergeFields for a plain import (no new fields)', async () => {
|
|
674
732
|
const mergeFields = jest.fn();
|
|
675
733
|
const updateSchema = jest.fn(async () => RETURNED_FIELDS);
|
|
@@ -1063,6 +1121,367 @@ describe('ImportState items limit and completion hooks', () => {
|
|
|
1063
1121
|
expect(onImportCompleted).toHaveBeenCalledTimes(expectedCalls);
|
|
1064
1122
|
});
|
|
1065
1123
|
});
|
|
1124
|
+
describe('ImportState row validation', () => {
|
|
1125
|
+
testkit.beforeAndAfter();
|
|
1126
|
+
beforeEach(() => jest.useRealTimers());
|
|
1127
|
+
it('uploads only valid rows and includes validation failures in the summary', async () => {
|
|
1128
|
+
const { state, client, downloadCsv } = setup({
|
|
1129
|
+
getJob: jest.fn(async () => ({
|
|
1130
|
+
...COMPLETED_JOB,
|
|
1131
|
+
progress: { itemsSuccessful: 1, itemsRejected: 0, current: 1 },
|
|
1132
|
+
})),
|
|
1133
|
+
}, {
|
|
1134
|
+
createFieldValidator: () => (raw) => raw === 'Apple' ? ['Name is required'] : [],
|
|
1135
|
+
});
|
|
1136
|
+
await state.startImport();
|
|
1137
|
+
const file = client.uploadFile.mock.calls[0][0];
|
|
1138
|
+
const content = await new Promise((resolve) => {
|
|
1139
|
+
const reader = new FileReader();
|
|
1140
|
+
reader.onload = () => resolve(String(reader.result));
|
|
1141
|
+
reader.readAsText(file);
|
|
1142
|
+
});
|
|
1143
|
+
expect(content).toBe('name\r\nPear');
|
|
1144
|
+
expect(state.result).toEqual({
|
|
1145
|
+
succeeded: 1,
|
|
1146
|
+
created: 1,
|
|
1147
|
+
updated: 0,
|
|
1148
|
+
failed: 1,
|
|
1149
|
+
});
|
|
1150
|
+
expect(state.totalCount).toBe(2);
|
|
1151
|
+
expect(state.processedCount).toBe(2);
|
|
1152
|
+
expect(state.importStatus.data).toEqual(state.result);
|
|
1153
|
+
expect(state.currentStep).toBe('summary');
|
|
1154
|
+
await state.downloadFailedRows();
|
|
1155
|
+
expect(client.listMovementLogs).not.toHaveBeenCalled();
|
|
1156
|
+
expect(downloadCsv).toHaveBeenCalledWith('cairo.import.failureReasonColumn,Name\r\nName is required,Apple', 'failed-data.csv');
|
|
1157
|
+
});
|
|
1158
|
+
it('reports and downloads all-invalid rows without starting a server job', async () => {
|
|
1159
|
+
const { state, client, downloadCsv } = setup({}, {
|
|
1160
|
+
createFieldValidator: () => (raw) => [`Invalid ${raw}`],
|
|
1161
|
+
});
|
|
1162
|
+
await state.startImport();
|
|
1163
|
+
await state.downloadFailedRows();
|
|
1164
|
+
expect(client.uploadFile).not.toHaveBeenCalled();
|
|
1165
|
+
expect(client.listMovementLogs).not.toHaveBeenCalled();
|
|
1166
|
+
expect(state.result).toEqual({
|
|
1167
|
+
succeeded: 0,
|
|
1168
|
+
created: 0,
|
|
1169
|
+
updated: 0,
|
|
1170
|
+
failed: 2,
|
|
1171
|
+
});
|
|
1172
|
+
expect(state.currentStep).toBe('summary');
|
|
1173
|
+
expect(downloadCsv).toHaveBeenCalledWith('cairo.import.failureReasonColumn,Name\r\nInvalid Apple,Apple\r\nInvalid Pear,Pear', 'failed-data.csv');
|
|
1174
|
+
});
|
|
1175
|
+
it('combines local and server failures without shifting CSV row contents', async () => {
|
|
1176
|
+
const { state, downloadCsv } = setup({
|
|
1177
|
+
getJob: jest.fn(async () => ({
|
|
1178
|
+
...COMPLETED_JOB,
|
|
1179
|
+
status: JobStatus.PARTIALLY_SUCCESSFUL,
|
|
1180
|
+
progress: { itemsSuccessful: 1, itemsRejected: 1, current: 2 },
|
|
1181
|
+
})),
|
|
1182
|
+
listMovementLogs: jest.fn(async () => ({
|
|
1183
|
+
logs: [
|
|
1184
|
+
{
|
|
1185
|
+
sourceItemId: 'row 2',
|
|
1186
|
+
failure: { description: 'Server rejected Pear' },
|
|
1187
|
+
},
|
|
1188
|
+
],
|
|
1189
|
+
pagingMetadata: { hasNext: false },
|
|
1190
|
+
})),
|
|
1191
|
+
}, {
|
|
1192
|
+
createFieldValidator: () => (raw) => raw === 'Apple' ? ['Invalid Apple'] : [],
|
|
1193
|
+
});
|
|
1194
|
+
runInAction(() => {
|
|
1195
|
+
state.csvRows.push(['Mango']);
|
|
1196
|
+
});
|
|
1197
|
+
await state.startImport();
|
|
1198
|
+
await state.downloadFailedRows();
|
|
1199
|
+
expect(state.result).toEqual({
|
|
1200
|
+
succeeded: 1,
|
|
1201
|
+
created: 1,
|
|
1202
|
+
updated: 0,
|
|
1203
|
+
failed: 2,
|
|
1204
|
+
});
|
|
1205
|
+
expect(downloadCsv).toHaveBeenCalledWith('cairo.import.failureReasonColumn,Name\r\nInvalid Apple,Apple\r\nServer rejected Pear,Pear', 'failed-data.csv');
|
|
1206
|
+
});
|
|
1207
|
+
it('checks empty required values for manually configured destinations', async () => {
|
|
1208
|
+
const { state, client, downloadCsv } = setup({}, {
|
|
1209
|
+
destination: { storesCatalogProducts: {} },
|
|
1210
|
+
fields: [{ id: 'name', header: 'Name', required: true }],
|
|
1211
|
+
});
|
|
1212
|
+
runInAction(() => {
|
|
1213
|
+
state.csvRows = [['']];
|
|
1214
|
+
});
|
|
1215
|
+
await state.startImport();
|
|
1216
|
+
await state.downloadFailedRows();
|
|
1217
|
+
expect(client.submitImportJob).not.toHaveBeenCalled();
|
|
1218
|
+
expect(state.result.failed).toBe(1);
|
|
1219
|
+
expect(downloadCsv).toHaveBeenCalledWith(expect.stringContaining('cairo.editableTable.field.validation.required'), 'failed-data.csv');
|
|
1220
|
+
});
|
|
1221
|
+
it('skips existing and repeated IDs before validating their values', async () => {
|
|
1222
|
+
const find = jest.fn(async () => ({
|
|
1223
|
+
items: [{ id: 'existing' }],
|
|
1224
|
+
total: 1,
|
|
1225
|
+
}));
|
|
1226
|
+
const source = defineSchemaSource({
|
|
1227
|
+
collectionId: 'Movies',
|
|
1228
|
+
loadSchema: async () => ({
|
|
1229
|
+
schema: { id: 'Movies', idField: 'id', displayField: 'id', fields: {} },
|
|
1230
|
+
backend: { find },
|
|
1231
|
+
}),
|
|
1232
|
+
});
|
|
1233
|
+
const { state, client } = setup({}, {
|
|
1234
|
+
idFieldKey: 'id',
|
|
1235
|
+
fields: [
|
|
1236
|
+
{ id: 'id', header: 'ID' },
|
|
1237
|
+
{ id: 'name', header: 'Name', required: true },
|
|
1238
|
+
],
|
|
1239
|
+
getExistingIds: (ids, signal) => findExistingImportIds(source, ids, signal),
|
|
1240
|
+
});
|
|
1241
|
+
runInAction(() => {
|
|
1242
|
+
state.csvHeaders = ['ID', 'Name'];
|
|
1243
|
+
state.csvRows = [
|
|
1244
|
+
['existing', ''],
|
|
1245
|
+
['new', 'Valid'],
|
|
1246
|
+
['new', ''],
|
|
1247
|
+
['invalid', ''],
|
|
1248
|
+
];
|
|
1249
|
+
});
|
|
1250
|
+
state.setMapping('ID', 'id');
|
|
1251
|
+
state.setWritePolicy(WritePolicy.SKIP_EXISTING);
|
|
1252
|
+
await state.startImport();
|
|
1253
|
+
expect(state.result.failed).toBe(1);
|
|
1254
|
+
expect(state.totalCount).toBe(2);
|
|
1255
|
+
const file = client.uploadFile.mock.calls[0][0];
|
|
1256
|
+
const content = await new Promise((resolve) => {
|
|
1257
|
+
const reader = new FileReader();
|
|
1258
|
+
reader.onload = () => resolve(String(reader.result));
|
|
1259
|
+
reader.readAsText(file);
|
|
1260
|
+
});
|
|
1261
|
+
expect(content).toBe('id,name\r\nnew,Valid');
|
|
1262
|
+
expect(find).toHaveBeenCalledWith(expect.objectContaining({
|
|
1263
|
+
filters: { id: [{ id: 'existing' }, { id: 'new' }, { id: 'invalid' }] },
|
|
1264
|
+
rawSearch: '',
|
|
1265
|
+
offset: 0,
|
|
1266
|
+
limit: 3,
|
|
1267
|
+
}));
|
|
1268
|
+
});
|
|
1269
|
+
it('passes source constraints and required import overrides through the same validator', async () => {
|
|
1270
|
+
const sourceField = {
|
|
1271
|
+
id: 'name',
|
|
1272
|
+
displayName: 'Name',
|
|
1273
|
+
type: 'SHORT_TEXT',
|
|
1274
|
+
validation: { required: false, pattern: '^OK$' },
|
|
1275
|
+
};
|
|
1276
|
+
const { state } = setup({}, {
|
|
1277
|
+
fields: [{ id: 'name', header: 'Name', required: true }],
|
|
1278
|
+
fieldsSource: {
|
|
1279
|
+
fields: [sourceField],
|
|
1280
|
+
filterableFields: [],
|
|
1281
|
+
manageableFields: [],
|
|
1282
|
+
load: async () => { },
|
|
1283
|
+
columnIdForField: (field) => field.id,
|
|
1284
|
+
queryFieldId: (field) => field.id,
|
|
1285
|
+
getCellValue: () => undefined,
|
|
1286
|
+
},
|
|
1287
|
+
createFieldValidator: (field) => (raw) => field.validation?.required &&
|
|
1288
|
+
raw !== 'OK' &&
|
|
1289
|
+
field.validation?.pattern === '^OK$'
|
|
1290
|
+
? ['Name must match']
|
|
1291
|
+
: [],
|
|
1292
|
+
});
|
|
1293
|
+
await state.startImport();
|
|
1294
|
+
expect(state.result.failed).toBe(2);
|
|
1295
|
+
});
|
|
1296
|
+
it('does not validate CSV columns the user skipped', async () => {
|
|
1297
|
+
const { state, client } = setup({}, {
|
|
1298
|
+
fields: [
|
|
1299
|
+
{ id: 'name', header: 'Name' },
|
|
1300
|
+
{ id: 'extra', header: 'Extra' },
|
|
1301
|
+
],
|
|
1302
|
+
createFieldValidator: () => (raw) => raw === 'invalid' ? ['Invalid value'] : [],
|
|
1303
|
+
});
|
|
1304
|
+
runInAction(() => {
|
|
1305
|
+
state.csvHeaders = ['Name', 'Extra'];
|
|
1306
|
+
state.csvRows = [
|
|
1307
|
+
['Apple', 'invalid'],
|
|
1308
|
+
['Pear', 'invalid'],
|
|
1309
|
+
];
|
|
1310
|
+
});
|
|
1311
|
+
state.setMapping('Extra', null);
|
|
1312
|
+
await state.startImport();
|
|
1313
|
+
expect(state.result.failed).toBe(0);
|
|
1314
|
+
expect(client.uploadFile).toHaveBeenCalledTimes(1);
|
|
1315
|
+
});
|
|
1316
|
+
it('clears previous validation failures when retrying', async () => {
|
|
1317
|
+
let invalid = true;
|
|
1318
|
+
const { state, client } = setup({}, {
|
|
1319
|
+
createFieldValidator: () => (raw) => invalid ? [`Invalid ${raw}`] : [],
|
|
1320
|
+
});
|
|
1321
|
+
await state.startImport();
|
|
1322
|
+
invalid = false;
|
|
1323
|
+
await state.startImport();
|
|
1324
|
+
expect(state.result.failed).toBe(0);
|
|
1325
|
+
expect(state.result.succeeded).toBe(2);
|
|
1326
|
+
expect(client.uploadFile).toHaveBeenCalledTimes(1);
|
|
1327
|
+
});
|
|
1328
|
+
});
|
|
1329
|
+
describe('ImportState mapping dropdown fields (CAIRO-4392)', () => {
|
|
1330
|
+
testkit.beforeAndAfter();
|
|
1331
|
+
const FIELDS = [
|
|
1332
|
+
{ id: 'title', header: 'Title', type: 'SHORT_TEXT' },
|
|
1333
|
+
{ id: 'price', header: 'Price', type: 'NUMBER' },
|
|
1334
|
+
{ id: 'launch', header: 'Launch Date', type: 'DATE' },
|
|
1335
|
+
{ id: 'website', header: 'Website', type: 'URL' },
|
|
1336
|
+
{ id: 'tags', header: 'Tags', type: 'ARRAY' },
|
|
1337
|
+
{ id: 'reference', header: 'Reference', type: 'REFERENCE' },
|
|
1338
|
+
{ id: 'legacy', header: 'Legacy' },
|
|
1339
|
+
];
|
|
1340
|
+
function setupColumns(headers, rows) {
|
|
1341
|
+
const { state } = setup({}, { fields: FIELDS });
|
|
1342
|
+
runInAction(() => {
|
|
1343
|
+
state.csvHeaders = headers;
|
|
1344
|
+
state.csvRows = rows;
|
|
1345
|
+
state.mappingByHeader = {};
|
|
1346
|
+
});
|
|
1347
|
+
return state;
|
|
1348
|
+
}
|
|
1349
|
+
const fieldIds = (state, header) => state.fieldsForHeader(header).map((field) => field.id);
|
|
1350
|
+
it('detects a sparse column from its non-empty values', () => {
|
|
1351
|
+
const state = setupColumns(['Price'], [['10'], [''], ['20'], [''], ['30'], ['']]);
|
|
1352
|
+
expect(fieldIds(state, 'Price')).toContain('price');
|
|
1353
|
+
expect(fieldIds(state, 'Price')).not.toContain('launch');
|
|
1354
|
+
});
|
|
1355
|
+
it('offers only the fields that fit the column data', () => {
|
|
1356
|
+
const state = setupColumns(['Price', 'Launch Date', 'Notes'], [
|
|
1357
|
+
['10', '2026-01-01', 'first'],
|
|
1358
|
+
['20', '2026-01-02', 'second'],
|
|
1359
|
+
]);
|
|
1360
|
+
expect(fieldIds(state, 'Price')).toEqual(['title', 'price', 'legacy']);
|
|
1361
|
+
expect(fieldIds(state, 'Launch Date')).toEqual([
|
|
1362
|
+
'title',
|
|
1363
|
+
'launch',
|
|
1364
|
+
'legacy',
|
|
1365
|
+
]);
|
|
1366
|
+
expect(fieldIds(state, 'Notes')).toEqual(['title', 'reference', 'legacy']);
|
|
1367
|
+
});
|
|
1368
|
+
it('keeps the field a column is mapped to even when it does not fit', () => {
|
|
1369
|
+
const state = setupColumns(['Score'], [['high'], ['low']]);
|
|
1370
|
+
state.setMapping('Score', 'price');
|
|
1371
|
+
expect(fieldIds(state, 'Score')).toContain('price');
|
|
1372
|
+
expect(fieldIds(state, 'Score')).not.toContain('launch');
|
|
1373
|
+
});
|
|
1374
|
+
it('offers every field for a column with no values', () => {
|
|
1375
|
+
const state = setupColumns(['Empty'], [[''], ['']]);
|
|
1376
|
+
expect(fieldIds(state, 'Empty')).toEqual(FIELDS.map((field) => field.id));
|
|
1377
|
+
});
|
|
1378
|
+
it('keeps custom fields available but hides unknown fields even when selected', () => {
|
|
1379
|
+
const { state } = setup({}, {
|
|
1380
|
+
fields: [
|
|
1381
|
+
{ id: 'rating', header: 'Rating', type: 'CUSTOM' },
|
|
1382
|
+
{ id: 'unknown', header: 'Unknown', type: 'UNKNOWN' },
|
|
1383
|
+
],
|
|
1384
|
+
});
|
|
1385
|
+
state.setMapping('Name', 'unknown');
|
|
1386
|
+
expect(fieldIds(state, 'Name')).toEqual(['rating']);
|
|
1387
|
+
});
|
|
1388
|
+
it('does not auto-map an unknown field with a matching name', async () => {
|
|
1389
|
+
const { state } = setup({}, {
|
|
1390
|
+
fields: [{ id: 'status', header: 'Status', type: 'UNKNOWN' }],
|
|
1391
|
+
});
|
|
1392
|
+
await state.onFileSelected({
|
|
1393
|
+
name: 'status.csv',
|
|
1394
|
+
arrayBuffer: async () => new TextEncoder().encode('Status\nPUBLISHED\n').buffer,
|
|
1395
|
+
});
|
|
1396
|
+
expect(state.mappingByHeader.Status).toBeNull();
|
|
1397
|
+
expect(fieldIds(state, 'Status')).toEqual([]);
|
|
1398
|
+
});
|
|
1399
|
+
it('separates duplicate headers without colliding with existing suffixes', async () => {
|
|
1400
|
+
const { state } = setup({}, { fields: FIELDS });
|
|
1401
|
+
await state.onFileSelected({
|
|
1402
|
+
name: 'duplicates.csv',
|
|
1403
|
+
arrayBuffer: async () => new TextEncoder().encode('Price,Price,Price (2)\n20,,hello\n').buffer,
|
|
1404
|
+
});
|
|
1405
|
+
expect(state.csvHeaders).toEqual(['Price', 'Price (3)', 'Price (2)']);
|
|
1406
|
+
expect(fieldIds(state, 'Price')).not.toContain('reference');
|
|
1407
|
+
expect(fieldIds(state, 'Price (3)')).toContain('reference');
|
|
1408
|
+
state.setMapping('Price (3)', 'reference');
|
|
1409
|
+
expect(state.mappingByHeader.Price).toEqual({
|
|
1410
|
+
kind: 'existing',
|
|
1411
|
+
fieldId: 'price',
|
|
1412
|
+
});
|
|
1413
|
+
expect(state.mappingByHeader['Price (3)']).toEqual({
|
|
1414
|
+
kind: 'existing',
|
|
1415
|
+
fieldId: 'reference',
|
|
1416
|
+
});
|
|
1417
|
+
});
|
|
1418
|
+
it('reuses format checks when mappings change and refreshes them for a new upload', async () => {
|
|
1419
|
+
const isValueCompatible = jest.fn(() => undefined);
|
|
1420
|
+
const { state } = setup({}, { fields: FIELDS, isValueCompatible });
|
|
1421
|
+
expect(fieldIds(state, 'Name')).toContain('title');
|
|
1422
|
+
const initialChecks = isValueCompatible.mock.calls.length;
|
|
1423
|
+
expect(initialChecks).toBeGreaterThan(0);
|
|
1424
|
+
state.setMapping('Name', 'price');
|
|
1425
|
+
expect(fieldIds(state, 'Name')).toContain('price');
|
|
1426
|
+
state.setMapping('Name', null);
|
|
1427
|
+
expect(fieldIds(state, 'Name')).not.toContain('price');
|
|
1428
|
+
expect(isValueCompatible).toHaveBeenCalledTimes(initialChecks);
|
|
1429
|
+
await state.onFileSelected({
|
|
1430
|
+
name: 'numbers.csv',
|
|
1431
|
+
arrayBuffer: async () => new TextEncoder().encode('Name\n10\n20\n').buffer,
|
|
1432
|
+
});
|
|
1433
|
+
expect(fieldIds(state, 'Name')).toContain('price');
|
|
1434
|
+
expect(isValueCompatible.mock.calls.length).toBeGreaterThan(initialChecks);
|
|
1435
|
+
});
|
|
1436
|
+
it('refreshes compatibility when the source fields change', () => {
|
|
1437
|
+
const fields = observable.box([
|
|
1438
|
+
{ id: 'value', header: 'Value', type: 'NUMBER' },
|
|
1439
|
+
]);
|
|
1440
|
+
const { state } = setup({}, {
|
|
1441
|
+
config: {
|
|
1442
|
+
get fields() {
|
|
1443
|
+
return fields.get();
|
|
1444
|
+
},
|
|
1445
|
+
dataSource: { collectionId: 'Movies' },
|
|
1446
|
+
},
|
|
1447
|
+
});
|
|
1448
|
+
expect(fieldIds(state, 'Name')).toEqual([]);
|
|
1449
|
+
runInAction(() => fields.set([{ id: 'value', header: 'Value', type: 'SHORT_TEXT' }]));
|
|
1450
|
+
expect(fieldIds(state, 'Name')).toEqual(['value']);
|
|
1451
|
+
});
|
|
1452
|
+
it.each([
|
|
1453
|
+
['14:30:00.000', true],
|
|
1454
|
+
['14:30:00x000', false],
|
|
1455
|
+
])('offers Time for %s only when the format fits', (value, fits) => {
|
|
1456
|
+
const { state } = setup({}, {
|
|
1457
|
+
fields: [{ id: 'clock', header: 'Clock', type: 'TIME' }],
|
|
1458
|
+
});
|
|
1459
|
+
runInAction(() => {
|
|
1460
|
+
state.csvRows = [[value]];
|
|
1461
|
+
});
|
|
1462
|
+
expect(fieldIds(state, 'Name')).toEqual(fits ? ['clock'] : []);
|
|
1463
|
+
});
|
|
1464
|
+
it('updates column compatibility when a new file is uploaded', async () => {
|
|
1465
|
+
const { state } = setup({}, { fields: FIELDS });
|
|
1466
|
+
const upload = (csv) => state.onFileSelected({
|
|
1467
|
+
name: 'data.csv',
|
|
1468
|
+
arrayBuffer: async () => new TextEncoder().encode(csv).buffer,
|
|
1469
|
+
});
|
|
1470
|
+
await upload('Value\n10\n20\n');
|
|
1471
|
+
expect(fieldIds(state, 'Value')).toContain('price');
|
|
1472
|
+
expect(fieldIds(state, 'Value')).not.toContain('launch');
|
|
1473
|
+
await upload('Value\n2026-01-01\n2026-01-02\n');
|
|
1474
|
+
expect(fieldIds(state, 'Value')).toContain('launch');
|
|
1475
|
+
expect(fieldIds(state, 'Value')).not.toContain('price');
|
|
1476
|
+
});
|
|
1477
|
+
it.each([
|
|
1478
|
+
[['10', '20', 'bad'], true],
|
|
1479
|
+
[['10', 'bad', 'worse'], false],
|
|
1480
|
+
])('offers Number only when two-thirds of the samples fit: %j', (values, fits) => {
|
|
1481
|
+
const state = setupColumns(['Value'], values.map((value) => [value]));
|
|
1482
|
+
expect(fieldIds(state, 'Value').includes('price')).toBe(fits);
|
|
1483
|
+
});
|
|
1484
|
+
});
|
|
1066
1485
|
describe('ImportState BI reporting', () => {
|
|
1067
1486
|
beforeEach(() => {
|
|
1068
1487
|
jest.useRealTimers();
|