|
| 1 | +import {expect} from 'chai'; |
| 2 | +import {beforeEach, describe, it} from 'mocha'; |
| 3 | +import {computeGraphs, getAnyVals} from '../src/actions/dependencies'; |
| 4 | +import {getCallbacksByInput} from '../src/actions/dependencies_ts'; |
| 5 | +import {EventEmitter} from '../src/actions/utils'; |
| 6 | + |
| 7 | +const config = {validate_callbacks: true}; |
| 8 | + |
| 9 | +// Build a paths fixture that matches the layout crawling output |
| 10 | +// (paths.strs for string ids, paths.objs for wildcard ids). |
| 11 | +function makePaths(stringIds, wildcardItems) { |
| 12 | + const paths = { |
| 13 | + strs: {}, |
| 14 | + objs: {}, |
| 15 | + events: new EventEmitter() |
| 16 | + }; |
| 17 | + stringIds.forEach(id => { |
| 18 | + paths.strs[id] = ['props', 'children', 0]; |
| 19 | + }); |
| 20 | + Object.entries(wildcardItems || {}).forEach(([keyStr, items]) => { |
| 21 | + paths.objs[keyStr] = items.map((values, i) => ({ |
| 22 | + values, |
| 23 | + path: ['props', 'children', i] |
| 24 | + })); |
| 25 | + }); |
| 26 | + return paths; |
| 27 | +} |
| 28 | + |
| 29 | +describe('dependencies — MATCH validation (#2462)', () => { |
| 30 | + let errors; |
| 31 | + const dispatchError = (message, lines) => { |
| 32 | + errors.push({message, lines}); |
| 33 | + }; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + errors = []; |
| 37 | + }); |
| 38 | + |
| 39 | + it('permits MATCH Input with fixed-id Output', () => { |
| 40 | + computeGraphs( |
| 41 | + [ |
| 42 | + { |
| 43 | + output: 'out.children', |
| 44 | + inputs: [{id: '{"id":["MATCH"]}', property: 'n_clicks'}], |
| 45 | + state: [], |
| 46 | + no_output: false |
| 47 | + } |
| 48 | + ], |
| 49 | + dispatchError, |
| 50 | + config |
| 51 | + ); |
| 52 | + expect(errors).to.eql([]); |
| 53 | + }); |
| 54 | + |
| 55 | + it('permits MATCH Input with no-output callback', () => { |
| 56 | + computeGraphs( |
| 57 | + [ |
| 58 | + { |
| 59 | + output: '', |
| 60 | + inputs: [{id: '{"id":["MATCH"]}', property: 'n_clicks'}], |
| 61 | + state: [], |
| 62 | + no_output: true |
| 63 | + } |
| 64 | + ], |
| 65 | + dispatchError, |
| 66 | + config |
| 67 | + ); |
| 68 | + expect(errors).to.eql([]); |
| 69 | + }); |
| 70 | + |
| 71 | + it('permits MATCH State with fixed-id Output', () => { |
| 72 | + computeGraphs( |
| 73 | + [ |
| 74 | + { |
| 75 | + output: 'out.children', |
| 76 | + inputs: [{id: '{"id":["MATCH"]}', property: 'n_clicks'}], |
| 77 | + state: [{id: '{"id":["MATCH"]}', property: 'id'}], |
| 78 | + no_output: false |
| 79 | + } |
| 80 | + ], |
| 81 | + dispatchError, |
| 82 | + config |
| 83 | + ); |
| 84 | + expect(errors).to.eql([]); |
| 85 | + }); |
| 86 | + |
| 87 | + it('permits MATCH Input with ALL-only wildcard Output', () => { |
| 88 | + computeGraphs( |
| 89 | + [ |
| 90 | + { |
| 91 | + output: '{"id":["ALL"]}.children', |
| 92 | + inputs: [ |
| 93 | + { |
| 94 | + id: '{"type":"btn","idx":["MATCH"]}', |
| 95 | + property: 'n_clicks' |
| 96 | + } |
| 97 | + ], |
| 98 | + state: [], |
| 99 | + no_output: false |
| 100 | + } |
| 101 | + ], |
| 102 | + dispatchError, |
| 103 | + config |
| 104 | + ); |
| 105 | + expect(errors).to.eql([]); |
| 106 | + }); |
| 107 | + |
| 108 | + it('still errors on ALLSMALLER Input with fixed Output', () => { |
| 109 | + computeGraphs( |
| 110 | + [ |
| 111 | + { |
| 112 | + output: 'out.children', |
| 113 | + inputs: [{id: '{"id":["ALLSMALLER"]}', property: 'value'}], |
| 114 | + state: [], |
| 115 | + no_output: false |
| 116 | + } |
| 117 | + ], |
| 118 | + dispatchError, |
| 119 | + config |
| 120 | + ); |
| 121 | + expect(errors).to.have.lengthOf(1); |
| 122 | + expect(errors[0].message).to.equal( |
| 123 | + '`Input` / `State` wildcards not in `Output`s' |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it('still errors when Output has MATCH on different keys than Input', () => { |
| 128 | + computeGraphs( |
| 129 | + [ |
| 130 | + { |
| 131 | + output: '{"a":["MATCH"]}.children', |
| 132 | + inputs: [{id: '{"b":["MATCH"]}', property: 'n_clicks'}], |
| 133 | + state: [], |
| 134 | + no_output: false |
| 135 | + } |
| 136 | + ], |
| 137 | + dispatchError, |
| 138 | + config |
| 139 | + ); |
| 140 | + // Should produce an error because out has MATCH on "a" |
| 141 | + // but input has MATCH on "b". |
| 142 | + expect(errors).to.have.lengthOf(1); |
| 143 | + expect(errors[0].message).to.equal( |
| 144 | + '`Input` / `State` wildcards not in `Output`s' |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it('still errors on Mismatched MATCH across Outputs', () => { |
| 149 | + computeGraphs( |
| 150 | + [ |
| 151 | + { |
| 152 | + output: '..{"b":["MATCH"]}.children...{"b":["ALL"],"c":1}.children..', |
| 153 | + inputs: [ |
| 154 | + {id: '{"b":["MATCH"],"c":2}', property: 'children'} |
| 155 | + ], |
| 156 | + state: [], |
| 157 | + no_output: false |
| 158 | + } |
| 159 | + ], |
| 160 | + dispatchError, |
| 161 | + config |
| 162 | + ); |
| 163 | + const msgs = errors.map(e => e.message); |
| 164 | + expect(msgs).to.include( |
| 165 | + 'Mismatched `MATCH` wildcards across `Output`s' |
| 166 | + ); |
| 167 | + }); |
| 168 | +}); |
| 169 | + |
| 170 | +describe('dependencies — MATCH trigger resolvedId (#2462)', () => { |
| 171 | + it('getAnyVals picks MATCH values from trigger id', () => { |
| 172 | + // Use the same object reference for MATCH that the module uses |
| 173 | + // internally by exercising computeGraphs first. |
| 174 | + const errors = []; |
| 175 | + const graphs = computeGraphs( |
| 176 | + [ |
| 177 | + { |
| 178 | + output: 'out.children', |
| 179 | + inputs: [{id: '{"id":["MATCH"]}', property: 'n_clicks'}], |
| 180 | + state: [], |
| 181 | + no_output: false |
| 182 | + } |
| 183 | + ], |
| 184 | + (m, l) => errors.push({m, l}), |
| 185 | + config |
| 186 | + ); |
| 187 | + expect(errors).to.eql([]); |
| 188 | + const pattern = graphs.inputPatterns.id.n_clicks[0]; |
| 189 | + const anyVals = getAnyVals(pattern.values, ['btn-1']); |
| 190 | + expect(anyVals).to.equal('["btn-1"]'); |
| 191 | + }); |
| 192 | + |
| 193 | + it('fires distinct callbacks per MATCH trigger when Output is fixed', () => { |
| 194 | + const errors = []; |
| 195 | + const graphs = computeGraphs( |
| 196 | + [ |
| 197 | + { |
| 198 | + output: 'out.children', |
| 199 | + inputs: [{id: '{"id":["MATCH"]}', property: 'n_clicks'}], |
| 200 | + state: [], |
| 201 | + no_output: false |
| 202 | + } |
| 203 | + ], |
| 204 | + (m, l) => errors.push({m, l}), |
| 205 | + config |
| 206 | + ); |
| 207 | + expect(errors).to.eql([]); |
| 208 | + |
| 209 | + const paths = makePaths(['out'], { |
| 210 | + id: [['btn-1'], ['btn-2']] |
| 211 | + }); |
| 212 | + |
| 213 | + const first = getCallbacksByInput( |
| 214 | + graphs, |
| 215 | + paths, |
| 216 | + {id: 'btn-1'}, |
| 217 | + 'n_clicks', |
| 218 | + undefined, |
| 219 | + false |
| 220 | + ); |
| 221 | + const second = getCallbacksByInput( |
| 222 | + graphs, |
| 223 | + paths, |
| 224 | + {id: 'btn-2'}, |
| 225 | + 'n_clicks', |
| 226 | + undefined, |
| 227 | + false |
| 228 | + ); |
| 229 | + |
| 230 | + expect(first).to.have.lengthOf(1); |
| 231 | + expect(second).to.have.lengthOf(1); |
| 232 | + expect(first[0].resolvedId).to.not.equal(second[0].resolvedId); |
| 233 | + expect(first[0].resolvedId).to.include('btn-1'); |
| 234 | + expect(second[0].resolvedId).to.include('btn-2'); |
| 235 | + }); |
| 236 | +}); |
0 commit comments