32 lines
1.4 KiB
JavaScript
32 lines
1.4 KiB
JavaScript
|
|
// Unit test for the mobile keyboard-lift logic (pure function, no DOM).
|
||
|
|
// The composer must sit exactly on top of the keyboard. The amount to lift it is the gap between the
|
||
|
|
// LAYOUT viewport (document.documentElement.clientHeight) and the VISIBLE viewport
|
||
|
|
// (window.visualViewport.height + offsetTop). This is self-adapting across Capacitor keyboard resize
|
||
|
|
// modes, and needs no per-device constant.
|
||
|
|
//
|
||
|
|
// Run: node test/keyboard-lift.test.js
|
||
|
|
|
||
|
|
function bzKeyboardLift(clientHeight, vvHeight, vvTop) {
|
||
|
|
return Math.max(0, Math.round(clientHeight - vvHeight - (vvTop || 0)));
|
||
|
|
}
|
||
|
|
|
||
|
|
// Cases use REAL numbers captured from the on-device debug readout (iPhone, dpr=3):
|
||
|
|
const cases = [
|
||
|
|
// [name, clientHeight, vvHeight, vvTop, expectedLift]
|
||
|
|
['keyboard closed (all equal) ', 926, 926, 0, 0],
|
||
|
|
['resize:native open (WebView shrank; residual)', 581, 535, 0, 46], // <- the device values you sent
|
||
|
|
['resize:none open (WebView full; full lift) ', 926, 581, 0, 345],
|
||
|
|
['with a visual-viewport offset ', 926, 560, 20, 346],
|
||
|
|
['guard: never negative ', 500, 600, 0, 0],
|
||
|
|
];
|
||
|
|
|
||
|
|
let pass = 0, fail = 0;
|
||
|
|
for (const [name, ch, vh, vt, expected] of cases) {
|
||
|
|
const got = bzKeyboardLift(ch, vh, vt);
|
||
|
|
const ok = got === expected;
|
||
|
|
console.log(`${ok ? 'PASS' : 'FAIL'} ${name} lift=${got}${ok ? '' : ` (expected ${expected})`}`);
|
||
|
|
ok ? pass++ : fail++;
|
||
|
|
}
|
||
|
|
console.log(`\n${pass} passed, ${fail} failed`);
|
||
|
|
process.exit(fail ? 1 : 0);
|