@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,7 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _SchemaSource = require("../../../types/SchemaSource");
|
|
4
|
+
var _findExistingImportIds = require("../findExistingImportIds");
|
|
3
5
|
var _util = require("util");
|
|
4
6
|
var _mobx = require("mobx");
|
|
7
|
+
var _testkit = require("@wix/fe-essentials-standalone/testkit");
|
|
5
8
|
var _ImportState = require("../ImportState");
|
|
6
9
|
var _types = require("@wix/bex-utils/@wix/ambassador-hub-v1-job/types");
|
|
7
10
|
var _hubJobClient = require("../hubJobClient");
|
|
@@ -50,6 +53,7 @@ function setup(clientOverrides = {}, extra = {}) {
|
|
|
50
53
|
const translate = (key, params) => params ? `${key}:${JSON.stringify(params)}` : key;
|
|
51
54
|
const {
|
|
52
55
|
detectColumnType,
|
|
56
|
+
isValueCompatible,
|
|
53
57
|
config: configOverride,
|
|
54
58
|
collectionId,
|
|
55
59
|
destination,
|
|
@@ -105,6 +109,9 @@ function setup(clientOverrides = {}, extra = {}) {
|
|
|
105
109
|
})
|
|
106
110
|
};
|
|
107
111
|
const props = {
|
|
112
|
+
createFieldValidator: extra.createFieldValidator,
|
|
113
|
+
getExistingIds: extra.getExistingIds,
|
|
114
|
+
fieldsSource: extra.fieldsSource,
|
|
108
115
|
config,
|
|
109
116
|
httpClient: {
|
|
110
117
|
request: jest.fn()
|
|
@@ -118,6 +125,7 @@ function setup(clientOverrides = {}, extra = {}) {
|
|
|
118
125
|
showToast: showToast,
|
|
119
126
|
downloadCsv,
|
|
120
127
|
detectColumnType,
|
|
128
|
+
isValueCompatible,
|
|
121
129
|
...(reportBi && {
|
|
122
130
|
reportBi
|
|
123
131
|
})
|
|
@@ -153,6 +161,43 @@ describe('ImportState engine (data-hub job)', () => {
|
|
|
153
161
|
// real even if the preset defaults to fake ones.
|
|
154
162
|
jest.useRealTimers();
|
|
155
163
|
});
|
|
164
|
+
it('uploads duplicate-named columns to their own destination fields', async () => {
|
|
165
|
+
const {
|
|
166
|
+
state,
|
|
167
|
+
client
|
|
168
|
+
} = setup({}, {
|
|
169
|
+
fields: [{
|
|
170
|
+
id: 'price',
|
|
171
|
+
header: 'Number',
|
|
172
|
+
type: 'NUMBER'
|
|
173
|
+
}, {
|
|
174
|
+
id: 'otherPrice',
|
|
175
|
+
header: 'Number',
|
|
176
|
+
type: 'NUMBER'
|
|
177
|
+
}]
|
|
178
|
+
});
|
|
179
|
+
await state.onFileSelected({
|
|
180
|
+
name: 'duplicates.csv',
|
|
181
|
+
arrayBuffer: async () => new _util.TextEncoder().encode('Number,Number\n20,30\n40,50\n').buffer
|
|
182
|
+
});
|
|
183
|
+
expect(state.mappingByHeader).toEqual({
|
|
184
|
+
Number: {
|
|
185
|
+
kind: 'existing',
|
|
186
|
+
fieldId: 'price'
|
|
187
|
+
},
|
|
188
|
+
'Number (2)': {
|
|
189
|
+
kind: 'existing',
|
|
190
|
+
fieldId: 'otherPrice'
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
await state.startImport();
|
|
194
|
+
const uploaded = await new Promise(resolve => {
|
|
195
|
+
const reader = new FileReader();
|
|
196
|
+
reader.onload = () => resolve(String(reader.result));
|
|
197
|
+
reader.readAsText(client.uploadFile.mock.calls[0][0]);
|
|
198
|
+
});
|
|
199
|
+
expect(uploaded.split(/\r?\n/)).toEqual(['price,otherPrice', '20,30', '40,50']);
|
|
200
|
+
});
|
|
156
201
|
it('uploads the aligned file, submits a job, and reports success', async () => {
|
|
157
202
|
const {
|
|
158
203
|
state,
|
|
@@ -958,6 +1003,35 @@ describe('ImportState platformized destination (non–Wix-Data)', () => {
|
|
|
958
1003
|
// The merge runs alongside the data refetch on the success path.
|
|
959
1004
|
expect(collection.clearResultAndMoveToStart).toHaveBeenCalled();
|
|
960
1005
|
});
|
|
1006
|
+
it('validates new field settings before merging them on completion', async () => {
|
|
1007
|
+
const mergeFields = jest.fn();
|
|
1008
|
+
const updateSchema = async () => ({
|
|
1009
|
+
color: {
|
|
1010
|
+
id: 'color',
|
|
1011
|
+
displayName: 'Color',
|
|
1012
|
+
type: 'SHORT_TEXT',
|
|
1013
|
+
validation: {
|
|
1014
|
+
required: true
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
});
|
|
1018
|
+
const {
|
|
1019
|
+
state,
|
|
1020
|
+
downloadCsv
|
|
1021
|
+
} = setup({}, {
|
|
1022
|
+
updateSchema,
|
|
1023
|
+
mergeFields
|
|
1024
|
+
});
|
|
1025
|
+
withNewFieldMapping(state);
|
|
1026
|
+
(0, _mobx.runInAction)(() => {
|
|
1027
|
+
state.csvRows = [['Apple', ''], ['Pear', 'Green']];
|
|
1028
|
+
});
|
|
1029
|
+
await state.startImport();
|
|
1030
|
+
await state.downloadFailedRows();
|
|
1031
|
+
expect(state.result.failed).toBe(1);
|
|
1032
|
+
expect(downloadCsv).toHaveBeenCalledWith(expect.stringContaining('Apple,'), 'failed-data.csv');
|
|
1033
|
+
expect(mergeFields).toHaveBeenCalledTimes(1);
|
|
1034
|
+
});
|
|
961
1035
|
it('does not call mergeFields for a plain import (no new fields)', async () => {
|
|
962
1036
|
const mergeFields = jest.fn();
|
|
963
1037
|
const updateSchema = jest.fn(async () => RETURNED_FIELDS);
|
|
@@ -1574,6 +1648,483 @@ describe('ImportState items limit and completion hooks', () => {
|
|
|
1574
1648
|
expect(onImportCompleted).toHaveBeenCalledTimes(expectedCalls);
|
|
1575
1649
|
});
|
|
1576
1650
|
});
|
|
1651
|
+
describe('ImportState row validation', () => {
|
|
1652
|
+
_testkit.testkit.beforeAndAfter();
|
|
1653
|
+
beforeEach(() => jest.useRealTimers());
|
|
1654
|
+
it('uploads only valid rows and includes validation failures in the summary', async () => {
|
|
1655
|
+
const {
|
|
1656
|
+
state,
|
|
1657
|
+
client,
|
|
1658
|
+
downloadCsv
|
|
1659
|
+
} = setup({
|
|
1660
|
+
getJob: jest.fn(async () => ({
|
|
1661
|
+
...COMPLETED_JOB,
|
|
1662
|
+
progress: {
|
|
1663
|
+
itemsSuccessful: 1,
|
|
1664
|
+
itemsRejected: 0,
|
|
1665
|
+
current: 1
|
|
1666
|
+
}
|
|
1667
|
+
}))
|
|
1668
|
+
}, {
|
|
1669
|
+
createFieldValidator: () => raw => raw === 'Apple' ? ['Name is required'] : []
|
|
1670
|
+
});
|
|
1671
|
+
await state.startImport();
|
|
1672
|
+
const file = client.uploadFile.mock.calls[0][0];
|
|
1673
|
+
const content = await new Promise(resolve => {
|
|
1674
|
+
const reader = new FileReader();
|
|
1675
|
+
reader.onload = () => resolve(String(reader.result));
|
|
1676
|
+
reader.readAsText(file);
|
|
1677
|
+
});
|
|
1678
|
+
expect(content).toBe('name\r\nPear');
|
|
1679
|
+
expect(state.result).toEqual({
|
|
1680
|
+
succeeded: 1,
|
|
1681
|
+
created: 1,
|
|
1682
|
+
updated: 0,
|
|
1683
|
+
failed: 1
|
|
1684
|
+
});
|
|
1685
|
+
expect(state.totalCount).toBe(2);
|
|
1686
|
+
expect(state.processedCount).toBe(2);
|
|
1687
|
+
expect(state.importStatus.data).toEqual(state.result);
|
|
1688
|
+
expect(state.currentStep).toBe('summary');
|
|
1689
|
+
await state.downloadFailedRows();
|
|
1690
|
+
expect(client.listMovementLogs).not.toHaveBeenCalled();
|
|
1691
|
+
expect(downloadCsv).toHaveBeenCalledWith('cairo.import.failureReasonColumn,Name\r\nName is required,Apple', 'failed-data.csv');
|
|
1692
|
+
});
|
|
1693
|
+
it('reports and downloads all-invalid rows without starting a server job', async () => {
|
|
1694
|
+
const {
|
|
1695
|
+
state,
|
|
1696
|
+
client,
|
|
1697
|
+
downloadCsv
|
|
1698
|
+
} = setup({}, {
|
|
1699
|
+
createFieldValidator: () => raw => [`Invalid ${raw}`]
|
|
1700
|
+
});
|
|
1701
|
+
await state.startImport();
|
|
1702
|
+
await state.downloadFailedRows();
|
|
1703
|
+
expect(client.uploadFile).not.toHaveBeenCalled();
|
|
1704
|
+
expect(client.listMovementLogs).not.toHaveBeenCalled();
|
|
1705
|
+
expect(state.result).toEqual({
|
|
1706
|
+
succeeded: 0,
|
|
1707
|
+
created: 0,
|
|
1708
|
+
updated: 0,
|
|
1709
|
+
failed: 2
|
|
1710
|
+
});
|
|
1711
|
+
expect(state.currentStep).toBe('summary');
|
|
1712
|
+
expect(downloadCsv).toHaveBeenCalledWith('cairo.import.failureReasonColumn,Name\r\nInvalid Apple,Apple\r\nInvalid Pear,Pear', 'failed-data.csv');
|
|
1713
|
+
});
|
|
1714
|
+
it('combines local and server failures without shifting CSV row contents', async () => {
|
|
1715
|
+
const {
|
|
1716
|
+
state,
|
|
1717
|
+
downloadCsv
|
|
1718
|
+
} = setup({
|
|
1719
|
+
getJob: jest.fn(async () => ({
|
|
1720
|
+
...COMPLETED_JOB,
|
|
1721
|
+
status: _types.JobStatus.PARTIALLY_SUCCESSFUL,
|
|
1722
|
+
progress: {
|
|
1723
|
+
itemsSuccessful: 1,
|
|
1724
|
+
itemsRejected: 1,
|
|
1725
|
+
current: 2
|
|
1726
|
+
}
|
|
1727
|
+
})),
|
|
1728
|
+
listMovementLogs: jest.fn(async () => ({
|
|
1729
|
+
logs: [{
|
|
1730
|
+
sourceItemId: 'row 2',
|
|
1731
|
+
failure: {
|
|
1732
|
+
description: 'Server rejected Pear'
|
|
1733
|
+
}
|
|
1734
|
+
}],
|
|
1735
|
+
pagingMetadata: {
|
|
1736
|
+
hasNext: false
|
|
1737
|
+
}
|
|
1738
|
+
}))
|
|
1739
|
+
}, {
|
|
1740
|
+
createFieldValidator: () => raw => raw === 'Apple' ? ['Invalid Apple'] : []
|
|
1741
|
+
});
|
|
1742
|
+
(0, _mobx.runInAction)(() => {
|
|
1743
|
+
state.csvRows.push(['Mango']);
|
|
1744
|
+
});
|
|
1745
|
+
await state.startImport();
|
|
1746
|
+
await state.downloadFailedRows();
|
|
1747
|
+
expect(state.result).toEqual({
|
|
1748
|
+
succeeded: 1,
|
|
1749
|
+
created: 1,
|
|
1750
|
+
updated: 0,
|
|
1751
|
+
failed: 2
|
|
1752
|
+
});
|
|
1753
|
+
expect(downloadCsv).toHaveBeenCalledWith('cairo.import.failureReasonColumn,Name\r\nInvalid Apple,Apple\r\nServer rejected Pear,Pear', 'failed-data.csv');
|
|
1754
|
+
});
|
|
1755
|
+
it('checks empty required values for manually configured destinations', async () => {
|
|
1756
|
+
const {
|
|
1757
|
+
state,
|
|
1758
|
+
client,
|
|
1759
|
+
downloadCsv
|
|
1760
|
+
} = setup({}, {
|
|
1761
|
+
destination: {
|
|
1762
|
+
storesCatalogProducts: {}
|
|
1763
|
+
},
|
|
1764
|
+
fields: [{
|
|
1765
|
+
id: 'name',
|
|
1766
|
+
header: 'Name',
|
|
1767
|
+
required: true
|
|
1768
|
+
}]
|
|
1769
|
+
});
|
|
1770
|
+
(0, _mobx.runInAction)(() => {
|
|
1771
|
+
state.csvRows = [['']];
|
|
1772
|
+
});
|
|
1773
|
+
await state.startImport();
|
|
1774
|
+
await state.downloadFailedRows();
|
|
1775
|
+
expect(client.submitImportJob).not.toHaveBeenCalled();
|
|
1776
|
+
expect(state.result.failed).toBe(1);
|
|
1777
|
+
expect(downloadCsv).toHaveBeenCalledWith(expect.stringContaining('cairo.editableTable.field.validation.required'), 'failed-data.csv');
|
|
1778
|
+
});
|
|
1779
|
+
it('skips existing and repeated IDs before validating their values', async () => {
|
|
1780
|
+
const find = jest.fn(async () => ({
|
|
1781
|
+
items: [{
|
|
1782
|
+
id: 'existing'
|
|
1783
|
+
}],
|
|
1784
|
+
total: 1
|
|
1785
|
+
}));
|
|
1786
|
+
const source = (0, _SchemaSource.defineSchemaSource)({
|
|
1787
|
+
collectionId: 'Movies',
|
|
1788
|
+
loadSchema: async () => ({
|
|
1789
|
+
schema: {
|
|
1790
|
+
id: 'Movies',
|
|
1791
|
+
idField: 'id',
|
|
1792
|
+
displayField: 'id',
|
|
1793
|
+
fields: {}
|
|
1794
|
+
},
|
|
1795
|
+
backend: {
|
|
1796
|
+
find
|
|
1797
|
+
}
|
|
1798
|
+
})
|
|
1799
|
+
});
|
|
1800
|
+
const {
|
|
1801
|
+
state,
|
|
1802
|
+
client
|
|
1803
|
+
} = setup({}, {
|
|
1804
|
+
idFieldKey: 'id',
|
|
1805
|
+
fields: [{
|
|
1806
|
+
id: 'id',
|
|
1807
|
+
header: 'ID'
|
|
1808
|
+
}, {
|
|
1809
|
+
id: 'name',
|
|
1810
|
+
header: 'Name',
|
|
1811
|
+
required: true
|
|
1812
|
+
}],
|
|
1813
|
+
getExistingIds: (ids, signal) => (0, _findExistingImportIds.findExistingImportIds)(source, ids, signal)
|
|
1814
|
+
});
|
|
1815
|
+
(0, _mobx.runInAction)(() => {
|
|
1816
|
+
state.csvHeaders = ['ID', 'Name'];
|
|
1817
|
+
state.csvRows = [['existing', ''], ['new', 'Valid'], ['new', ''], ['invalid', '']];
|
|
1818
|
+
});
|
|
1819
|
+
state.setMapping('ID', 'id');
|
|
1820
|
+
state.setWritePolicy(_types.WritePolicy.SKIP_EXISTING);
|
|
1821
|
+
await state.startImport();
|
|
1822
|
+
expect(state.result.failed).toBe(1);
|
|
1823
|
+
expect(state.totalCount).toBe(2);
|
|
1824
|
+
const file = client.uploadFile.mock.calls[0][0];
|
|
1825
|
+
const content = await new Promise(resolve => {
|
|
1826
|
+
const reader = new FileReader();
|
|
1827
|
+
reader.onload = () => resolve(String(reader.result));
|
|
1828
|
+
reader.readAsText(file);
|
|
1829
|
+
});
|
|
1830
|
+
expect(content).toBe('id,name\r\nnew,Valid');
|
|
1831
|
+
expect(find).toHaveBeenCalledWith(expect.objectContaining({
|
|
1832
|
+
filters: {
|
|
1833
|
+
id: [{
|
|
1834
|
+
id: 'existing'
|
|
1835
|
+
}, {
|
|
1836
|
+
id: 'new'
|
|
1837
|
+
}, {
|
|
1838
|
+
id: 'invalid'
|
|
1839
|
+
}]
|
|
1840
|
+
},
|
|
1841
|
+
rawSearch: '',
|
|
1842
|
+
offset: 0,
|
|
1843
|
+
limit: 3
|
|
1844
|
+
}));
|
|
1845
|
+
});
|
|
1846
|
+
it('passes source constraints and required import overrides through the same validator', async () => {
|
|
1847
|
+
const sourceField = {
|
|
1848
|
+
id: 'name',
|
|
1849
|
+
displayName: 'Name',
|
|
1850
|
+
type: 'SHORT_TEXT',
|
|
1851
|
+
validation: {
|
|
1852
|
+
required: false,
|
|
1853
|
+
pattern: '^OK$'
|
|
1854
|
+
}
|
|
1855
|
+
};
|
|
1856
|
+
const {
|
|
1857
|
+
state
|
|
1858
|
+
} = setup({}, {
|
|
1859
|
+
fields: [{
|
|
1860
|
+
id: 'name',
|
|
1861
|
+
header: 'Name',
|
|
1862
|
+
required: true
|
|
1863
|
+
}],
|
|
1864
|
+
fieldsSource: {
|
|
1865
|
+
fields: [sourceField],
|
|
1866
|
+
filterableFields: [],
|
|
1867
|
+
manageableFields: [],
|
|
1868
|
+
load: async () => {},
|
|
1869
|
+
columnIdForField: field => field.id,
|
|
1870
|
+
queryFieldId: field => field.id,
|
|
1871
|
+
getCellValue: () => undefined
|
|
1872
|
+
},
|
|
1873
|
+
createFieldValidator: field => raw => {
|
|
1874
|
+
var _field$validation, _field$validation2;
|
|
1875
|
+
return (_field$validation = field.validation) != null && _field$validation.required && raw !== 'OK' && ((_field$validation2 = field.validation) == null ? void 0 : _field$validation2.pattern) === '^OK$' ? ['Name must match'] : [];
|
|
1876
|
+
}
|
|
1877
|
+
});
|
|
1878
|
+
await state.startImport();
|
|
1879
|
+
expect(state.result.failed).toBe(2);
|
|
1880
|
+
});
|
|
1881
|
+
it('does not validate CSV columns the user skipped', async () => {
|
|
1882
|
+
const {
|
|
1883
|
+
state,
|
|
1884
|
+
client
|
|
1885
|
+
} = setup({}, {
|
|
1886
|
+
fields: [{
|
|
1887
|
+
id: 'name',
|
|
1888
|
+
header: 'Name'
|
|
1889
|
+
}, {
|
|
1890
|
+
id: 'extra',
|
|
1891
|
+
header: 'Extra'
|
|
1892
|
+
}],
|
|
1893
|
+
createFieldValidator: () => raw => raw === 'invalid' ? ['Invalid value'] : []
|
|
1894
|
+
});
|
|
1895
|
+
(0, _mobx.runInAction)(() => {
|
|
1896
|
+
state.csvHeaders = ['Name', 'Extra'];
|
|
1897
|
+
state.csvRows = [['Apple', 'invalid'], ['Pear', 'invalid']];
|
|
1898
|
+
});
|
|
1899
|
+
state.setMapping('Extra', null);
|
|
1900
|
+
await state.startImport();
|
|
1901
|
+
expect(state.result.failed).toBe(0);
|
|
1902
|
+
expect(client.uploadFile).toHaveBeenCalledTimes(1);
|
|
1903
|
+
});
|
|
1904
|
+
it('clears previous validation failures when retrying', async () => {
|
|
1905
|
+
let invalid = true;
|
|
1906
|
+
const {
|
|
1907
|
+
state,
|
|
1908
|
+
client
|
|
1909
|
+
} = setup({}, {
|
|
1910
|
+
createFieldValidator: () => raw => invalid ? [`Invalid ${raw}`] : []
|
|
1911
|
+
});
|
|
1912
|
+
await state.startImport();
|
|
1913
|
+
invalid = false;
|
|
1914
|
+
await state.startImport();
|
|
1915
|
+
expect(state.result.failed).toBe(0);
|
|
1916
|
+
expect(state.result.succeeded).toBe(2);
|
|
1917
|
+
expect(client.uploadFile).toHaveBeenCalledTimes(1);
|
|
1918
|
+
});
|
|
1919
|
+
});
|
|
1920
|
+
describe('ImportState mapping dropdown fields (CAIRO-4392)', () => {
|
|
1921
|
+
_testkit.testkit.beforeAndAfter();
|
|
1922
|
+
const FIELDS = [{
|
|
1923
|
+
id: 'title',
|
|
1924
|
+
header: 'Title',
|
|
1925
|
+
type: 'SHORT_TEXT'
|
|
1926
|
+
}, {
|
|
1927
|
+
id: 'price',
|
|
1928
|
+
header: 'Price',
|
|
1929
|
+
type: 'NUMBER'
|
|
1930
|
+
}, {
|
|
1931
|
+
id: 'launch',
|
|
1932
|
+
header: 'Launch Date',
|
|
1933
|
+
type: 'DATE'
|
|
1934
|
+
}, {
|
|
1935
|
+
id: 'website',
|
|
1936
|
+
header: 'Website',
|
|
1937
|
+
type: 'URL'
|
|
1938
|
+
}, {
|
|
1939
|
+
id: 'tags',
|
|
1940
|
+
header: 'Tags',
|
|
1941
|
+
type: 'ARRAY'
|
|
1942
|
+
}, {
|
|
1943
|
+
id: 'reference',
|
|
1944
|
+
header: 'Reference',
|
|
1945
|
+
type: 'REFERENCE'
|
|
1946
|
+
}, {
|
|
1947
|
+
id: 'legacy',
|
|
1948
|
+
header: 'Legacy'
|
|
1949
|
+
}];
|
|
1950
|
+
function setupColumns(headers, rows) {
|
|
1951
|
+
const {
|
|
1952
|
+
state
|
|
1953
|
+
} = setup({}, {
|
|
1954
|
+
fields: FIELDS
|
|
1955
|
+
});
|
|
1956
|
+
(0, _mobx.runInAction)(() => {
|
|
1957
|
+
state.csvHeaders = headers;
|
|
1958
|
+
state.csvRows = rows;
|
|
1959
|
+
state.mappingByHeader = {};
|
|
1960
|
+
});
|
|
1961
|
+
return state;
|
|
1962
|
+
}
|
|
1963
|
+
const fieldIds = (state, header) => state.fieldsForHeader(header).map(field => field.id);
|
|
1964
|
+
it('detects a sparse column from its non-empty values', () => {
|
|
1965
|
+
const state = setupColumns(['Price'], [['10'], [''], ['20'], [''], ['30'], ['']]);
|
|
1966
|
+
expect(fieldIds(state, 'Price')).toContain('price');
|
|
1967
|
+
expect(fieldIds(state, 'Price')).not.toContain('launch');
|
|
1968
|
+
});
|
|
1969
|
+
it('offers only the fields that fit the column data', () => {
|
|
1970
|
+
const state = setupColumns(['Price', 'Launch Date', 'Notes'], [['10', '2026-01-01', 'first'], ['20', '2026-01-02', 'second']]);
|
|
1971
|
+
expect(fieldIds(state, 'Price')).toEqual(['title', 'price', 'legacy']);
|
|
1972
|
+
expect(fieldIds(state, 'Launch Date')).toEqual(['title', 'launch', 'legacy']);
|
|
1973
|
+
expect(fieldIds(state, 'Notes')).toEqual(['title', 'reference', 'legacy']);
|
|
1974
|
+
});
|
|
1975
|
+
it('keeps the field a column is mapped to even when it does not fit', () => {
|
|
1976
|
+
const state = setupColumns(['Score'], [['high'], ['low']]);
|
|
1977
|
+
state.setMapping('Score', 'price');
|
|
1978
|
+
expect(fieldIds(state, 'Score')).toContain('price');
|
|
1979
|
+
expect(fieldIds(state, 'Score')).not.toContain('launch');
|
|
1980
|
+
});
|
|
1981
|
+
it('offers every field for a column with no values', () => {
|
|
1982
|
+
const state = setupColumns(['Empty'], [[''], ['']]);
|
|
1983
|
+
expect(fieldIds(state, 'Empty')).toEqual(FIELDS.map(field => field.id));
|
|
1984
|
+
});
|
|
1985
|
+
it('keeps custom fields available but hides unknown fields even when selected', () => {
|
|
1986
|
+
const {
|
|
1987
|
+
state
|
|
1988
|
+
} = setup({}, {
|
|
1989
|
+
fields: [{
|
|
1990
|
+
id: 'rating',
|
|
1991
|
+
header: 'Rating',
|
|
1992
|
+
type: 'CUSTOM'
|
|
1993
|
+
}, {
|
|
1994
|
+
id: 'unknown',
|
|
1995
|
+
header: 'Unknown',
|
|
1996
|
+
type: 'UNKNOWN'
|
|
1997
|
+
}]
|
|
1998
|
+
});
|
|
1999
|
+
state.setMapping('Name', 'unknown');
|
|
2000
|
+
expect(fieldIds(state, 'Name')).toEqual(['rating']);
|
|
2001
|
+
});
|
|
2002
|
+
it('does not auto-map an unknown field with a matching name', async () => {
|
|
2003
|
+
const {
|
|
2004
|
+
state
|
|
2005
|
+
} = setup({}, {
|
|
2006
|
+
fields: [{
|
|
2007
|
+
id: 'status',
|
|
2008
|
+
header: 'Status',
|
|
2009
|
+
type: 'UNKNOWN'
|
|
2010
|
+
}]
|
|
2011
|
+
});
|
|
2012
|
+
await state.onFileSelected({
|
|
2013
|
+
name: 'status.csv',
|
|
2014
|
+
arrayBuffer: async () => new _util.TextEncoder().encode('Status\nPUBLISHED\n').buffer
|
|
2015
|
+
});
|
|
2016
|
+
expect(state.mappingByHeader.Status).toBeNull();
|
|
2017
|
+
expect(fieldIds(state, 'Status')).toEqual([]);
|
|
2018
|
+
});
|
|
2019
|
+
it('separates duplicate headers without colliding with existing suffixes', async () => {
|
|
2020
|
+
const {
|
|
2021
|
+
state
|
|
2022
|
+
} = setup({}, {
|
|
2023
|
+
fields: FIELDS
|
|
2024
|
+
});
|
|
2025
|
+
await state.onFileSelected({
|
|
2026
|
+
name: 'duplicates.csv',
|
|
2027
|
+
arrayBuffer: async () => new _util.TextEncoder().encode('Price,Price,Price (2)\n20,,hello\n').buffer
|
|
2028
|
+
});
|
|
2029
|
+
expect(state.csvHeaders).toEqual(['Price', 'Price (3)', 'Price (2)']);
|
|
2030
|
+
expect(fieldIds(state, 'Price')).not.toContain('reference');
|
|
2031
|
+
expect(fieldIds(state, 'Price (3)')).toContain('reference');
|
|
2032
|
+
state.setMapping('Price (3)', 'reference');
|
|
2033
|
+
expect(state.mappingByHeader.Price).toEqual({
|
|
2034
|
+
kind: 'existing',
|
|
2035
|
+
fieldId: 'price'
|
|
2036
|
+
});
|
|
2037
|
+
expect(state.mappingByHeader['Price (3)']).toEqual({
|
|
2038
|
+
kind: 'existing',
|
|
2039
|
+
fieldId: 'reference'
|
|
2040
|
+
});
|
|
2041
|
+
});
|
|
2042
|
+
it('reuses format checks when mappings change and refreshes them for a new upload', async () => {
|
|
2043
|
+
const isValueCompatible = jest.fn(() => undefined);
|
|
2044
|
+
const {
|
|
2045
|
+
state
|
|
2046
|
+
} = setup({}, {
|
|
2047
|
+
fields: FIELDS,
|
|
2048
|
+
isValueCompatible
|
|
2049
|
+
});
|
|
2050
|
+
expect(fieldIds(state, 'Name')).toContain('title');
|
|
2051
|
+
const initialChecks = isValueCompatible.mock.calls.length;
|
|
2052
|
+
expect(initialChecks).toBeGreaterThan(0);
|
|
2053
|
+
state.setMapping('Name', 'price');
|
|
2054
|
+
expect(fieldIds(state, 'Name')).toContain('price');
|
|
2055
|
+
state.setMapping('Name', null);
|
|
2056
|
+
expect(fieldIds(state, 'Name')).not.toContain('price');
|
|
2057
|
+
expect(isValueCompatible).toHaveBeenCalledTimes(initialChecks);
|
|
2058
|
+
await state.onFileSelected({
|
|
2059
|
+
name: 'numbers.csv',
|
|
2060
|
+
arrayBuffer: async () => new _util.TextEncoder().encode('Name\n10\n20\n').buffer
|
|
2061
|
+
});
|
|
2062
|
+
expect(fieldIds(state, 'Name')).toContain('price');
|
|
2063
|
+
expect(isValueCompatible.mock.calls.length).toBeGreaterThan(initialChecks);
|
|
2064
|
+
});
|
|
2065
|
+
it('refreshes compatibility when the source fields change', () => {
|
|
2066
|
+
const fields = _mobx.observable.box([{
|
|
2067
|
+
id: 'value',
|
|
2068
|
+
header: 'Value',
|
|
2069
|
+
type: 'NUMBER'
|
|
2070
|
+
}]);
|
|
2071
|
+
const {
|
|
2072
|
+
state
|
|
2073
|
+
} = setup({}, {
|
|
2074
|
+
config: {
|
|
2075
|
+
get fields() {
|
|
2076
|
+
return fields.get();
|
|
2077
|
+
},
|
|
2078
|
+
dataSource: {
|
|
2079
|
+
collectionId: 'Movies'
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
});
|
|
2083
|
+
expect(fieldIds(state, 'Name')).toEqual([]);
|
|
2084
|
+
(0, _mobx.runInAction)(() => fields.set([{
|
|
2085
|
+
id: 'value',
|
|
2086
|
+
header: 'Value',
|
|
2087
|
+
type: 'SHORT_TEXT'
|
|
2088
|
+
}]));
|
|
2089
|
+
expect(fieldIds(state, 'Name')).toEqual(['value']);
|
|
2090
|
+
});
|
|
2091
|
+
it.each([['14:30:00.000', true], ['14:30:00x000', false]])('offers Time for %s only when the format fits', (value, fits) => {
|
|
2092
|
+
const {
|
|
2093
|
+
state
|
|
2094
|
+
} = setup({}, {
|
|
2095
|
+
fields: [{
|
|
2096
|
+
id: 'clock',
|
|
2097
|
+
header: 'Clock',
|
|
2098
|
+
type: 'TIME'
|
|
2099
|
+
}]
|
|
2100
|
+
});
|
|
2101
|
+
(0, _mobx.runInAction)(() => {
|
|
2102
|
+
state.csvRows = [[value]];
|
|
2103
|
+
});
|
|
2104
|
+
expect(fieldIds(state, 'Name')).toEqual(fits ? ['clock'] : []);
|
|
2105
|
+
});
|
|
2106
|
+
it('updates column compatibility when a new file is uploaded', async () => {
|
|
2107
|
+
const {
|
|
2108
|
+
state
|
|
2109
|
+
} = setup({}, {
|
|
2110
|
+
fields: FIELDS
|
|
2111
|
+
});
|
|
2112
|
+
const upload = csv => state.onFileSelected({
|
|
2113
|
+
name: 'data.csv',
|
|
2114
|
+
arrayBuffer: async () => new _util.TextEncoder().encode(csv).buffer
|
|
2115
|
+
});
|
|
2116
|
+
await upload('Value\n10\n20\n');
|
|
2117
|
+
expect(fieldIds(state, 'Value')).toContain('price');
|
|
2118
|
+
expect(fieldIds(state, 'Value')).not.toContain('launch');
|
|
2119
|
+
await upload('Value\n2026-01-01\n2026-01-02\n');
|
|
2120
|
+
expect(fieldIds(state, 'Value')).toContain('launch');
|
|
2121
|
+
expect(fieldIds(state, 'Value')).not.toContain('price');
|
|
2122
|
+
});
|
|
2123
|
+
it.each([[['10', '20', 'bad'], true], [['10', 'bad', 'worse'], false]])('offers Number only when two-thirds of the samples fit: %j', (values, fits) => {
|
|
2124
|
+
const state = setupColumns(['Value'], values.map(value => [value]));
|
|
2125
|
+
expect(fieldIds(state, 'Value').includes('price')).toBe(fits);
|
|
2126
|
+
});
|
|
2127
|
+
});
|
|
1577
2128
|
describe('ImportState BI reporting', () => {
|
|
1578
2129
|
beforeEach(() => {
|
|
1579
2130
|
jest.useRealTimers();
|