Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#158 Add an test an action button that triggers an event #182

Merged
merged 2 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 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, fieldset button, .form-group input, .form-group select, .form-group textarea` );
let all_nodes = $( `canvas, fieldset button, .daquestionactionbutton, .form-group input, .form-group select, .form-group textarea` );

page_data.fields = [];
for ( let node of all_nodes ) {
Expand Down Expand Up @@ -582,6 +582,15 @@ module.exports = {

// Often used in the variable name
let encoded_name = $node.attr( `name` );
if ( $( node ).hasClass( 'daquestionactionbutton' ) ) {
let href = node.attribs.href;
let match = href.match( /action=([^&]*)/ );
if ( match ) {
encoded_name = match[1];
} else {
throw( `Cannot find the action button on this page. This shouldn't be possible. Talk to the developers.` );
}
}

// ====================
// Table rows
Expand Down Expand Up @@ -621,7 +630,7 @@ module.exports = {

} // ends for every node

if ( page_data.sought_var[0] === `button_continue`) {
if ( page_data.sought_var[0] === `button_event_action`) {
// console.log( field_like_names );
// console.log( JSON.stringify( page_data, null, 2 ));
}
Expand All @@ -643,6 +652,7 @@ module.exports = {
let selector = `${ node.name }`; // element tag
if ( node.attribs.name ) { selector += `[name="${ node.attribs.name }"]`; }
if ( node.attribs.value ) { selector += `[value="${ node.attribs.value }"]`; }
if ( $( node ).attr( 'data-linknum' )) { selector += `[data-linknum="${ $( node ).attr( 'data-linknum' ) }"]`; }

// Make sure this selector matches only one node
let matching_nodes = $( selector );
Expand Down Expand Up @@ -730,6 +740,15 @@ module.exports = {
layer_1_decoded = field_like_names[ layer_1_decoded ];
}

try {
let action_button_json = JSON.parse( layer_1_decoded );
if ( action_button_json.action ) { layer_1_decoded = action_button_json.action }
} catch ( error ) {
// Things that are not action buttons can't be parsed this way.
// A special error for action buttons requires passing the node in here too, which is a lot to pass in.
// TODO: Should we move this back into `getPageData()`?
}

// May have the setting value as well as the var_name
let checkbox_1_match = layer_1_decoded.match( /^(.*)\[B'(.*)'\]/ );
let checkbox_2_match = layer_1_decoded.match( /^(.*)\[R'(.*)'\]/ );
Expand Down Expand Up @@ -816,6 +835,7 @@ module.exports = {
return buffer.toString( `base64` );
},

// TODO: Change name to 'getMatchingRows'
getMatchingRow: async function ( scope, { story_table, page_data }) {
/* Given html_data and a list of objects describing fields and values,
* returns the matching fields and the data necessary to set those values.
Expand Down Expand Up @@ -880,6 +900,10 @@ module.exports = {
case 'textarea':
if ( names_match ) { rows_match = true; }
break;

case 'a':
if ( names_match ) { rows_match = true; }
break;
}

if ( rows_match ) {
Expand All @@ -891,8 +915,8 @@ module.exports = {
} // ends for each field row possibility
} // ends for each field

if ( page_data.sought_var[0] === `button_continue`) {
// console.log( JSON.stringify( matches, null, 2 ));
if ( page_data.sought_var[0] === `button_event_action`) {
// console.log( JSON.stringify( matches, null ));
}
return matches;
}, // Ends scope.getMatchingRow()
Expand Down
7 changes: 5 additions & 2 deletions tests/unit_tests/getMatchingRow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ it("matches the right table and field rows for simple show if fields", async fun

// `continue button field:`
it(`creates the right data for one continue button`, async function() {
// `continue button field:`
let result1 = await getMatchingRow( scope, { page_data: page_data.button_continue, story_table: tables.button_continue });
expect( result1 ).to.deep.equal( matches.button_continue );
});
Expand Down Expand Up @@ -54,5 +53,9 @@ it("matches the right table and field rows for other mutiple choice continue but
expect( result3 ).to.deep.equal( matches.buttons_other_3 );
});


// `field:` and `action buttons:`
it(`creates the right data for one continue button`, async function() {
let result1 = await getMatchingRow( scope, { page_data: page_data.buttons_event_action, story_table: tables.buttons_event_action });
expect( result1 ).to.deep.equal( matches.buttons_event_action );
});

6 changes: 6 additions & 0 deletions tests/unit_tests/getPageData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ it(`creates the right data for other mutiple choice continue buttons`, async fun
expect( result ).to.deep.equal( page_data.buttons_other );
});

it(`creates the right data for an event action button`, async function() {
// `field:` and `action buttons:`
let result = await getPageData( scope, { html: html.buttons_event_action });
expect( result ).to.deep.equal( page_data.buttons_event_action );
});


/*
Fields:
Expand Down
28 changes: 27 additions & 1 deletion tests/unit_tests/html.fixtures.js

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

8 changes: 8 additions & 0 deletions tests/unit_tests/matches.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,13 @@ matches.buttons_other_3 = [
}
];

// `field:` and `action buttons:`
matches.buttons_event_action = [
{
"field":{"selector":"a[data-linknum=\"1\"]","tag":"a","rows":[{"var_name":"end","value":"","checked":""}],"type":""},
"table":{"var_name":"end","value":"","checked":""}
}
];


module.exports = matches;
34 changes: 34 additions & 0 deletions tests/unit_tests/page_data.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,38 @@ page_data.buttons_other = {
]
};

// `field:` and `action buttons:`
page_data.buttons_event_action = {
"sought_var": [
"button_event_action"
],
"fields": [
{
"selector": "button[name=\"YnV0dG9uX2V2ZW50X2FjdGlvbg\"][value=\"True\"]",
"tag": "button",
"rows": [
{
"var_name": "button_event_action",
"value": "True",
"checked": false
}
],
"type": "submit"
},
{
"selector": "a[data-linknum=\"1\"]",
"tag": "a",
"rows": [
{
"var_name": "end",
"value": "",
"checked": ""
}
],
"type": ""
}
]
}


module.exports = page_data;
6 changes: 6 additions & 0 deletions tests/unit_tests/tables.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,10 @@ tables.buttons_other_3 = [
{ "var_name": "buttons_other", "value": "button_3", "checked": true, }, // May want to change `checked`
];

// `field:` and `action buttons:`
tables.buttons_event_action = [
{ "var_name": "end", "value": "", "checked": "", }, // May want to change `checked`
];


module.exports = tables;