forked from tsai/budibase
5 changed files with 60 additions and 36 deletions
@ -0,0 +1,27 @@ |
|||
/** |
|||
* buildStateOrigins |
|||
* |
|||
* Builds an object that details all the bound state in the application, and what updates it. |
|||
* |
|||
* @param screenDefinition - the screen definition metadata. |
|||
* @returns {Object} an object with the client state values and how they are managed. |
|||
*/ |
|||
export const buildStateOrigins = screenDefinition => { |
|||
const origins = {}; |
|||
|
|||
function traverse(propValue) { |
|||
for (let key in propValue) { |
|||
if (!Array.isArray(propValue[key])) continue; |
|||
|
|||
if (key === "_children") propValue[key].forEach(traverse); |
|||
|
|||
for (let element of propValue[key]) { |
|||
if (element["##eventHandlerType"] === "Set State") origins[element.parameters.path] = element; |
|||
} |
|||
} |
|||
} |
|||
|
|||
traverse(screenDefinition.props); |
|||
|
|||
return origins; |
|||
}; |
|||
@ -0,0 +1,29 @@ |
|||
import { buildStateOrigins } from "../src/builderStore/buildStateOrigins"; |
|||
|
|||
it("builds the correct stateOrigins object from a screen definition with handlers", () => { |
|||
expect(buildStateOrigins({ |
|||
"name": "screen1", |
|||
"description": "", |
|||
"props": { |
|||
"_component": "@budibase/standard-components/div", |
|||
"className": "", |
|||
"onClick": [ |
|||
{ |
|||
"##eventHandlerType": "Set State", |
|||
"parameters": { |
|||
"path": "testKey", |
|||
"value": "value" |
|||
} |
|||
} |
|||
] |
|||
} |
|||
})).toEqual({ |
|||
"testKey": { |
|||
"##eventHandlerType": "Set State", |
|||
"parameters": { |
|||
"path": "testKey", |
|||
"value": "value" |
|||
} |
|||
} |
|||
}); |
|||
}); |
|||
Loading…
Reference in new issue