Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

350 líneas
8.5 KiB

  1. import { getFormValues, LineEnding, setFormValues } from '../../src';
  2. import * as utils from '../utils'
  3. describe('textarea', () => {
  4. describe('basic', () => {
  5. beforeEach(utils.setup(`
  6. <!DOCTYPE html>
  7. <html lang="en-PH">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>Textarea/Basic</title>
  11. </head>
  12. <body>
  13. <form>
  14. <label>
  15. <span>Hello</span>
  16. <textarea name="hello">Hi</textarea>
  17. </label>
  18. <button type="submit">Submit</button>
  19. </form>
  20. </body>
  21. </html>
  22. `))
  23. it('should have single form value', () => {
  24. utils.test({
  25. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  26. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  27. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  28. .toString();
  29. const after = utils.makeSearchParams(search)
  30. .toString();
  31. expect(before)
  32. .toEqual(after);
  33. },
  34. expectedStaticValue: {
  35. hello: 'Hi',
  36. },
  37. });
  38. });
  39. })
  40. describe('disabled', () => {
  41. beforeEach(utils.setup(`
  42. <!DOCTYPE html>
  43. <html lang="en-PH">
  44. <head>
  45. <meta charset="UTF-8">
  46. <title>Textarea/Disabled</title>
  47. </head>
  48. <body>
  49. <form>
  50. <label>
  51. <span>Hello</span>
  52. <textarea
  53. name="hello"
  54. disabled
  55. >Hi</textarea>
  56. </label>
  57. <button type="submit">Submit</button>
  58. </form>
  59. </body>
  60. </html>
  61. `))
  62. it('should have blank form value', () => {
  63. utils.test({
  64. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  65. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  66. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  67. .toString();
  68. const after = utils.makeSearchParams(search)
  69. .toString();
  70. expect(before)
  71. .toEqual(after);
  72. },
  73. expectedStaticValue: {},
  74. });
  75. });
  76. })
  77. describe('outside', () => {
  78. beforeEach(utils.setup(`
  79. <!DOCTYPE html>
  80. <html lang="en-PH">
  81. <head>
  82. <meta charset="UTF-8">
  83. <title>Textarea/Outside</title>
  84. </head>
  85. <body>
  86. <form id="form">
  87. <button type="submit">Submit</button>
  88. </form>
  89. <label>
  90. <span>Hello</span>
  91. <textarea name="hello" form="form">Hi</textarea>
  92. </label>
  93. </body>
  94. </html>
  95. `))
  96. it('should have single form value', () => {
  97. utils.test({
  98. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  99. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  100. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  101. .toString();
  102. const after = utils.makeSearchParams(search)
  103. .toString();
  104. expect(before)
  105. .toEqual(after);
  106. },
  107. expectedStaticValue: {
  108. hello: 'Hi',
  109. },
  110. });
  111. });
  112. });
  113. describe('readonly', () => {
  114. beforeEach(utils.setup(`
  115. <!DOCTYPE html>
  116. <html lang="en-PH">
  117. <head>
  118. <meta charset="UTF-8">
  119. <title>Textarea/Readonly</title>
  120. </head>
  121. <body>
  122. <form>
  123. <label>
  124. <span>Hello</span>
  125. <textarea
  126. name="hello"
  127. readonly
  128. >Hi</textarea>
  129. </label>
  130. <button type="submit">Submit</button>
  131. </form>
  132. </body>
  133. </html>
  134. `))
  135. it('should have single form value', () => {
  136. utils.test({
  137. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  138. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  139. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  140. .toString();
  141. const after = utils.makeSearchParams(search)
  142. .toString();
  143. expect(before)
  144. .toEqual(after);
  145. },
  146. expectedStaticValue: {
  147. hello: 'Hi',
  148. },
  149. });
  150. });
  151. });
  152. describe('programmatic value setting', () => {
  153. beforeEach(utils.setup(`
  154. <!DOCTYPE html>
  155. <html lang="en-PH">
  156. <head>
  157. <meta charset="UTF-8">
  158. <title>Textarea/Programmatic Value Setting</title>
  159. </head>
  160. <body>
  161. <form>
  162. <label>
  163. <span>Hello</span>
  164. <textarea name="hello"></textarea>
  165. </label>
  166. <button type="submit">Submit</button>
  167. </form>
  168. </body>
  169. </html>
  170. `));
  171. it('should have form values set', () => {
  172. utils.test({
  173. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  174. onLoaded: (form: HTMLFormElement) => {
  175. setFormValues(form, { hello: 'Hi', })
  176. },
  177. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  178. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  179. .toString();
  180. const after = utils.makeSearchParams(search)
  181. .toString();
  182. expect(before)
  183. .toEqual(after);
  184. },
  185. expectedStaticValue: {
  186. hello: 'Hi',
  187. },
  188. });
  189. });
  190. });
  191. describe('lines', () => {
  192. beforeEach(utils.setup(`
  193. <!DOCTYPE html>
  194. <html lang="en-PH">
  195. <head>
  196. <meta charset="UTF-8">
  197. <title>Textarea/Lines</title>
  198. </head>
  199. <body>
  200. <form>
  201. <label>
  202. <span>Hello</span>
  203. <textarea name="hello"></textarea>
  204. </label>
  205. <button type="submit">Submit</button>
  206. </form>
  207. </body>
  208. </html>
  209. `));
  210. it('should read LF line breaks', () => {
  211. utils.test({
  212. querySubmitter: (cy: any) => {
  213. cy.get('[name="hello"]')
  214. .type('Hi\nHello', { parseSpecialCharSequences: false })
  215. return cy.get('[type="submit"]')
  216. },
  217. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  218. const before = utils.makeSearchParams(getFormValues(form, { submitter, lineEndings: LineEnding.LF }))
  219. .toString();
  220. const after = utils.makeSearchParams(search)
  221. .toString();
  222. expect(before)
  223. .toEqual(after);
  224. },
  225. expectedStaticValue: {
  226. hello: 'Hi\nHello',
  227. },
  228. });
  229. });
  230. it('should read CR line breaks', () => {
  231. utils.test({
  232. querySubmitter: (cy: any) => {
  233. cy.get('[name="hello"]')
  234. .type('Hi\rHello', { parseSpecialCharSequences: false })
  235. return cy.get('[type="submit"]')
  236. },
  237. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  238. const before = utils.makeSearchParams(getFormValues(form, { submitter, lineEndings: LineEnding.CR }))
  239. .toString();
  240. const after = utils.makeSearchParams(search)
  241. .toString();
  242. expect(before)
  243. .toEqual(after);
  244. },
  245. expectedStaticValue: {
  246. hello: 'Hi\rHello',
  247. },
  248. });
  249. });
  250. it('should read CRLF line breaks', () => {
  251. utils.test({
  252. querySubmitter: (cy: any) => {
  253. cy.get('[name="hello"]')
  254. .type('Hi\r\nHello', { parseSpecialCharSequences: false })
  255. return cy.get('[type="submit"]')
  256. },
  257. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  258. const before = utils.makeSearchParams(getFormValues(form, { submitter, lineEndings: LineEnding.CRLF }))
  259. .toString();
  260. const after = utils.makeSearchParams(search)
  261. .toString();
  262. expect(before)
  263. .toEqual(after);
  264. },
  265. expectedStaticValue: {
  266. hello: 'Hi\r\nHello',
  267. },
  268. });
  269. });
  270. });
  271. describe('duplicate', () => {
  272. beforeEach(utils.setup(`
  273. <!DOCTYPE html>
  274. <html lang="en-PH">
  275. <head>
  276. <meta charset="UTF-8">
  277. <title>Textarea/Duplicate</title>
  278. </head>
  279. <body>
  280. <form>
  281. <label>
  282. <span>Hello 1</span>
  283. <textarea id="hello1" name="hello">value</textarea>
  284. </label>
  285. <label>
  286. <span>Hello 2</span>
  287. <textarea id="hello2" name="hello">another value</textarea>
  288. </label>
  289. <button type="submit">Submit</button>
  290. </form>
  291. </body>
  292. </html>
  293. `));
  294. it('should get both values', () => {
  295. utils.test({
  296. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  297. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  298. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  299. .toString();
  300. const after = utils.makeSearchParams(search)
  301. .toString();
  302. expect(before)
  303. .toEqual(after);
  304. },
  305. expectedStaticValue: {
  306. hello: ['value', 'another value'],
  307. },
  308. });
  309. });
  310. it('should set both values', () => {
  311. utils.test({
  312. onLoaded: (form: HTMLFormElement) => {
  313. setFormValues(form, {
  314. hello: ['new value 1', 'another value 2'],
  315. })
  316. },
  317. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  318. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  319. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  320. .toString();
  321. const after = utils.makeSearchParams(search)
  322. .toString();
  323. expect(before)
  324. .toEqual(after);
  325. },
  326. expectedStaticValue: {
  327. hello: ['new value 1', 'another value 2'],
  328. },
  329. });
  330. });
  331. });
  332. })