Skip to content

Commit

Permalink
Merge pull request #630 from werpu/main
Browse files Browse the repository at this point in the history
  • Loading branch information
volosied authored Oct 23, 2023
2 parents e2f5217 + 53d9b46 commit cebbf17
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 19 deletions.
14 changes: 7 additions & 7 deletions api/src/client/package-lock.json

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

2 changes: 1 addition & 1 deletion api/src/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"html-webpack-plugin": "^5.5.1",
"jsdom": "^21.1.1",
"jsdom-global": "^3.0.2",
"jsf.js_next_gen": "4.0.2-beta.3",
"jsf.js_next_gen": "4.0.2-beta.4",
"mocha": "^10.2.0",
"npm-check-updates": "^16.10.8",
"nyc": "^15.1.0",
Expand Down
34 changes: 24 additions & 10 deletions api/src/client/typescript/faces/impl/xhrCore/XhrRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,7 @@ export class XhrRequest extends AsyncRunnable<XMLHttpRequest> {
requestPassThroughParams.$nspEnabled = true;
}

const issuingItemId = this.internalContext.getIf(CTX_PARAM_SRC_CTL_ID).value;
if(issuingItemId) {
const itemValue = DQ.byId(issuingItemId).inputValue;
if(itemValue.isPresent()) {
const arr = new ExtConfig({});
arr.assign(issuingItemId).value = itemValue.value;
formData.shallowMerge(arr, true, true);
}
}
this.appendIssuingItem(formData);

this.responseContext = requestPassThroughParams.deepCopy;

Expand Down Expand Up @@ -387,7 +379,6 @@ export class XhrRequest extends AsyncRunnable<XMLHttpRequest> {
// We need to resolve the local handlers lazily,
// because some frameworks might decorate them over the context in the response
let eventHandler = resolveHandlerFunc(this.requestContext, this.responseContext, ON_EVENT);

Implementation.sendEvent(eventData, eventHandler);
} catch (e) {
e.source = e?.source ?? this.requestContext.getIf(SOURCE).value;
Expand All @@ -409,4 +400,27 @@ export class XhrRequest extends AsyncRunnable<XMLHttpRequest> {

Implementation.sendError(errorData, eventHandler);
}

private appendIssuingItem(formData: XhrFormData) {
const issuingItemId = this.internalContext.getIf(CTX_PARAM_SRC_CTL_ID).value;
//not encoded
if(issuingItemId && formData.getIf(issuingItemId).isAbsent()) {
const issuingItem = DQ.byId(issuingItemId);
const itemValue = issuingItem.inputValue;
const arr = new ExtConfig({});
const type: string = issuingItem.type.orElse("").value.toLowerCase();

//Checkbox and radio only value pass if checked is set, otherwise they should not show
//up at all, and if checked is set, they either can have a value or simply being boolean
if((type == "checkbox" || type == "radio") && issuingItem.attr("checked").isAbsent()) {
return;
} else if((type == "checkbox" || type == "radio")) {
arr.assign(issuingItemId).value = itemValue.orElse(true).value;
} else {
arr.assign(issuingItemId).value = itemValue.value;
}

formData.shallowMerge(arr, true, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,30 @@ export module StandardInits {
</html>`;


const CHECKBOX_RADIO_FORM = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form id="form1">
<input type="checkbox" checked name="page:numbers:1" id="page:numbers:1" value="1"></input>
<input type="checkbox" name="page:numbers:2" id="page:numbers:2" value="2"></input>
<input type="checkbox" name="page:numbers:3" id="page:numbers:3" value="3"></input>
</form>
<form id="form2">
<input type="radio" checked name="page:numbers:r:1" id="page:numbers:r:1" value="1"></input>
<input type="radio" name="page:numbers:r:2" id="page:numbers:r:2" value="2"></input>
<input type="radio" name="page:numbers:r:3" id="page:numbers:r:3" value="3"></input>
</form>
</body>
</html>
`;


/**
* a page simulating basically a simple faces form
*/
Expand Down Expand Up @@ -521,6 +545,10 @@ function triggerRequestChain(event) {
return init(VIRTUAL_ELEMENT, withJsf);
}

export function initCheckboxRadioForm(withJsf = true): Promise<() => void> {
return init(CHECKBOX_RADIO_FORM, withJsf);
}

export function defaultMyFaces23(withJsf = true): Promise<() => void> {
return init(HTML_FORM_DEFAULT.replace(/jakarta/gi, "javax"), withJsf, false);
}
Expand Down
79 changes: 78 additions & 1 deletion api/src/client/typescript/faces/test/xhrCore/RequestTest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import defaultMyFaces = StandardInits.defaultMyFaces;
import initVirtualElement = StandardInits.initVirtualElement;
import STD_XML = StandardInits.STD_XML;
import exp from "constants";
import initCheckboxForm = StandardInits.initCheckboxRadioForm;

declare var faces: any;
declare var Implementation: any;
Expand Down Expand Up @@ -652,7 +653,7 @@ describe('Tests after core when it hits response', function () {
dqElem.querySelectorAll("input").click();

faces.ajax.request(element, null, {
execute: "page:numbers",
execute: "jd_0:blarg",
render: "@form",
params: {
pass1: "pass1",
Expand Down Expand Up @@ -684,5 +685,81 @@ describe('Tests after core when it hits response', function () {

});

/**
* the idea is if a checkbox or radio button is the issuing element
* we cannot attach it
*/
it("must handle issuing checkboxes and radio buttons with values correctly", () => {
const waitForResult = initCheckboxForm();
return waitForResult.then((close) => {
const send = sinon.spy(XMLHttpRequest.prototype, "send");
let dqElem = DomQuery.byId("page:numbers:1");
dqElem.removeAttribute("checked");
let element = dqElem.getAsElem(0).value;

faces.ajax.request(element, null, {
execute: "form1",
render: "@form",
params: {
pass1: "pass1",
pass2: "pass2"
}
});

let argsVal: any = send.args[0][0];
let arsArr = argsVal.split("&");
let resultsMap = {};
let doubles = 0;
for (let val of arsArr) {
let keyVal = val.split("=");
if(!!resultsMap[keyVal[0]]) {
doubles++;
}
resultsMap[keyVal[0]] = keyVal[1];
}

//TODO test assert here
expect(resultsMap["page%3numbers%31"]).to.not.exist;
//expect(doubles).to.eq(2);

});
});

it("must handle issuing checkboxes and radio buttons with values correctly", () => {
const waitForResult = initCheckboxForm();
return waitForResult.then((close) => {
const send = sinon.spy(XMLHttpRequest.prototype, "send");
let dqElem = DomQuery.byId("page:numbers:r:1");
dqElem.removeAttribute("checked");
let element = dqElem.getAsElem(0).value;

faces.ajax.request(element, null, {
execute: "form2",
render: "@form",
params: {
pass1: "pass1",
pass2: "pass2"
}
});

let argsVal: any = send.args[0][0];
let arsArr = argsVal.split("&");
let resultsMap = {};
let doubles = 0;
for (let val of arsArr) {
let keyVal = val.split("=");
if(!!resultsMap[keyVal[0]]) {
doubles++;
}
resultsMap[keyVal[0]] = keyVal[1];
}

//TODO test assert here
expect(resultsMap["page%3numbers%3r%31"]).to.not.exist;
//expect(doubles).to.eq(2);

});
});

});

0 comments on commit cebbf17

Please sign in to comment.