@wix/bex-core 2.400.0 → 2.401.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.
Files changed (51) hide show
  1. package/dist/cjs/hooks/useImport.js +2 -0
  2. package/dist/cjs/hooks/useImport.js.map +1 -1
  3. package/dist/cjs/state/ImportState/ImportState.js +67 -17
  4. package/dist/cjs/state/ImportState/ImportState.js.map +1 -1
  5. package/dist/cjs/state/ImportState/__tests__/ImportState.engine.spec.js +248 -0
  6. package/dist/cjs/state/ImportState/__tests__/ImportState.engine.spec.js.map +1 -1
  7. package/dist/cjs/state/ImportState/__tests__/buildImportConfig.spec.js +30 -4
  8. package/dist/cjs/state/ImportState/__tests__/buildImportConfig.spec.js.map +1 -1
  9. package/dist/cjs/state/ImportState/__tests__/importFieldCompatibility.spec.js +91 -0
  10. package/dist/cjs/state/ImportState/__tests__/importFieldCompatibility.spec.js.map +1 -0
  11. package/dist/cjs/state/ImportState/detectFieldType.js +3 -3
  12. package/dist/cjs/state/ImportState/detectFieldType.js.map +1 -1
  13. package/dist/cjs/state/ImportState/importFieldCompatibility.js +92 -0
  14. package/dist/cjs/state/ImportState/importFieldCompatibility.js.map +1 -0
  15. package/dist/cjs/state/ImportState/importSampling.js +6 -0
  16. package/dist/cjs/state/ImportState/importSampling.js.map +1 -0
  17. package/dist/cjs/state/ImportState/index.js.map +1 -1
  18. package/dist/cjs/types/Field.js.map +1 -1
  19. package/dist/esm/hooks/useImport.js +2 -1
  20. package/dist/esm/hooks/useImport.js.map +1 -1
  21. package/dist/esm/state/ImportState/ImportState.js +68 -17
  22. package/dist/esm/state/ImportState/ImportState.js.map +1 -1
  23. package/dist/esm/state/ImportState/__tests__/ImportState.engine.spec.js +187 -2
  24. package/dist/esm/state/ImportState/__tests__/ImportState.engine.spec.js.map +1 -1
  25. package/dist/esm/state/ImportState/__tests__/buildImportConfig.spec.js +32 -4
  26. package/dist/esm/state/ImportState/__tests__/buildImportConfig.spec.js.map +1 -1
  27. package/dist/esm/state/ImportState/__tests__/importFieldCompatibility.spec.js +99 -0
  28. package/dist/esm/state/ImportState/__tests__/importFieldCompatibility.spec.js.map +1 -0
  29. package/dist/esm/state/ImportState/detectFieldType.js +3 -3
  30. package/dist/esm/state/ImportState/detectFieldType.js.map +1 -1
  31. package/dist/esm/state/ImportState/importFieldCompatibility.js +102 -0
  32. package/dist/esm/state/ImportState/importFieldCompatibility.js.map +1 -0
  33. package/dist/esm/state/ImportState/importSampling.js +2 -0
  34. package/dist/esm/state/ImportState/importSampling.js.map +1 -0
  35. package/dist/esm/state/ImportState/index.js.map +1 -1
  36. package/dist/types/hooks/useImport.d.ts +2 -1
  37. package/dist/types/hooks/useImport.d.ts.map +1 -1
  38. package/dist/types/state/ImportState/ImportState.d.ts +10 -1
  39. package/dist/types/state/ImportState/ImportState.d.ts.map +1 -1
  40. package/dist/types/state/ImportState/__tests__/importFieldCompatibility.spec.d.ts +2 -0
  41. package/dist/types/state/ImportState/__tests__/importFieldCompatibility.spec.d.ts.map +1 -0
  42. package/dist/types/state/ImportState/detectFieldType.d.ts.map +1 -1
  43. package/dist/types/state/ImportState/importFieldCompatibility.d.ts +4 -0
  44. package/dist/types/state/ImportState/importFieldCompatibility.d.ts.map +1 -0
  45. package/dist/types/state/ImportState/importSampling.d.ts +2 -0
  46. package/dist/types/state/ImportState/importSampling.d.ts.map +1 -0
  47. package/dist/types/state/ImportState/index.d.ts +1 -0
  48. package/dist/types/state/ImportState/index.d.ts.map +1 -1
  49. package/dist/types/types/Field.d.ts +4 -0
  50. package/dist/types/types/Field.d.ts.map +1 -1
  51. package/package.json +4 -4
