|
| 1 | +import { |
| 2 | + afterEach, beforeEach, describe, expect, jest, test, |
| 3 | +} from '@jest/globals'; |
| 4 | +import { Deferred } from '@js/core/utils/deferred'; |
| 5 | +import type { Store } from '@js/data'; |
| 6 | +import CustomStore from '@js/data/custom_store'; |
| 7 | +import DataSource from '@js/data/data_source'; |
| 8 | + |
| 9 | +import { DataSourceAdapterTreeList } from './m_data_source_adapter'; |
| 10 | + |
| 11 | +describe('TreeList DataSourceAdapter - T1311885 Race Condition', () => { |
| 12 | + let dataSourceAdapter: DataSourceAdapterTreeList; |
| 13 | + let mockStore: Store; |
| 14 | + let loadCalls: { filter: any; deferred: any; type?: string }[]; |
| 15 | + const parentData = [ |
| 16 | + { Task_ID: 1, Task_Parent_ID: 0, Task_Subject: 'Parent 1' }, |
| 17 | + { Task_ID: 2, Task_Parent_ID: 0, Task_Subject: 'Parent 2' }, |
| 18 | + ]; |
| 19 | + |
| 20 | + const childData = [ |
| 21 | + { Task_ID: 10, Task_Parent_ID: 1, Task_Subject: 'Child 1' }, |
| 22 | + { Task_ID: 20, Task_Parent_ID: 2, Task_Subject: 'Child 2' }, |
| 23 | + ]; |
| 24 | + |
| 25 | + const OPERATION_ID = { |
| 26 | + FIRST: 1, |
| 27 | + SECOND: 2, |
| 28 | + }; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + loadCalls = []; |
| 32 | + |
| 33 | + mockStore = new CustomStore({ |
| 34 | + key: 'Task_ID', |
| 35 | + load: (options: any) => { |
| 36 | + // @ts-expect-error |
| 37 | + const deferred = new Deferred(); |
| 38 | + loadCalls.push({ filter: options?.filter, deferred }); |
| 39 | + return deferred.promise(); |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + const dataSource = new DataSource({ |
| 44 | + store: mockStore, |
| 45 | + reshapeOnPush: true, |
| 46 | + }); |
| 47 | + |
| 48 | + const mockComponent = { |
| 49 | + option: jest.fn((key: string) => { |
| 50 | + const options: any = { |
| 51 | + remoteOperations: { filtering: true, sorting: true }, |
| 52 | + parentIdExpr: 'Task_Parent_ID', |
| 53 | + hasItemsExpr: 'Has_Items', |
| 54 | + filterMode: 'fullBranch', |
| 55 | + expandedRowKeys: [], |
| 56 | + dataStructure: 'plain', |
| 57 | + rootValue: 0, |
| 58 | + }; |
| 59 | + return options[key]; |
| 60 | + }), |
| 61 | + _createActionByOption: jest.fn(() => jest.fn()), |
| 62 | + on: jest.fn(() => mockComponent), |
| 63 | + off: jest.fn(() => mockComponent), |
| 64 | + _eventsStrategy: { |
| 65 | + on: jest.fn(), |
| 66 | + off: jest.fn(), |
| 67 | + fireEvent: jest.fn(), |
| 68 | + hasEvent: jest.fn(() => false), |
| 69 | + }, |
| 70 | + } as any; |
| 71 | + |
| 72 | + dataSourceAdapter = new DataSourceAdapterTreeList(mockComponent); |
| 73 | + dataSourceAdapter.init(dataSource, { remoteOperations: { filtering: true } }); |
| 74 | + |
| 75 | + (dataSourceAdapter as any)._loadDataSource = jest.fn((options: any) => { |
| 76 | + // @ts-expect-error |
| 77 | + const deferred = new Deferred(); |
| 78 | + |
| 79 | + loadCalls.push({ |
| 80 | + filter: options?.filter, |
| 81 | + deferred, |
| 82 | + type: 'dataSource', |
| 83 | + }); |
| 84 | + |
| 85 | + return deferred.promise(); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + afterEach(() => { |
| 90 | + jest.clearAllMocks(); |
| 91 | + jest.restoreAllMocks(); |
| 92 | + loadCalls = []; |
| 93 | + (dataSourceAdapter as any)._loadDataSource = undefined; |
| 94 | + (dataSourceAdapter as any).loadFromStore = undefined; |
| 95 | + mockStore = undefined as any; |
| 96 | + dataSourceAdapter = undefined as any; |
| 97 | + }); |
| 98 | + |
| 99 | + test('T1311885 - _loadParentsOrChildren should NOT throw concat error when _cachedStoreData is cleared', async () => { |
| 100 | + let firstLoadDeferred: any = null; |
| 101 | + let errorMessage = ''; |
| 102 | + |
| 103 | + const unhandledRejectionHandler = (reason: any) => { |
| 104 | + errorMessage = reason?.message || String(reason); |
| 105 | + }; |
| 106 | + process.on('unhandledRejection', unhandledRejectionHandler); |
| 107 | + |
| 108 | + (dataSourceAdapter as any)._cachedStoreData = parentData; |
| 109 | + (dataSourceAdapter as any)._dataSource = { |
| 110 | + store: jest.fn(() => mockStore), |
| 111 | + cancel: jest.fn(), |
| 112 | + }; |
| 113 | + (dataSourceAdapter as any)._lastOperationId = OPERATION_ID.FIRST; |
| 114 | + |
| 115 | + const options = { |
| 116 | + remoteOperations: { filtering: true }, |
| 117 | + storeLoadOptions: { sort: null }, |
| 118 | + loadOptions: { sort: null }, |
| 119 | + operationId: OPERATION_ID.FIRST, |
| 120 | + }; |
| 121 | + |
| 122 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 123 | + (dataSourceAdapter as any).loadFromStore = jest.fn((loadOptions, store) => { |
| 124 | + // @ts-expect-error |
| 125 | + const deferred = new Deferred(); |
| 126 | + |
| 127 | + if (!firstLoadDeferred) { |
| 128 | + firstLoadDeferred = deferred; |
| 129 | + } |
| 130 | + |
| 131 | + return deferred.promise(); |
| 132 | + }); |
| 133 | + |
| 134 | + (dataSourceAdapter as any)._loadParentsOrChildren( |
| 135 | + childData, |
| 136 | + options, |
| 137 | + ); |
| 138 | + |
| 139 | + expect((dataSourceAdapter as any).loadFromStore).toHaveBeenCalledTimes(1); |
| 140 | + expect(firstLoadDeferred).toBeDefined(); |
| 141 | + |
| 142 | + (dataSourceAdapter as any)._cachedStoreData = undefined; |
| 143 | + (dataSourceAdapter as any)._lastOperationId = OPERATION_ID.SECOND; |
| 144 | + |
| 145 | + firstLoadDeferred.resolve(parentData); |
| 146 | + await Promise.resolve(); |
| 147 | + |
| 148 | + process.off('unhandledRejection', unhandledRejectionHandler); |
| 149 | + |
| 150 | + expect(errorMessage).toBe(''); |
| 151 | + expect(errorMessage).not.toMatch(/concat|Cannot read properties of undefined/i); |
| 152 | + }); |
| 153 | +}); |
0 commit comments