|
1 | 1 | import dh from '@deephaven/jsapi-shim'; |
2 | 2 | import { TimeoutError } from '@deephaven/utils'; |
3 | | -import { fetchVariableDefinition } from './ConnectionUtils'; |
| 3 | +import { |
| 4 | + fetchVariableDefinition, |
| 5 | + fetchVariableDefinitionByPredicate, |
| 6 | +} from './ConnectionUtils'; |
4 | 7 |
|
5 | 8 | const mockRemoveListener = jest.fn(); |
6 | 9 | const mockSubscribeToFieldUpdates = jest.fn( |
@@ -94,3 +97,81 @@ it('throws an Error if variable not found', async () => { |
94 | 97 | await expect(fetchPromise).rejects.not.toThrow(TimeoutError); |
95 | 98 | expect(mockRemoveListener).toHaveBeenCalled(); |
96 | 99 | }); |
| 100 | + |
| 101 | +describe('fetchVariableDefinitionByPredicate', () => { |
| 102 | + it('finds definition matching predicate', async () => { |
| 103 | + const predicate = (def: dh.ide.VariableDefinition) => |
| 104 | + def.type === dh.VariableType.FIGURE; |
| 105 | + |
| 106 | + const fetchPromise = fetchVariableDefinitionByPredicate( |
| 107 | + connection, |
| 108 | + predicate |
| 109 | + ); |
| 110 | + |
| 111 | + expect(mockSubscribeToFieldUpdates).toHaveBeenCalled(); |
| 112 | + expect(mockRemoveListener).not.toHaveBeenCalled(); |
| 113 | + |
| 114 | + const listener = mockSubscribeToFieldUpdates.mock.calls[0][0]; |
| 115 | + |
| 116 | + listener({ |
| 117 | + created: [testDefinition1, testDefinition2], |
| 118 | + updated: [], |
| 119 | + removed: [], |
| 120 | + }); |
| 121 | + |
| 122 | + const result = await fetchPromise; |
| 123 | + |
| 124 | + expect(result).toBe(testDefinition2); |
| 125 | + expect(mockRemoveListener).toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('throws a TimeoutError if finding the variable timed out', async () => { |
| 129 | + const timeout = 1000; |
| 130 | + const predicate = (def: dh.ide.VariableDefinition) => |
| 131 | + def.title.startsWith('NON_EXISTENT'); |
| 132 | + |
| 133 | + const fetchPromise = fetchVariableDefinitionByPredicate( |
| 134 | + connection, |
| 135 | + predicate, |
| 136 | + timeout, |
| 137 | + 'Custom error message' |
| 138 | + ); |
| 139 | + |
| 140 | + expect(mockSubscribeToFieldUpdates).toHaveBeenCalled(); |
| 141 | + expect(mockRemoveListener).not.toHaveBeenCalled(); |
| 142 | + |
| 143 | + jest.advanceTimersByTime(timeout + 2000); |
| 144 | + |
| 145 | + await expect(fetchPromise).rejects.toThrow(TimeoutError); |
| 146 | + await expect(fetchPromise).rejects.toThrow('Timeout: Custom error message'); |
| 147 | + expect(mockRemoveListener).toHaveBeenCalled(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('throws an Error if no definition matches predicate', async () => { |
| 151 | + const predicate = (def: dh.ide.VariableDefinition) => |
| 152 | + def.title === 'NON_EXISTENT'; |
| 153 | + |
| 154 | + const fetchPromise = fetchVariableDefinitionByPredicate( |
| 155 | + connection, |
| 156 | + predicate, |
| 157 | + undefined, |
| 158 | + 'Custom not found message' |
| 159 | + ); |
| 160 | + |
| 161 | + expect(mockSubscribeToFieldUpdates).toHaveBeenCalled(); |
| 162 | + expect(mockRemoveListener).not.toHaveBeenCalled(); |
| 163 | + |
| 164 | + const listener = mockSubscribeToFieldUpdates.mock.calls[0][0]; |
| 165 | + |
| 166 | + listener({ |
| 167 | + created: [testDefinition1], |
| 168 | + updated: [], |
| 169 | + removed: [], |
| 170 | + }); |
| 171 | + |
| 172 | + await expect(fetchPromise).rejects.toThrow(Error); |
| 173 | + await expect(fetchPromise).rejects.toThrow('Custom not found message'); |
| 174 | + await expect(fetchPromise).rejects.not.toThrow(TimeoutError); |
| 175 | + expect(mockRemoveListener).toHaveBeenCalled(); |
| 176 | + }); |
| 177 | +}); |
0 commit comments