@@ -2,6 +2,7 @@
2
2
 
3
3
  var _util = require("util");
4
4
  var _mobx = require("mobx");
5
+ var _testkit = require("@wix/fe-essentials-standalone/testkit");
5
6
  var _ImportState = require("../ImportState");
6
7
  var _types = require("@wix/bex-utils/@wix/ambassador-hub-v1-job/types");
7
8
  var _hubJobClient = require("../hubJobClient");
@@ -50,6 +51,7 @@ function setup(clientOverrides = {}, extra = {}) {
50
51
  const translate = (key, params) => params ? `${key}:${JSON.stringify(params)}` : key;
51
52
  const {
52
53
  detectColumnType,
54
+ isValueCompatible,
53
55
  config: configOverride,
54
56
  collectionId,
55
57
  destination,
@@ -118,6 +120,7 @@ function setup(clientOverrides = {}, extra = {}) {
118
120
  showToast: showToast,
119
121
  downloadCsv,
120
122
  detectColumnType,
123
+ isValueCompatible,
121
124
  ...(reportBi && {
122
125
  reportBi
123
126
  })
@@ -153,6 +156,43 @@ describe('ImportState engine (data-hub job)', () => {
153
156
  // real even if the preset defaults to fake ones.
154
157
  jest.useRealTimers();
155
158
  });
159
+ it('uploads duplicate-named columns to their own destination fields', async () => {
160
+ const {
161
+ state,
162
+ client
163
+ } = setup({}, {
164
+ fields: [{
165
+ id: 'price',
166
+ header: 'Number',
167
+ type: 'NUMBER'
168
+ }, {
169
+ id: 'otherPrice',
170
+ header: 'Number',
171
+ type: 'NUMBER'
172
+ }]
173
+ });
174
+ await state.onFileSelected({
175
+ name: 'duplicates.csv',
176
+ arrayBuffer: async () => new _util.TextEncoder().encode('Number,Number\n20,30\n40,50\n').buffer
177
+ });
178
+ expect(state.mappingByHeader).toEqual({
179
+ Number: {
180
+ kind: 'existing',
181
+ fieldId: 'price'
182
+ },
183
+ 'Number (2)': {
184
+ kind: 'existing',
185
+ fieldId: 'otherPrice'
186
+ }
187
+ });
188
+ await state.startImport();
189
+ const uploaded = await new Promise(resolve => {
190
+ const reader = new FileReader();
191
+ reader.onload = () => resolve(String(reader.result));
192
+ reader.readAsText(client.uploadFile.mock.calls[0][0]);
193
+ });
194
+ expect(uploaded.split(/\r?\n/)).toEqual(['price,otherPrice', '20,30', '40,50']);
195
+ });
156
196
  it('uploads the aligned file, submits a job, and reports success', async () => {
157
197
  const {
158
198
  state,
@@ -1574,6 +1614,214 @@ describe('ImportState items limit and completion hooks', () => {
1574
1614
  expect(onImportCompleted).toHaveBeenCalledTimes(expectedCalls);
1575
1615
  });
1576
1616
  });
1617
+ describe('ImportState mapping dropdown fields (CAIRO-4392)', () => {
1618
+ _testkit.testkit.beforeAndAfter();
1619
+ const FIELDS = [{
1620
+ id: 'title',
1621
+ header: 'Title',
1622
+ type: 'SHORT_TEXT'
1623
+ }, {
1624
+ id: 'price',
1625
+ header: 'Price',
1626
+ type: 'NUMBER'
1627
+ }, {
1628
+ id: 'launch',
1629
+ header: 'Launch Date',
1630
+ type: 'DATE'
1631
+ }, {
1632
+ id: 'website',
1633
+ header: 'Website',
1634
+ type: 'URL'
1635
+ }, {
1636
+ id: 'tags',
1637
+ header: 'Tags',
1638
+ type: 'ARRAY'
1639
+ }, {
1640
+ id: 'reference',
1641
+ header: 'Reference',
1642
+ type: 'REFERENCE'
1643
+ }, {
1644
+ id: 'legacy',
1645
+ header: 'Legacy'
1646
+ }];
1647
+ function setupColumns(headers, rows) {
1648
+ const {
1649
+ state
1650
+ } = setup({}, {
1651
+ fields: FIELDS
1652
+ });
1653
+ (0, _mobx.runInAction)(() => {
1654
+ state.csvHeaders = headers;
1655
+ state.csvRows = rows;
1656
+ state.mappingByHeader = {};
1657
+ });
1658
+ return state;
1659
+ }
1660
+ const fieldIds = (state, header) => state.fieldsForHeader(header).map(field => field.id);
1661
+ it('detects a sparse column from its non-empty values', () => {
1662
+ const state = setupColumns(['Price'], [['10'], [''], ['20'], [''], ['30'], ['']]);
1663
+ expect(fieldIds(state, 'Price')).toContain('price');
1664
+ expect(fieldIds(state, 'Price')).not.toContain('launch');
1665
+ });
1666
+ it('offers only the fields that fit the column data', () => {
1667
+ const state = setupColumns(['Price', 'Launch Date', 'Notes'], [['10', '2026-01-01', 'first'], ['20', '2026-01-02', 'second']]);
1668
+ expect(fieldIds(state, 'Price')).toEqual(['title', 'price', 'legacy']);
1669
+ expect(fieldIds(state, 'Launch Date')).toEqual(['title', 'launch', 'legacy']);
1670
+ expect(fieldIds(state, 'Notes')).toEqual(['title', 'reference', 'legacy']);
1671
+ });
1672
+ it('keeps the field a column is mapped to even when it does not fit', () => {
1673
+ const state = setupColumns(['Score'], [['high'], ['low']]);
1674
+ state.setMapping('Score', 'price');
1675
+ expect(fieldIds(state, 'Score')).toContain('price');
1676
+ expect(fieldIds(state, 'Score')).not.toContain('launch');
1677
+ });
1678
+ it('offers every field for a column with no values', () => {
1679
+ const state = setupColumns(['Empty'], [[''], ['']]);
1680
+ expect(fieldIds(state, 'Empty')).toEqual(FIELDS.map(field => field.id));
1681
+ });
1682
+ it('keeps custom fields available but hides unknown fields even when selected', () => {
1683
+ const {
1684
+ state
1685
+ } = setup({}, {
1686
+ fields: [{
1687
+ id: 'rating',
1688
+ header: 'Rating',
1689
+ type: 'CUSTOM'
1690
+ }, {
1691
+ id: 'unknown',
1692
+ header: 'Unknown',
1693
+ type: 'UNKNOWN'
1694
+ }]
1695
+ });
1696
+ state.setMapping('Name', 'unknown');
1697
+ expect(fieldIds(state, 'Name')).toEqual(['rating']);
1698
+ });
1699
+ it('does not auto-map an unknown field with a matching name', async () => {
1700
+ const {
1701
+ state
1702
+ } = setup({}, {
1703
+ fields: [{
1704
+ id: 'status',
1705
+ header: 'Status',
1706
+ type: 'UNKNOWN'
1707
+ }]
1708
+ });
1709
+ await state.onFileSelected({
1710
+ name: 'status.csv',
1711
+ arrayBuffer: async () => new _util.TextEncoder().encode('Status\nPUBLISHED\n').buffer
1712
+ });
1713
+ expect(state.mappingByHeader.Status).toBeNull();
1714
+ expect(fieldIds(state, 'Status')).toEqual([]);
1715
+ });
1716
+ it('separates duplicate headers without colliding with existing suffixes', async () => {
1717
+ const {
1718
+ state
1719
+ } = setup({}, {
1720
+ fields: FIELDS
1721
+ });
1722
+ await state.onFileSelected({
1723
+ name: 'duplicates.csv',
1724
+ arrayBuffer: async () => new _util.TextEncoder().encode('Price,Price,Price (2)\n20,,hello\n').buffer
1725
+ });
1726
+ expect(state.csvHeaders).toEqual(['Price', 'Price (3)', 'Price (2)']);
1727
+ expect(fieldIds(state, 'Price')).not.toContain('reference');
1728
+ expect(fieldIds(state, 'Price (3)')).toContain('reference');
1729
+ state.setMapping('Price (3)', 'reference');
1730
+ expect(state.mappingByHeader.Price).toEqual({
1731
+ kind: 'existing',
1732
+ fieldId: 'price'
1733
+ });
1734
+ expect(state.mappingByHeader['Price (3)']).toEqual({
1735
+ kind: 'existing',
1736
+ fieldId: 'reference'
1737
+ });
1738
+ });
1739
+ it('reuses format checks when mappings change and refreshes them for a new upload', async () => {
1740
+ const isValueCompatible = jest.fn(() => undefined);
1741
+ const {
1742
+ state
1743
+ } = setup({}, {
1744
+ fields: FIELDS,
1745
+ isValueCompatible
1746
+ });
1747
+ expect(fieldIds(state, 'Name')).toContain('title');
1748
+ const initialChecks = isValueCompatible.mock.calls.length;
1749
+ expect(initialChecks).toBeGreaterThan(0);
1750
+ state.setMapping('Name', 'price');
1751
+ expect(fieldIds(state, 'Name')).toContain('price');
1752
+ state.setMapping('Name', null);
1753
+ expect(fieldIds(state, 'Name')).not.toContain('price');
1754
+ expect(isValueCompatible).toHaveBeenCalledTimes(initialChecks);
1755
+ await state.onFileSelected({
1756
+ name: 'numbers.csv',
1757
+ arrayBuffer: async () => new _util.TextEncoder().encode('Name\n10\n20\n').buffer
1758
+ });
1759
+ expect(fieldIds(state, 'Name')).toContain('price');
1760
+ expect(isValueCompatible.mock.calls.length).toBeGreaterThan(initialChecks);
1761
+ });
1762
+ it('refreshes compatibility when the source fields change', () => {
1763
+ const fields = _mobx.observable.box([{
1764
+ id: 'value',
1765
+ header: 'Value',
1766
+ type: 'NUMBER'
1767
+ }]);
1768
+ const {
1769
+ state
1770
+ } = setup({}, {
1771
+ config: {
1772
+ get fields() {
1773
+ return fields.get();
1774
+ },
1775
+ dataSource: {
1776
+ collectionId: 'Movies'
1777
+ }
1778
+ }
1779
+ });
1780
+ expect(fieldIds(state, 'Name')).toEqual([]);
1781
+ (0, _mobx.runInAction)(() => fields.set([{
1782
+ id: 'value',
1783
+ header: 'Value',
1784
+ type: 'SHORT_TEXT'
1785
+ }]));
1786
+ expect(fieldIds(state, 'Name')).toEqual(['value']);
1787
+ });
1788
+ it.each([['14:30:00.000', true], ['14:30:00x000', false]])('offers Time for %s only when the format fits', (value, fits) => {
1789
+ const {
1790
+ state
1791
+ } = setup({}, {
1792
+ fields: [{
1793
+ id: 'clock',
1794
+ header: 'Clock',
1795
+ type: 'TIME'
1796
+ }]
1797
+ });
1798
+ (0, _mobx.runInAction)(() => {
1799
+ state.csvRows = [[value]];
1800
+ });
1801
+ expect(fieldIds(state, 'Name')).toEqual(fits ? ['clock'] : []);
1802
+ });
1803
+ it('updates column compatibility when a new file is uploaded', async () => {
1804
+ const {
1805
+ state
1806
+ } = setup({}, {
1807
+ fields: FIELDS
1808
+ });
1809
+ const upload = csv => state.onFileSelected({
1810
+ name: 'data.csv',
1811
+ arrayBuffer: async () => new _util.TextEncoder().encode(csv).buffer
1812
+ });
1813
+ await upload('Value\n10\n20\n');
1814
+ expect(fieldIds(state, 'Value')).toContain('price');
1815
+ expect(fieldIds(state, 'Value')).not.toContain('launch');
1816
+ await upload('Value\n2026-01-01\n2026-01-02\n');
1817
+ expect(fieldIds(state, 'Value')).toContain('launch');
1818
+ expect(fieldIds(state, 'Value')).not.toContain('price');
1819
+ });
1820
+ it.each([[['10', '20', 'bad'], true], [['10', 'bad', 'worse'], false]])('offers Number only when two-thirds of the samples fit: %j', (values, fits) => {
1821
+ const state = setupColumns(['Value'], values.map(value => [value]));
1822
+ expect(fieldIds(state, 'Value').includes('price')).toBe(fits);
1823
+ });
1824
+ });
1577
1825
  describe('ImportState BI reporting', () => {
1578
1826
  beforeEach(() => {
1579
1827
  jest.useRealTimers();