scripting_spec.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. /* Copyright 2020 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { loadScript } from "../../src/display/display_utils.js";
  16. const sandboxBundleSrc = "../../build/generic/build/pdf.sandbox.js";
  17. describe("Scripting", function () {
  18. let sandbox, send_queue, test_id, ref, windowAlert;
  19. function getId() {
  20. const id = `${ref++}R`;
  21. return id;
  22. }
  23. function myeval(code) {
  24. const key = (test_id++).toString();
  25. return sandbox.eval(code, key).then(() => {
  26. const result = send_queue.get(key).result;
  27. send_queue.delete(key);
  28. return result;
  29. });
  30. }
  31. beforeAll(function () {
  32. test_id = 0;
  33. ref = 1;
  34. send_queue = new Map();
  35. window.dispatchEvent = event => {
  36. if (event.detail.command) {
  37. send_queue.set(event.detail.command, event.detail);
  38. } else if (send_queue.has(event.detail.id)) {
  39. const prev = send_queue.get(event.detail.id);
  40. Object.assign(prev, event.detail);
  41. } else {
  42. send_queue.set(event.detail.id, event.detail);
  43. }
  44. };
  45. windowAlert = window.alert;
  46. window.alert = value => {
  47. const command = "alert";
  48. send_queue.set(command, { command, value });
  49. };
  50. const promise = loadScript(sandboxBundleSrc).then(() => {
  51. return window.pdfjsSandbox.QuickJSSandbox();
  52. });
  53. sandbox = {
  54. createSandbox(data) {
  55. promise.then(sbx => sbx.create(data));
  56. },
  57. dispatchEventInSandbox(data) {
  58. return promise.then(sbx => sbx.dispatchEvent(data));
  59. },
  60. nukeSandbox() {
  61. promise.then(sbx => sbx.nukeSandbox());
  62. },
  63. eval(code, key) {
  64. return promise.then(sbx => sbx.evalForTesting(code, key));
  65. },
  66. };
  67. });
  68. afterAll(function () {
  69. sandbox.nukeSandbox();
  70. sandbox = null;
  71. send_queue = null;
  72. window.alert = windowAlert;
  73. });
  74. describe("Sandbox", function () {
  75. it("should send a value, execute an action and get back a new value", async () => {
  76. function compute(n) {
  77. let s = 0;
  78. for (let i = 0; i < n; i++) {
  79. s += i;
  80. }
  81. return s;
  82. }
  83. const number = 123;
  84. const expected = ((number - 1) * number) / 2;
  85. const refId = getId();
  86. const data = {
  87. objects: {
  88. field: [
  89. {
  90. id: refId,
  91. value: "",
  92. actions: {
  93. Keystroke: [
  94. `${compute.toString()}event.value = compute(parseInt(event.value));`,
  95. ],
  96. },
  97. type: "text",
  98. },
  99. ],
  100. },
  101. calculationOrder: [],
  102. appInfo: { language: "en-US", platform: "Linux x86_64" },
  103. };
  104. sandbox.createSandbox(data);
  105. await sandbox.dispatchEventInSandbox({
  106. id: refId,
  107. value: `${number}`,
  108. name: "Keystroke",
  109. willCommit: true,
  110. });
  111. expect(send_queue.has(refId)).toEqual(true);
  112. expect(send_queue.get(refId)).toEqual({
  113. id: refId,
  114. siblings: null,
  115. value: expected,
  116. formattedValue: null,
  117. });
  118. });
  119. });
  120. describe("Doc", function () {
  121. it("should treat globalThis as the doc", async () => {
  122. const refId = getId();
  123. const data = {
  124. objects: {
  125. field: [
  126. {
  127. id: refId,
  128. value: "",
  129. actions: {},
  130. type: "text",
  131. },
  132. ],
  133. },
  134. appInfo: { language: "en-US", platform: "Linux x86_64" },
  135. calculationOrder: [],
  136. dispatchEventName: "_dispatchMe",
  137. };
  138. sandbox.createSandbox(data);
  139. await myeval(`(this.foobar = 123456, 0)`);
  140. const value = await myeval(`this.getField("field").doc.foobar`);
  141. expect(value).toEqual(123456);
  142. });
  143. it("should get field using a path", async () => {
  144. const base = value => {
  145. return {
  146. id: getId(),
  147. value,
  148. actions: {},
  149. type: "text",
  150. };
  151. };
  152. const data = {
  153. objects: {
  154. A: [base(1)],
  155. "A.B": [base(2)],
  156. "A.B.C": [base(3)],
  157. "A.B.C.D": [base(4)],
  158. "A.B.C.D.E": [base(5)],
  159. "A.B.C.D.E.F": [base(6)],
  160. "A.B.C.D.G": [base(7)],
  161. C: [base(8)],
  162. },
  163. appInfo: { language: "en-US", platform: "Linux x86_64" },
  164. calculationOrder: [],
  165. dispatchEventName: "_dispatchMe",
  166. };
  167. sandbox.createSandbox(data);
  168. let value = await myeval(`this.getField("A").value`);
  169. expect(value).toEqual(1);
  170. value = await myeval(`this.getField("B.C").value`);
  171. expect(value).toEqual(3);
  172. // path has been cached so try again
  173. value = await myeval(`this.getField("B.C").value`);
  174. expect(value).toEqual(3);
  175. value = await myeval(`this.getField("B.C.D#0").value`);
  176. expect(value).toEqual(5);
  177. value = await myeval(`this.getField("B.C.D#1").value`);
  178. expect(value).toEqual(7);
  179. value = await myeval(`this.getField("C").value`);
  180. expect(value).toEqual(8);
  181. value = await myeval(
  182. `this.getField("A.B.C.D").getArray().map((x) => x.value)`
  183. );
  184. expect(value).toEqual([4, 5, 6, 7]);
  185. });
  186. });
  187. describe("Util", function () {
  188. beforeAll(function () {
  189. sandbox.createSandbox({
  190. appInfo: { language: "en-US", platform: "Linux x86_64" },
  191. objects: {},
  192. calculationOrder: [],
  193. });
  194. });
  195. describe("printd", function () {
  196. it("should print a date according to a format", async () => {
  197. const date = `new Date("Sun Apr 15 2007 03:14:15")`;
  198. let value = await myeval(`util.printd(0, ${date})`);
  199. expect(value).toEqual("D:20070415031415");
  200. value = await myeval(`util.printd(1, ${date})`);
  201. expect(value).toEqual("2007.04.15 03:14:15");
  202. value = await myeval(`util.printd(2, ${date})`);
  203. expect(value).toEqual("4/15/07 3:14:15 am");
  204. value = await myeval(`util.printd("mmmm mmm mm m", ${date})`);
  205. expect(value).toEqual("April Apr 04 4");
  206. value = await myeval(`util.printd("dddd ddd dd d", ${date})`);
  207. expect(value).toEqual("Sunday Sun 15 15");
  208. });
  209. });
  210. describe("scand", function () {
  211. it("should parse a date according to a format", async () => {
  212. const date = new Date("Sun Apr 15 2007 03:14:15");
  213. let value = await myeval(
  214. `util.scand(0, "D:20070415031415").toString()`
  215. );
  216. expect(new Date(value)).toEqual(date);
  217. value = await myeval(`util.scand(1, "2007.04.15 03:14:15").toString()`);
  218. expect(new Date(value)).toEqual(date);
  219. value = await myeval(`util.scand(2, "4/15/07 3:14:15 am").toString()`);
  220. expect(new Date(value)).toEqual(date);
  221. });
  222. });
  223. describe("printf", function () {
  224. it("should print some data according to a format", async () => {
  225. let value = await myeval(
  226. `util.printf("Integer numbers: %d, %d,...", 1.234, 56.789)`
  227. );
  228. expect(value).toEqual("Integer numbers: 1, 56,...");
  229. value = await myeval(
  230. `util.printf("Hex numbers: %x, %x,...", 1234, 56789)`
  231. );
  232. expect(value).toEqual("Hex numbers: 4D2, DDD5,...");
  233. value = await myeval(
  234. `util.printf("Hex numbers with 0x: %#x, %#x,...", 1234, 56789)`
  235. );
  236. expect(value).toEqual("Hex numbers with 0x: 0x4D2, 0xDDD5,...");
  237. value = await myeval(
  238. `util.printf("Decimal number: %,0+.3f", 1234567.89123)`
  239. );
  240. expect(value).toEqual("Decimal number: +1,234,567.891");
  241. value = await myeval(
  242. `util.printf("Decimal number: %,0+8.3f", 1.234567)`
  243. );
  244. expect(value).toEqual("Decimal number: + 1.235");
  245. value = await myeval(
  246. `util.printf("Decimal number: %,0.2f", -12.34567)`
  247. );
  248. expect(value).toEqual("Decimal number: -12.35");
  249. value = await myeval(`util.printf("Decimal number: %,0.0f", 4.95)`);
  250. expect(value).toEqual("Decimal number: 5");
  251. value = await myeval(`util.printf("Decimal number: %,0.0f", 4.49)`);
  252. expect(value).toEqual("Decimal number: 4");
  253. value = await myeval(`util.printf("Decimal number: %,0.0f", -4.95)`);
  254. expect(value).toEqual("Decimal number: -5");
  255. value = await myeval(`util.printf("Decimal number: %,0.0f", -4.49)`);
  256. expect(value).toEqual("Decimal number: -4");
  257. });
  258. it("should print a string with no argument", async () => {
  259. const value = await myeval(`util.printf("hello world")`);
  260. expect(value).toEqual("hello world");
  261. });
  262. it("print a string with a percent", async () => {
  263. const value = await myeval(`util.printf("%%s")`);
  264. expect(value).toEqual("%%s");
  265. });
  266. });
  267. describe("printx", function () {
  268. it("should print some data according to a format", async () => {
  269. const value = await myeval(
  270. `util.printx("9 (999) 999-9999", "aaa14159697489zzz")`
  271. );
  272. expect(value).toEqual("1 (415) 969-7489");
  273. });
  274. });
  275. });
  276. describe("Events", function () {
  277. it("should trigger an event and modify the source", async () => {
  278. const refId = getId();
  279. const data = {
  280. objects: {
  281. field: [
  282. {
  283. id: refId,
  284. value: "",
  285. actions: {
  286. test: [`event.source.value = "123";`],
  287. },
  288. type: "text",
  289. },
  290. ],
  291. },
  292. appInfo: { language: "en-US", platform: "Linux x86_64" },
  293. calculationOrder: [],
  294. };
  295. sandbox.createSandbox(data);
  296. await sandbox.dispatchEventInSandbox({
  297. id: refId,
  298. value: "",
  299. name: "test",
  300. willCommit: true,
  301. });
  302. expect(send_queue.has(refId)).toEqual(true);
  303. expect(send_queue.get(refId)).toEqual({
  304. id: refId,
  305. value: 123,
  306. });
  307. });
  308. it("should trigger a Keystroke event and invalidate it", async () => {
  309. const refId = getId();
  310. const data = {
  311. objects: {
  312. field: [
  313. {
  314. id: refId,
  315. value: "",
  316. actions: {
  317. Keystroke: [`event.rc = false;`],
  318. },
  319. type: "text",
  320. },
  321. ],
  322. },
  323. appInfo: { language: "en-US", platform: "Linux x86_64" },
  324. calculationOrder: [],
  325. };
  326. sandbox.createSandbox(data);
  327. await sandbox.dispatchEventInSandbox({
  328. id: refId,
  329. value: "hell",
  330. name: "Keystroke",
  331. willCommit: false,
  332. change: "o",
  333. selStart: 4,
  334. selEnd: 4,
  335. });
  336. expect(send_queue.has(refId)).toEqual(true);
  337. expect(send_queue.get(refId)).toEqual({
  338. id: refId,
  339. siblings: null,
  340. value: "hell",
  341. selRange: [4, 4],
  342. });
  343. });
  344. it("should trigger a Keystroke event and change it", async () => {
  345. const refId = getId();
  346. const data = {
  347. objects: {
  348. field: [
  349. {
  350. id: refId,
  351. value: "",
  352. actions: {
  353. Keystroke: [`event.change = "a";`],
  354. },
  355. type: "text",
  356. },
  357. ],
  358. },
  359. appInfo: { language: "en-US", platform: "Linux x86_64" },
  360. calculationOrder: [],
  361. };
  362. sandbox.createSandbox(data);
  363. await sandbox.dispatchEventInSandbox({
  364. id: refId,
  365. value: "hell",
  366. name: "Keystroke",
  367. willCommit: false,
  368. change: "o",
  369. selStart: 4,
  370. selEnd: 4,
  371. });
  372. expect(send_queue.has(refId)).toEqual(true);
  373. expect(send_queue.get(refId)).toEqual({
  374. id: refId,
  375. siblings: null,
  376. value: "hella",
  377. selRange: [5, 5],
  378. });
  379. });
  380. it("should trigger an invalid commit Keystroke event", async () => {
  381. const refId = getId();
  382. const data = {
  383. objects: {
  384. field: [
  385. {
  386. id: refId,
  387. value: "",
  388. actions: {
  389. test: [`event.rc = false;`],
  390. },
  391. type: "text",
  392. },
  393. ],
  394. },
  395. appInfo: { language: "en-US", platform: "Linux x86_64" },
  396. calculationOrder: [],
  397. };
  398. sandbox.createSandbox(data);
  399. await sandbox.dispatchEventInSandbox({
  400. id: refId,
  401. value: "",
  402. name: "test",
  403. willCommit: true,
  404. });
  405. expect(send_queue.has(refId)).toEqual(false);
  406. });
  407. it("should trigger a valid commit Keystroke event", async () => {
  408. const refId1 = getId();
  409. const refId2 = getId();
  410. const data = {
  411. objects: {
  412. field1: [
  413. {
  414. id: refId1,
  415. value: "",
  416. actions: {
  417. Validate: [`event.value = "world";`],
  418. },
  419. type: "text",
  420. },
  421. ],
  422. field2: [
  423. {
  424. id: refId2,
  425. value: "",
  426. actions: {
  427. Calculate: [`event.value = "hello";`],
  428. },
  429. type: "text",
  430. },
  431. ],
  432. },
  433. appInfo: { language: "en-US", platform: "Linux x86_64" },
  434. calculationOrder: [refId2],
  435. };
  436. sandbox.createSandbox(data);
  437. await sandbox.dispatchEventInSandbox({
  438. id: refId1,
  439. value: "hello",
  440. name: "Keystroke",
  441. willCommit: true,
  442. });
  443. expect(send_queue.has(refId1)).toEqual(true);
  444. expect(send_queue.get(refId1)).toEqual({
  445. id: refId1,
  446. siblings: null,
  447. value: "world",
  448. formattedValue: null,
  449. });
  450. });
  451. });
  452. describe("Color", function () {
  453. beforeAll(function () {
  454. sandbox.createSandbox({
  455. appInfo: { language: "en-US", platform: "Linux x86_64" },
  456. objects: {},
  457. calculationOrder: [],
  458. });
  459. });
  460. function round(color) {
  461. return [
  462. color[0],
  463. ...color.slice(1).map(x => Math.round(x * 1000) / 1000),
  464. ];
  465. }
  466. it("should convert RGB color for different color spaces", async () => {
  467. let value = await myeval(`color.convert(["RGB", 0.1, 0.2, 0.3], "T")`);
  468. expect(round(value)).toEqual(["T"]);
  469. value = await myeval(`color.convert(["RGB", 0.1, 0.2, 0.3], "G")`);
  470. expect(round(value)).toEqual(["G", 0.181]);
  471. value = await myeval(`color.convert(["RGB", 0.1, 0.2, 0.3], "RGB")`);
  472. expect(round(value)).toEqual(["RGB", 0.1, 0.2, 0.3]);
  473. value = await myeval(`color.convert(["RGB", 0.1, 0.2, 0.3], "CMYK")`);
  474. expect(round(value)).toEqual(["CMYK", 0.9, 0.8, 0.7, 0.7]);
  475. });
  476. it("should convert CMYK color for different color spaces", async () => {
  477. let value = await myeval(
  478. `color.convert(["CMYK", 0.1, 0.2, 0.3, 0.4], "T")`
  479. );
  480. expect(round(value)).toEqual(["T"]);
  481. value = await myeval(`color.convert(["CMYK", 0.1, 0.2, 0.3, 0.4], "G")`);
  482. expect(round(value)).toEqual(["G", 0.371]);
  483. value = await myeval(
  484. `color.convert(["CMYK", 0.1, 0.2, 0.3, 0.4], "RGB")`
  485. );
  486. expect(round(value)).toEqual(["RGB", 0.5, 0.3, 0.4]);
  487. value = await myeval(
  488. `color.convert(["CMYK", 0.1, 0.2, 0.3, 0.4], "CMYK")`
  489. );
  490. expect(round(value)).toEqual(["CMYK", 0.1, 0.2, 0.3, 0.4]);
  491. });
  492. it("should convert Gray color for different color spaces", async () => {
  493. let value = await myeval(`color.convert(["G", 0.1], "T")`);
  494. expect(round(value)).toEqual(["T"]);
  495. value = await myeval(`color.convert(["G", 0.1], "G")`);
  496. expect(round(value)).toEqual(["G", 0.1]);
  497. value = await myeval(`color.convert(["G", 0.1], "RGB")`);
  498. expect(round(value)).toEqual(["RGB", 0.1, 0.1, 0.1]);
  499. value = await myeval(`color.convert(["G", 0.1], "CMYK")`);
  500. expect(round(value)).toEqual(["CMYK", 0, 0, 0, 0.9]);
  501. });
  502. it("should convert Transparent color for different color spaces", async () => {
  503. let value = await myeval(`color.convert(["T"], "T")`);
  504. expect(round(value)).toEqual(["T"]);
  505. value = await myeval(`color.convert(["T"], "G")`);
  506. expect(round(value)).toEqual(["G", 0]);
  507. value = await myeval(`color.convert(["T"], "RGB")`);
  508. expect(round(value)).toEqual(["RGB", 0, 0, 0]);
  509. value = await myeval(`color.convert(["T"], "CMYK")`);
  510. expect(round(value)).toEqual(["CMYK", 0, 0, 0, 1]);
  511. });
  512. });
  513. describe("App", function () {
  514. beforeAll(function () {
  515. sandbox.createSandbox({
  516. appInfo: { language: "en-US", platform: "Linux x86_64" },
  517. objects: {},
  518. calculationOrder: [],
  519. });
  520. });
  521. it("should test language", async () => {
  522. let value = await myeval(`app.language`);
  523. expect(value).toEqual("ENU");
  524. value = await myeval(`app.language = "hello"`);
  525. expect(value).toEqual("app.language is read-only");
  526. });
  527. it("should test platform", async () => {
  528. let value = await myeval(`app.platform`);
  529. expect(value).toEqual("UNIX");
  530. value = await myeval(`app.platform = "hello"`);
  531. expect(value).toEqual("app.platform is read-only");
  532. });
  533. });
  534. describe("AForm", function () {
  535. beforeAll(function () {
  536. sandbox.createSandbox({
  537. appInfo: { language: "en-US", platform: "Linux x86_64" },
  538. objects: {},
  539. calculationOrder: [],
  540. dispatchEventName: "_dispatchMe",
  541. });
  542. });
  543. describe("AFParseDateEx", function () {
  544. it("should parse a date with a format", async () => {
  545. const check = async (date, format, expected) => {
  546. const value = await myeval(
  547. `AFParseDateEx("${date}", "${format}").toISOString().replace(/T.*$/, "")`
  548. );
  549. expect(value).toEqual(
  550. new Date(expected).toISOString().replace(/T.*$/, "")
  551. );
  552. };
  553. await check("05", "dd", "2000/01/05");
  554. await check("12", "mm", "2000/12/01");
  555. await check("2022", "yyyy", "2022/01/01");
  556. await check("a1$9bbbb21", "dd/mm/yyyy", "2021/09/01");
  557. // The following test isn't working as expected because
  558. // the quickjs date parser has been replaced by the browser one
  559. // and the date "1.9.2021" is valid in Chrome but not in Firefox.
  560. // The supported date format is not specified...
  561. // await check("1.9.2021", "dd/mm/yyyy", "2021/09/01");
  562. });
  563. });
  564. describe("AFExtractNums", function () {
  565. it("should extract numbers", async () => {
  566. let value = await myeval(`AFExtractNums("123 456 789")`);
  567. expect(value).toEqual(["123", "456", "789"]);
  568. value = await myeval(`AFExtractNums("123.456")`);
  569. expect(value).toEqual(["123", "456"]);
  570. value = await myeval(`AFExtractNums("123")`);
  571. expect(value).toEqual(["123"]);
  572. value = await myeval(`AFExtractNums(".123")`);
  573. expect(value).toEqual(["0", "123"]);
  574. value = await myeval(`AFExtractNums(",123")`);
  575. expect(value).toEqual(["0", "123"]);
  576. });
  577. });
  578. describe("AFMakeNumber", function () {
  579. it("should convert string to number", async () => {
  580. let value = await myeval(`AFMakeNumber("123.456")`);
  581. expect(value).toEqual(123.456);
  582. value = await myeval(`AFMakeNumber(123.456)`);
  583. expect(value).toEqual(123.456);
  584. value = await myeval(`AFMakeNumber("-123.456")`);
  585. expect(value).toEqual(-123.456);
  586. value = await myeval(`AFMakeNumber("-123,456")`);
  587. expect(value).toEqual(-123.456);
  588. value = await myeval(`AFMakeNumber("not a number")`);
  589. expect(value).toEqual(null);
  590. });
  591. });
  592. describe("AFMakeArrayFromList", function () {
  593. it("should split a string into an array of strings", async () => {
  594. const value = await myeval(
  595. `AFMakeArrayFromList("aaaa, bbbbbbb,cc,ddd, e")`
  596. );
  597. expect(value).toEqual(["aaaa", " bbbbbbb", "cc", "ddd", "e"]);
  598. });
  599. });
  600. describe("AFNumber_format", function () {
  601. it("should format a number", async () => {
  602. const refId = getId();
  603. const data = {
  604. objects: {
  605. field: [
  606. {
  607. id: refId,
  608. value: "",
  609. actions: {
  610. test1: [
  611. `AFNumber_Format(2, 0, 0, 0, "€", false);` +
  612. `event.source.value = event.value;`,
  613. ],
  614. test2: [
  615. `AFNumber_Format(1, 3, 0, 0, "$", true);` +
  616. `event.source.value = event.value;`,
  617. ],
  618. test3: [
  619. `AFNumber_Format(2, 0, 1, 0, "€", false);` +
  620. `event.source.value = event.value;`,
  621. ],
  622. test4: [
  623. `AFNumber_Format(2, 0, 2, 0, "€", false);` +
  624. `event.source.value = event.value;`,
  625. ],
  626. test5: [
  627. `AFNumber_Format(2, 0, 3, 0, "€", false);` +
  628. `event.source.value = event.value;`,
  629. ],
  630. },
  631. type: "text",
  632. },
  633. ],
  634. },
  635. appInfo: { language: "en-US", platform: "Linux x86_64" },
  636. calculationOrder: [],
  637. dispatchEventName: "_dispatchMe",
  638. };
  639. sandbox.createSandbox(data);
  640. await sandbox.dispatchEventInSandbox({
  641. id: refId,
  642. value: "123456.789",
  643. name: "test1",
  644. });
  645. expect(send_queue.has(refId)).toEqual(true);
  646. expect(send_queue.get(refId)).toEqual({
  647. id: refId,
  648. value: "123,456.79€",
  649. });
  650. send_queue.delete(refId);
  651. await sandbox.dispatchEventInSandbox({
  652. id: refId,
  653. value: "223456.789",
  654. name: "test2",
  655. });
  656. expect(send_queue.has(refId)).toEqual(true);
  657. expect(send_queue.get(refId)).toEqual({
  658. id: refId,
  659. value: "$223456,8",
  660. });
  661. send_queue.delete(refId);
  662. await sandbox.dispatchEventInSandbox({
  663. id: refId,
  664. value: "-323456.789",
  665. name: "test3",
  666. });
  667. expect(send_queue.has(refId)).toEqual(true);
  668. expect(send_queue.get(refId)).toEqual({
  669. id: refId,
  670. value: "323,456.79€",
  671. textColor: ["RGB", 1, 0, 0],
  672. });
  673. send_queue.delete(refId);
  674. await sandbox.dispatchEventInSandbox({
  675. id: refId,
  676. value: "-423456.789",
  677. name: "test4",
  678. });
  679. expect(send_queue.has(refId)).toEqual(true);
  680. expect(send_queue.get(refId)).toEqual({
  681. id: refId,
  682. value: "(423,456.79€)",
  683. });
  684. send_queue.delete(refId);
  685. await sandbox.dispatchEventInSandbox({
  686. id: refId,
  687. value: "-52345.678",
  688. name: "test5",
  689. });
  690. expect(send_queue.has(refId)).toEqual(true);
  691. expect(send_queue.get(refId)).toEqual({
  692. id: refId,
  693. value: "(52,345.68€)",
  694. textColor: ["RGB", 1, 0, 0],
  695. });
  696. });
  697. });
  698. describe("AFNumber_Keystroke", function () {
  699. it("should validate a number on a keystroke event", async () => {
  700. const refId = getId();
  701. const data = {
  702. objects: {
  703. field: [
  704. {
  705. id: refId,
  706. value: "",
  707. actions: {
  708. Validate: [
  709. `AFNumber_Keystroke(null, 0, null, null, null, null);`,
  710. ],
  711. },
  712. type: "text",
  713. name: "MyField",
  714. },
  715. ],
  716. },
  717. appInfo: { language: "en-US", platform: "Linux x86_64" },
  718. calculationOrder: [],
  719. dispatchEventName: "_dispatchMe",
  720. };
  721. sandbox.createSandbox(data);
  722. await sandbox.dispatchEventInSandbox({
  723. id: refId,
  724. value: "123456.789",
  725. name: "Keystroke",
  726. willCommit: true,
  727. });
  728. expect(send_queue.has(refId)).toEqual(true);
  729. expect(send_queue.get(refId)).toEqual({
  730. id: refId,
  731. siblings: null,
  732. value: 123456.789,
  733. formattedValue: null,
  734. });
  735. });
  736. it("should not validate a number on a keystroke event", async () => {
  737. const refId = getId();
  738. const data = {
  739. objects: {
  740. field: [
  741. {
  742. id: refId,
  743. value: "",
  744. actions: {
  745. Validate: [
  746. `AFNumber_Keystroke(null, 0, null, null, null, null);`,
  747. ],
  748. },
  749. type: "text",
  750. name: "MyField",
  751. },
  752. ],
  753. },
  754. appInfo: { language: "en-US", platform: "Linux x86_64" },
  755. calculationOrder: [],
  756. dispatchEventName: "_dispatchMe",
  757. };
  758. sandbox.createSandbox(data);
  759. await sandbox.dispatchEventInSandbox({
  760. id: refId,
  761. value: "123s456.789",
  762. name: "Keystroke",
  763. willCommit: true,
  764. });
  765. expect(send_queue.has("alert")).toEqual(true);
  766. expect(send_queue.get("alert")).toEqual({
  767. command: "alert",
  768. value:
  769. "The value entered does not match the format of the field [ MyField ]",
  770. });
  771. });
  772. });
  773. describe("AFPercent_Format", function () {
  774. it("should format a percentage", async () => {
  775. const refId = getId();
  776. const data = {
  777. objects: {
  778. field: [
  779. {
  780. id: refId,
  781. value: "",
  782. actions: {
  783. test1: [
  784. `AFPercent_Format(2, 1, false);` +
  785. `event.source.value = event.value;`,
  786. ],
  787. test2: [
  788. `AFPercent_Format(2, 1, true);` +
  789. `event.source.value = event.value;`,
  790. ],
  791. },
  792. type: "text",
  793. },
  794. ],
  795. },
  796. appInfo: { language: "en-US", platform: "Linux x86_64" },
  797. calculationOrder: [],
  798. dispatchEventName: "_dispatchMe",
  799. };
  800. sandbox.createSandbox(data);
  801. await sandbox.dispatchEventInSandbox({
  802. id: refId,
  803. value: "0.456789",
  804. name: "test1",
  805. });
  806. expect(send_queue.has(refId)).toEqual(true);
  807. expect(send_queue.get(refId)).toEqual({
  808. id: refId,
  809. value: "45.68%",
  810. });
  811. send_queue.delete(refId);
  812. await sandbox.dispatchEventInSandbox({
  813. id: refId,
  814. value: "0.456789",
  815. name: "test2",
  816. });
  817. expect(send_queue.has(refId)).toEqual(true);
  818. expect(send_queue.get(refId)).toEqual({
  819. id: refId,
  820. value: "%45.68",
  821. });
  822. });
  823. });
  824. describe("AFDate_Format", function () {
  825. it("should format a date", async () => {
  826. const refId = getId();
  827. const data = {
  828. objects: {
  829. field: [
  830. {
  831. id: refId,
  832. value: "",
  833. actions: {
  834. test1: [`AFDate_Format(0);event.source.value = event.value;`],
  835. test2: [
  836. `AFDate_Format(12);event.source.value = event.value;`,
  837. ],
  838. },
  839. type: "text",
  840. },
  841. ],
  842. },
  843. appInfo: { language: "en-US", platform: "Linux x86_64" },
  844. calculationOrder: [],
  845. dispatchEventName: "_dispatchMe",
  846. };
  847. sandbox.createSandbox(data);
  848. await sandbox.dispatchEventInSandbox({
  849. id: refId,
  850. value: "Sun Apr 15 2007 03:14:15",
  851. name: "test1",
  852. });
  853. expect(send_queue.has(refId)).toEqual(true);
  854. expect(send_queue.get(refId)).toEqual({
  855. id: refId,
  856. value: "4/15",
  857. });
  858. send_queue.delete(refId);
  859. await sandbox.dispatchEventInSandbox({
  860. id: refId,
  861. value: "Sun Apr 15 2007 03:14:15",
  862. name: "test2",
  863. });
  864. expect(send_queue.has(refId)).toEqual(true);
  865. expect(send_queue.get(refId)).toEqual({
  866. id: refId,
  867. value: "4/15/07 3:14 am",
  868. });
  869. });
  870. });
  871. describe("AFRange_Validate", function () {
  872. it("should validate a number in range [a, b]", async () => {
  873. const refId = getId();
  874. const data = {
  875. objects: {
  876. field: [
  877. {
  878. id: refId,
  879. value: "",
  880. actions: {
  881. Validate: [`AFRange_Validate(true, 123, true, 456);`],
  882. },
  883. type: "text",
  884. },
  885. ],
  886. },
  887. appInfo: { language: "en-US", platform: "Linux x86_64" },
  888. calculationOrder: [],
  889. dispatchEventName: "_dispatchMe",
  890. };
  891. sandbox.createSandbox(data);
  892. await sandbox.dispatchEventInSandbox({
  893. id: refId,
  894. value: "321",
  895. name: "Keystroke",
  896. willCommit: true,
  897. });
  898. expect(send_queue.has(refId)).toEqual(true);
  899. expect(send_queue.get(refId)).toEqual({
  900. id: refId,
  901. siblings: null,
  902. value: 321,
  903. formattedValue: null,
  904. });
  905. });
  906. it("should invalidate a number out of range [a, b]", async () => {
  907. const refId = getId();
  908. const data = {
  909. objects: {
  910. field: [
  911. {
  912. id: refId,
  913. value: "",
  914. actions: {
  915. Validate: [`AFRange_Validate(true, 123, true, 456);`],
  916. },
  917. type: "text",
  918. },
  919. ],
  920. },
  921. appInfo: { language: "en-US", platform: "Linux x86_64" },
  922. calculationOrder: [],
  923. dispatchEventName: "_dispatchMe",
  924. };
  925. sandbox.createSandbox(data);
  926. await sandbox.dispatchEventInSandbox({
  927. id: refId,
  928. value: "12",
  929. name: "Keystroke",
  930. willCommit: true,
  931. });
  932. expect(send_queue.has("alert")).toEqual(true);
  933. expect(send_queue.get("alert")).toEqual({
  934. command: "alert",
  935. value:
  936. "Invalid value: must be greater than or equal to 123 and less than or equal to 456.",
  937. });
  938. });
  939. });
  940. describe("AFSimple_Calculate", function () {
  941. it("should compute the sum of several fields", async () => {
  942. const refIds = [0, 1, 2, 3, 4].map(_ => getId());
  943. const data = {
  944. objects: {
  945. field1: [
  946. {
  947. id: refIds[0],
  948. value: "",
  949. actions: {},
  950. type: "text",
  951. },
  952. ],
  953. field2: [
  954. {
  955. id: refIds[1],
  956. value: "",
  957. actions: {},
  958. type: "text",
  959. },
  960. ],
  961. field3: [
  962. {
  963. id: refIds[2],
  964. value: "",
  965. actions: {},
  966. type: "text",
  967. },
  968. ],
  969. field4: [
  970. {
  971. id: refIds[3],
  972. value: "",
  973. actions: {
  974. Calculate: [
  975. `AFSimple_Calculate("SUM", ["field1", "field2", "field3", "unknown"]);`,
  976. ],
  977. },
  978. type: "text",
  979. },
  980. ],
  981. field5: [
  982. {
  983. id: refIds[4],
  984. value: "",
  985. actions: {
  986. Calculate: [
  987. `AFSimple_Calculate("SUM", "field1, field2, field3, unknown");`,
  988. ],
  989. },
  990. type: "text",
  991. },
  992. ],
  993. },
  994. appInfo: { language: "en-US", platform: "Linux x86_64" },
  995. calculationOrder: [refIds[3], refIds[4]],
  996. dispatchEventName: "_dispatchMe",
  997. };
  998. sandbox.createSandbox(data);
  999. await sandbox.dispatchEventInSandbox({
  1000. id: refIds[0],
  1001. value: "1",
  1002. name: "Keystroke",
  1003. willCommit: true,
  1004. });
  1005. expect(send_queue.has(refIds[3])).toEqual(true);
  1006. expect(send_queue.get(refIds[3])).toEqual({
  1007. id: refIds[3],
  1008. siblings: null,
  1009. value: 1,
  1010. formattedValue: null,
  1011. });
  1012. await sandbox.dispatchEventInSandbox({
  1013. id: refIds[1],
  1014. value: "2",
  1015. name: "Keystroke",
  1016. willCommit: true,
  1017. });
  1018. expect(send_queue.has(refIds[3])).toEqual(true);
  1019. expect(send_queue.get(refIds[3])).toEqual({
  1020. id: refIds[3],
  1021. siblings: null,
  1022. value: 3,
  1023. formattedValue: null,
  1024. });
  1025. await sandbox.dispatchEventInSandbox({
  1026. id: refIds[2],
  1027. value: "3",
  1028. name: "Keystroke",
  1029. willCommit: true,
  1030. });
  1031. expect(send_queue.has(refIds[3])).toEqual(true);
  1032. expect(send_queue.get(refIds[3])).toEqual({
  1033. id: refIds[3],
  1034. siblings: null,
  1035. value: 6,
  1036. formattedValue: null,
  1037. });
  1038. expect(send_queue.has(refIds[4])).toEqual(true);
  1039. expect(send_queue.get(refIds[4])).toEqual({
  1040. id: refIds[4],
  1041. siblings: null,
  1042. value: 6,
  1043. formattedValue: null,
  1044. });
  1045. });
  1046. it("should compute the sum of several fields in fields tree", async () => {
  1047. const refIds = [0, 1, 2, 3, 4, 5].map(_ => getId());
  1048. const data = {
  1049. objects: {
  1050. field1: [
  1051. {
  1052. id: refIds[0],
  1053. kidIds: [refIds[1], refIds[2]],
  1054. },
  1055. ],
  1056. "field1.field2": [
  1057. {
  1058. id: refIds[1],
  1059. kidIds: [refIds[3]],
  1060. },
  1061. ],
  1062. "field1.field3": [
  1063. {
  1064. id: refIds[2],
  1065. value: "",
  1066. actions: {},
  1067. type: "text",
  1068. },
  1069. ],
  1070. "field1.field2.field4": [
  1071. {
  1072. id: refIds[3],
  1073. kidIds: [refIds[4]],
  1074. },
  1075. ],
  1076. "field1.field2.field4.field5": [
  1077. {
  1078. id: refIds[4],
  1079. value: "",
  1080. actions: {},
  1081. type: "text",
  1082. },
  1083. ],
  1084. field6: [
  1085. {
  1086. id: refIds[5],
  1087. value: "",
  1088. actions: {
  1089. Calculate: [`AFSimple_Calculate("SUM", "field1");`],
  1090. },
  1091. type: "text",
  1092. },
  1093. ],
  1094. },
  1095. appInfo: { language: "en-US", platform: "Linux x86_64" },
  1096. calculationOrder: [refIds[5]],
  1097. dispatchEventName: "_dispatchMe",
  1098. };
  1099. sandbox.createSandbox(data);
  1100. await sandbox.dispatchEventInSandbox({
  1101. id: refIds[2],
  1102. value: "123",
  1103. name: "Keystroke",
  1104. willCommit: true,
  1105. });
  1106. expect(send_queue.has(refIds[5])).toEqual(true);
  1107. expect(send_queue.get(refIds[5])).toEqual({
  1108. id: refIds[5],
  1109. siblings: null,
  1110. value: 123,
  1111. formattedValue: null,
  1112. });
  1113. await sandbox.dispatchEventInSandbox({
  1114. id: refIds[4],
  1115. value: "456",
  1116. name: "Keystroke",
  1117. willCommit: true,
  1118. });
  1119. expect(send_queue.has(refIds[5])).toEqual(true);
  1120. expect(send_queue.get(refIds[5])).toEqual({
  1121. id: refIds[5],
  1122. siblings: null,
  1123. value: 579,
  1124. formattedValue: null,
  1125. });
  1126. });
  1127. });
  1128. describe("AFSpecial_KeystrokeEx", function () {
  1129. it("should validate a phone number on a keystroke event", async () => {
  1130. const refId = getId();
  1131. const data = {
  1132. objects: {
  1133. field: [
  1134. {
  1135. id: refId,
  1136. value: "",
  1137. actions: {
  1138. Keystroke: [`AFSpecial_KeystrokeEx("9AXO");`],
  1139. },
  1140. type: "text",
  1141. },
  1142. ],
  1143. },
  1144. appInfo: { language: "en-US", platform: "Linux x86_64" },
  1145. calculationOrder: [],
  1146. dispatchEventName: "_dispatchMe",
  1147. };
  1148. sandbox.createSandbox(data);
  1149. await sandbox.dispatchEventInSandbox({
  1150. id: refId,
  1151. value: "",
  1152. change: "3",
  1153. name: "Keystroke",
  1154. willCommit: false,
  1155. selStart: 0,
  1156. selEnd: 0,
  1157. });
  1158. expect(send_queue.has(refId)).toEqual(true);
  1159. send_queue.delete(refId);
  1160. await sandbox.dispatchEventInSandbox({
  1161. id: refId,
  1162. value: "3",
  1163. change: "F",
  1164. name: "Keystroke",
  1165. willCommit: false,
  1166. selStart: 1,
  1167. selEnd: 1,
  1168. });
  1169. expect(send_queue.has(refId)).toEqual(true);
  1170. send_queue.delete(refId);
  1171. await sandbox.dispatchEventInSandbox({
  1172. id: refId,
  1173. value: "3F",
  1174. change: "?",
  1175. name: "Keystroke",
  1176. willCommit: false,
  1177. selStart: 2,
  1178. selEnd: 2,
  1179. });
  1180. expect(send_queue.has(refId)).toEqual(true);
  1181. send_queue.delete(refId);
  1182. await sandbox.dispatchEventInSandbox({
  1183. id: refId,
  1184. value: "3F?",
  1185. change: "@",
  1186. name: "Keystroke",
  1187. willCommit: false,
  1188. selStart: 3,
  1189. selEnd: 3,
  1190. });
  1191. expect(send_queue.has(refId)).toEqual(true);
  1192. expect(send_queue.get(refId)).toEqual({
  1193. id: refId,
  1194. siblings: null,
  1195. value: "3F?",
  1196. selRange: [3, 3],
  1197. });
  1198. send_queue.delete(refId);
  1199. await sandbox.dispatchEventInSandbox({
  1200. id: refId,
  1201. value: "3F?",
  1202. change: "0",
  1203. name: "Keystroke",
  1204. willCommit: false,
  1205. selStart: 3,
  1206. selEnd: 3,
  1207. });
  1208. expect(send_queue.has(refId)).toEqual(true);
  1209. send_queue.delete(refId);
  1210. await sandbox.dispatchEventInSandbox({
  1211. id: refId,
  1212. value: "3F?0",
  1213. name: "Keystroke",
  1214. willCommit: true,
  1215. selStart: 4,
  1216. selEnd: 4,
  1217. });
  1218. expect(send_queue.has(refId)).toEqual(true);
  1219. expect(send_queue.get(refId)).toEqual({
  1220. id: refId,
  1221. siblings: null,
  1222. value: "3F?0",
  1223. formattedValue: null,
  1224. });
  1225. });
  1226. });
  1227. describe("AFSpecial_Keystroke", function () {
  1228. it("should validate a zip code on a keystroke event", async () => {
  1229. const refId = getId();
  1230. const data = {
  1231. objects: {
  1232. field: [
  1233. {
  1234. id: refId,
  1235. value: "",
  1236. actions: {
  1237. Keystroke: [`AFSpecial_Keystroke(0);`],
  1238. },
  1239. type: "text",
  1240. },
  1241. ],
  1242. },
  1243. appInfo: { language: "en-US", platform: "Linux x86_64" },
  1244. calculationOrder: [],
  1245. dispatchEventName: "_dispatchMe",
  1246. };
  1247. sandbox.createSandbox(data);
  1248. let value = "";
  1249. const changes = "12345";
  1250. let i = 0;
  1251. for (; i < changes.length; i++) {
  1252. const change = changes.charAt(i);
  1253. await sandbox.dispatchEventInSandbox({
  1254. id: refId,
  1255. value,
  1256. change,
  1257. name: "Keystroke",
  1258. willCommit: false,
  1259. selStart: i,
  1260. selEnd: i,
  1261. });
  1262. expect(send_queue.has(refId)).toEqual(true);
  1263. send_queue.delete(refId);
  1264. value += change;
  1265. }
  1266. await sandbox.dispatchEventInSandbox({
  1267. id: refId,
  1268. value,
  1269. change: "A",
  1270. name: "Keystroke",
  1271. willCommit: false,
  1272. selStart: i,
  1273. selEnd: i,
  1274. });
  1275. expect(send_queue.has(refId)).toEqual(true);
  1276. expect(send_queue.get(refId)).toEqual({
  1277. id: refId,
  1278. siblings: null,
  1279. value,
  1280. selRange: [i, i],
  1281. });
  1282. send_queue.delete(refId);
  1283. });
  1284. it("should validate a US phone number (long) on a keystroke event", async () => {
  1285. const refId = getId();
  1286. const data = {
  1287. objects: {
  1288. field: [
  1289. {
  1290. id: refId,
  1291. value: "",
  1292. actions: {
  1293. Keystroke: [`AFSpecial_Keystroke(2);`],
  1294. },
  1295. type: "text",
  1296. },
  1297. ],
  1298. },
  1299. appInfo: { language: "en-US", platform: "Linux x86_64" },
  1300. calculationOrder: [],
  1301. dispatchEventName: "_dispatchMe",
  1302. };
  1303. sandbox.createSandbox(data);
  1304. let value = "";
  1305. const changes = "(123) 456-7890";
  1306. let i = 0;
  1307. for (; i < changes.length; i++) {
  1308. const change = changes.charAt(i);
  1309. await sandbox.dispatchEventInSandbox({
  1310. id: refId,
  1311. value,
  1312. change,
  1313. name: "Keystroke",
  1314. willCommit: false,
  1315. selStart: i,
  1316. selEnd: i,
  1317. });
  1318. expect(send_queue.has(refId)).toEqual(true);
  1319. send_queue.delete(refId);
  1320. value += change;
  1321. }
  1322. await sandbox.dispatchEventInSandbox({
  1323. id: refId,
  1324. value,
  1325. change: "A",
  1326. name: "Keystroke",
  1327. willCommit: false,
  1328. selStart: i,
  1329. selEnd: i,
  1330. });
  1331. expect(send_queue.has(refId)).toEqual(true);
  1332. expect(send_queue.get(refId)).toEqual({
  1333. id: refId,
  1334. siblings: null,
  1335. value,
  1336. selRange: [i, i],
  1337. });
  1338. send_queue.delete(refId);
  1339. });
  1340. it("should validate a US phone number (short) on a keystroke event", async () => {
  1341. const refId = getId();
  1342. const data = {
  1343. objects: {
  1344. field: [
  1345. {
  1346. id: refId,
  1347. value: "",
  1348. actions: {
  1349. Keystroke: [`AFSpecial_Keystroke(2);`],
  1350. },
  1351. type: "text",
  1352. },
  1353. ],
  1354. },
  1355. appInfo: { language: "en-US", platform: "Linux x86_64" },
  1356. calculationOrder: [],
  1357. dispatchEventName: "_dispatchMe",
  1358. };
  1359. sandbox.createSandbox(data);
  1360. let value = "";
  1361. const changes = "123-4567";
  1362. let i = 0;
  1363. for (; i < changes.length; i++) {
  1364. const change = changes.charAt(i);
  1365. await sandbox.dispatchEventInSandbox({
  1366. id: refId,
  1367. value,
  1368. change,
  1369. name: "Keystroke",
  1370. willCommit: false,
  1371. selStart: i,
  1372. selEnd: i,
  1373. });
  1374. expect(send_queue.has(refId)).toEqual(true);
  1375. send_queue.delete(refId);
  1376. value += change;
  1377. }
  1378. await sandbox.dispatchEventInSandbox({
  1379. id: refId,
  1380. value,
  1381. change: "A",
  1382. name: "Keystroke",
  1383. willCommit: false,
  1384. selStart: i,
  1385. selEnd: i,
  1386. });
  1387. expect(send_queue.has(refId)).toEqual(true);
  1388. expect(send_queue.get(refId)).toEqual({
  1389. id: refId,
  1390. siblings: null,
  1391. value,
  1392. selRange: [i, i],
  1393. });
  1394. send_queue.delete(refId);
  1395. });
  1396. });
  1397. describe("eMailValidate", function () {
  1398. it("should validate an e-mail address", async () => {
  1399. let value = await myeval(`eMailValidate(123)`);
  1400. expect(value).toEqual(false);
  1401. value = await myeval(`eMailValidate("foo@bar.com")`);
  1402. expect(value).toEqual(true);
  1403. value = await myeval(`eMailValidate("foo bar")`);
  1404. expect(value).toEqual(false);
  1405. });
  1406. });
  1407. describe("AFExactMatch", function () {
  1408. it("should check matching between regexs and a string", async () => {
  1409. let value = await myeval(`AFExactMatch(/\\d+/, "123")`);
  1410. expect(value).toEqual(true);
  1411. value = await myeval(`AFExactMatch(/\\d+/, "foo")`);
  1412. expect(value).toEqual(0);
  1413. value = await myeval(`AFExactMatch([/\\d+/, /[fo]*/], "foo")`);
  1414. expect(value).toEqual(2);
  1415. value = await myeval(`AFExactMatch([/\\d+/, /[fo]*/], "bar")`);
  1416. expect(value).toEqual(0);
  1417. });
  1418. });
  1419. });
  1420. });