Skip to content

Commit

Permalink
Merge pull request #179 from plocket/158_yesnomaybe
Browse files Browse the repository at this point in the history
#158 yesnomaybe
  • Loading branch information
plocket authored Mar 24, 2021
2 parents 92c2b74 + a2293c3 commit f5c4d17
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 21 deletions.
29 changes: 13 additions & 16 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ module.exports = {
let field_like_names = await scope.getFieldNamesDict( scope, { $: $ });

// Will deal with `option` once inside `select`
let all_nodes = $( `canvas, .da-field-buttons button, .form-group input, .form-group select, .form-group textarea` );
let all_nodes = $( `canvas, fieldset button, .form-group input, .form-group select, .form-group textarea` );

page_data.fields = [];
for ( let node of all_nodes ) {
Expand All @@ -580,14 +580,8 @@ module.exports = {
// The selector we'll use to set the field's value later
field_data.selector = await scope.getFieldSelector( scope, { $, node });

// ====================
// Variable name
// ====================
// Often used in the variable name
let encoded_name = $node.attr( `name` );
// // For `show if` groups, the parents have the encoded var_name
// let saveas = $node.closest( `div[data-saveas]` ).attr( `data-saveas` );
// if ( saveas ) { encoded_name = saveas; }

// ====================
// Table rows
Expand All @@ -600,7 +594,6 @@ module.exports = {

if ( node.name === `select` ) {
values = await scope.getOptionValues( scope, { $select: $node });
field_data.rows = await scope.getPossibleTableRows( scope, { node, var_names, values });

} else if ( $node.hasClass('danota-checkbox') || $node.parents(`.da-field-container-datatype-object_radio`).name ) {
// 'None of the above' fields must find var name info from siblings' attributes
Expand All @@ -623,12 +616,12 @@ module.exports = {
}

// TODO: rename `rows` to `guesses`?
field_data.rows = await scope.getPossibleTableRows( scope, { node, var_names, values });
field_data.rows = await scope.getPossibleTableRowMatchers( scope, { node, var_names, values });
page_data.fields.push( field_data );

} // ends for every node

if ( page_data.sought_var[0] === `direct_showifs`) {
if ( page_data.sought_var[0] === `buttons_yesnomaybe`) {
// console.log( field_like_names );
// console.log( JSON.stringify( page_data, null, 2 ));
}
Expand Down Expand Up @@ -662,7 +655,7 @@ module.exports = {
return selector;
}, // Ends scope.getSelector()

getPossibleTableRows: async function ( scope, { node, var_names, values }) {
getPossibleTableRowMatchers: async function ( scope, { node, var_names, values }) {
/* Return variable names and values combined in ways that can
* try to match table rows. Tables can have three properties:
* variable name, value, and checked (when appropriate, whether
Expand All @@ -686,20 +679,24 @@ module.exports = {
} // end iterate through row possibilities

return possible_table_rows;
}, // Ends scope.getPossibleTableRows()
}, // Ends scope.getPossibleTableRowMatchers()

getFieldNamesDict: async function ( scope, { $ }) {
/* Some `show if` fields have `_field_n` instead of their var name,
/* Returns an object containing a map of the `_field_n` names and
* their matching variable names. If none exist, returns an empty object.
*
* `show if` fields have `_field_n` in the DOM instead of their var name,
* e.g. `_field_2`. Fortunately, there's a da DOM node that contains
* a map between such a string and the actual var name.
* See https://github.com/plocket/docassemble-cucumber/issues/173
* Example of a decoded field-like name key/value pair:
* _field_2: 'some_checkbox_field'
* Example: { _field_0: 'some_checkbox_field' }
*/
if ( !$('input[name="_varnames"]')[0] ) { return {}; }

let field_like_names = {};
let field_like_names_encoded = $('input[name="_varnames"]')[0].attribs.value;
let field_like_names_str = await scope.fromBase64( scope, { base64_str: field_like_names_encoded });
let field_like_names_json = JSON.parse( field_like_names_str );
let field_like_names = {};
for ( let field_like_key in field_like_names_json ) {
let field_like_DOM_name = await scope.fromBase64( scope, { base64_str: field_like_key });
let field_like_var_name = await scope.fromBase64( scope, { base64_str: field_like_names_json[ field_like_key ]});
Expand Down
16 changes: 16 additions & 0 deletions tests/unit_tests/getMatchingRow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,19 @@ it("matches the right table and field rows for simple show if fields", async fun
expect( result ).to.deep.equal( matches.show_if );

});


it("matches the right table and field rows for yesnomaybe buttons", async function() {

let result1 = await getMatchingRow( scope, { page_data: page_data.buttons_yesnomaybe, story_table: tables.buttons_yesnomaybe_yes });
expect( result1 ).to.deep.equal( matches.buttons_yesnomaybe_yes );

let result2 = await getMatchingRow( scope, { page_data: page_data.buttons_yesnomaybe, story_table: tables.buttons_yesnomaybe_no });
expect( result2 ).to.deep.equal( matches.buttons_yesnomaybe_no );

let result3 = await getMatchingRow( scope, { page_data: page_data.buttons_yesnomaybe, story_table: tables.buttons_yesnomaybe_none });
expect( result3 ).to.deep.equal( matches.buttons_yesnomaybe_none );

});


6 changes: 5 additions & 1 deletion tests/unit_tests/getPageData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ it(`creates the right data for standard fields`, async function() {
});

it(`creates the right data for 'show if' fields`, async function() {
// 18 fields (03/15/21)
let result = await getPageData( scope, { html: html.show_if });
expect( result ).to.deep.equal( page_data.show_if );
});

it(`creates the right data for yesnomaybe buttons`, async function() {
let result = await getPageData( scope, { html: html.buttons_yesnomaybe });
expect( result ).to.deep.equal( page_data.buttons_yesnomaybe );
});


/*
Fields:
Expand Down
34 changes: 34 additions & 0 deletions tests/unit_tests/html.fixtures.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions tests/unit_tests/matches.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,29 @@ matches.show_if = [
"table": { "var_name": "direct_showifs", "value": "True", "checked": true, },
},
];


module.exports = matches;



// ============================
// Buttons
// ============================
matches.buttons_yesnomaybe_yes = [
{
"field": { "selector": "button[name=\"YnV0dG9uc195ZXNub21heWJl\"][value=\"True\"]", "tag": "button", "rows": [ { "var_name": "buttons_yesnomaybe", "value": "True", "checked": false, } ], "type": "submit" },
"table": { "var_name": "buttons_yesnomaybe", "value": "True", "checked": true, },
}
];
matches.buttons_yesnomaybe_no = [
{
"field": { "selector": "button[name=\"YnV0dG9uc195ZXNub21heWJl\"][value=\"False\"]", "tag": "button", "rows": [ { "var_name": "buttons_yesnomaybe", "value": "False", "checked": false, } ], "type": "submit" },
"table": { "var_name": "buttons_yesnomaybe", "value": "False", "checked": true, },
}
];
matches.buttons_yesnomaybe_none = [
{
"field": { "selector": "button[name=\"YnV0dG9uc195ZXNub21heWJl\"][value=\"None\"]", "tag": "button", "rows": [ { "var_name": "buttons_yesnomaybe", "value": "None", "checked": false, } ], "type": "submit" },
"table": { "var_name": "buttons_yesnomaybe", "value": "None", "checked": true, },
}
];


module.exports = matches;
49 changes: 49 additions & 0 deletions tests/unit_tests/page_data.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,53 @@ page_data.show_if = {
]
}; // ends page_data.show_if


// ============================
// Buttons
// ============================
// TODO: Shall we allow 'maybe' in the table as a value for `None`?
page_data.buttons_yesnomaybe = {
"sought_var": [
"buttons_yesnomaybe"
],
"fields": [
{
"selector": "button[name=\"YnV0dG9uc195ZXNub21heWJl\"][value=\"True\"]",
"tag": "button",
"rows": [
{
"var_name": "buttons_yesnomaybe",
"value": "True",
"checked": false
}
],
"type": "submit"
},
{
"selector": "button[name=\"YnV0dG9uc195ZXNub21heWJl\"][value=\"False\"]",
"tag": "button",
"rows": [
{
"var_name": "buttons_yesnomaybe",
"value": "False",
"checked": false
}
],
"type": "submit"
},
{
"selector": "button[name=\"YnV0dG9uc195ZXNub21heWJl\"][value=\"None\"]",
"tag": "button",
"rows": [
{
"var_name": "buttons_yesnomaybe",
"value": "None",
"checked": false
}
],
"type": "submit"
}
]
};

module.exports = page_data;
14 changes: 14 additions & 0 deletions tests/unit_tests/tables.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ tables.show_if = [
{ "var_name": "showif_dropdown", "value": "showif_dropdown_2", "checked": true, }, // May want to change `checked`
];


// ============================
// Buttons
// ============================
tables.buttons_yesnomaybe_yes = [
{ "var_name": "buttons_yesnomaybe", "value": "True", "checked": true, }, // May want to change `checked`
];
tables.buttons_yesnomaybe_no = [
{ "var_name": "buttons_yesnomaybe", "value": "False", "checked": true, }, // May want to change `checked`
];
tables.buttons_yesnomaybe_none = [
{ "var_name": "buttons_yesnomaybe", "value": "None", "checked": true, }, // May want to change `checked`
];

module.exports = tables;

0 comments on commit f5c4d17

Please sign in to comment.