Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

date.test.ts 6.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import { getFormValues, setFormValues } from '../../src';
  2. import * as utils from '../utils'
  3. describe('date', () => {
  4. describe('basic', () => {
  5. beforeEach(utils.setup(`
  6. <!DOCTYPE html>
  7. <html lang="en-PH">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>Date/Basic</title>
  11. </head>
  12. <body>
  13. <form>
  14. <label>
  15. <span>Hello</span>
  16. <input type="date" name="hello" value="2003-04-06" />
  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. actionBeforeSubmit: (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: '2003-04-06',
  36. },
  37. });
  38. });
  39. it('should enable Date representation', () => {
  40. utils.test({
  41. actionBeforeSubmit: (cy: any) => cy.get('[type="submit"]'),
  42. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  43. const values = getFormValues(form, { submitter, forceDateValues: true });
  44. // somehow, checking instanceof Date fails here, because we're using an artificial date
  45. // object?
  46. const testDate = new Date(values.hello as Date);
  47. expect((values.hello as Date).getTime()).toBe(testDate.getTime());
  48. },
  49. });
  50. });
  51. })
  52. describe('disabled', () => {
  53. beforeEach(utils.setup(`
  54. <!DOCTYPE html>
  55. <html lang="en-PH">
  56. <head>
  57. <meta charset="UTF-8">
  58. <title>Date/Disabled</title>
  59. </head>
  60. <body>
  61. <form>
  62. <label>
  63. <span>Hello</span>
  64. <input
  65. type="date" name="hello" value="2003-06-09"
  66. disabled
  67. />
  68. </label>
  69. <button type="submit">Submit</button>
  70. </form>
  71. </body>
  72. </html>
  73. `))
  74. it('should have blank form value', () => {
  75. utils.test({
  76. actionBeforeSubmit: (cy: any) => cy.get('[type="submit"]'),
  77. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  78. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  79. .toString();
  80. const after = utils.makeSearchParams(search)
  81. .toString();
  82. expect(before)
  83. .toEqual(after);
  84. },
  85. expectedStaticValue: {},
  86. });
  87. });
  88. })
  89. describe('outside', () => {
  90. beforeEach(utils.setup(`
  91. <!DOCTYPE html>
  92. <html lang="en-PH">
  93. <head>
  94. <meta charset="UTF-8">
  95. <title>Date/Outside</title>
  96. </head>
  97. <body>
  98. <form id="form">
  99. <button type="submit">Submit</button>
  100. </form>
  101. <label>
  102. <span>Hello</span>
  103. <input type="date" name="hello" value="2003-04-20" form="form" />
  104. </label>
  105. </body>
  106. </html>
  107. `))
  108. it('should have single form value', () => {
  109. utils.test({
  110. actionBeforeSubmit: (cy: any) => cy.get('[type="submit"]'),
  111. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  112. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  113. .toString();
  114. const after = utils.makeSearchParams(search)
  115. .toString();
  116. expect(before)
  117. .toEqual(after);
  118. },
  119. expectedStaticValue: {
  120. hello: '2003-04-20',
  121. },
  122. });
  123. });
  124. });
  125. describe('readonly', () => {
  126. beforeEach(utils.setup(`
  127. <!DOCTYPE html>
  128. <html lang="en-PH">
  129. <head>
  130. <meta charset="UTF-8">
  131. <title>Date/Readonly</title>
  132. </head>
  133. <body>
  134. <form>
  135. <label>
  136. <span>Hello</span>
  137. <input
  138. type="text" name="hello" value="2003-11-11"
  139. readonly
  140. />
  141. </label>
  142. <button type="submit">Submit</button>
  143. </form>
  144. </body>
  145. </html>
  146. `))
  147. it('should have single form value', () => {
  148. utils.test({
  149. actionBeforeSubmit: (cy: any) => cy.get('[type="submit"]'),
  150. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  151. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  152. .toString();
  153. const after = utils.makeSearchParams(search)
  154. .toString();
  155. expect(before)
  156. .toEqual(after);
  157. },
  158. expectedStaticValue: {
  159. hello: '2003-11-11',
  160. },
  161. });
  162. });
  163. });
  164. describe('programmatic value setting', () => {
  165. beforeEach(utils.setup(`
  166. <!DOCTYPE html>
  167. <html lang="en-PH">
  168. <head>
  169. <meta charset="UTF-8">
  170. <title>Date/Programmatic Value Setting</title>
  171. </head>
  172. <body>
  173. <form>
  174. <label>
  175. <span>Hello</span>
  176. <input type="date" name="hello" />
  177. </label>
  178. <button type="submit">Submit</button>
  179. </form>
  180. </body>
  181. </html>
  182. `));
  183. it('should have form values set', () => {
  184. utils.test({
  185. actionBeforeSubmit: (cy: any) => cy.get('[type="submit"]'),
  186. onLoaded: (form: HTMLFormElement) => {
  187. setFormValues(form, { hello: new Date('2000-01-01'), })
  188. },
  189. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  190. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  191. .toString();
  192. const after = utils.makeSearchParams(search)
  193. .toString();
  194. expect(before)
  195. .toEqual(after);
  196. },
  197. expectedStaticValue: {
  198. hello: '2000-01-01',
  199. },
  200. });
  201. });
  202. });
  203. describe('duplicate', () => {
  204. beforeEach(utils.setup(`
  205. <!DOCTYPE html>
  206. <html lang="en-PH">
  207. <head>
  208. <meta charset="UTF-8">
  209. <title>Date/Duplicate</title>
  210. </head>
  211. <body>
  212. <form>
  213. <label>
  214. <span>Hello 1</span>
  215. <input id="hello1" type="date" name="hello" value="2007-07-07"/>
  216. </label>
  217. <label>
  218. <span>Hello 2</span>
  219. <input id="hello2" type="date" name="hello" value="2008-08-08"/>
  220. </label>
  221. <button type="submit">Submit</button>
  222. </form>
  223. </body>
  224. </html>
  225. `));
  226. it('should get both values', () => {
  227. utils.test({
  228. actionBeforeSubmit: (cy: any) => cy.get('[type="submit"]'),
  229. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  230. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  231. .toString();
  232. const after = utils.makeSearchParams(search)
  233. .toString();
  234. expect(before)
  235. .toEqual(after);
  236. },
  237. expectedStaticValue: {
  238. hello: ['2007-07-07', '2008-08-08'],
  239. },
  240. });
  241. });
  242. it('should set both values', () => {
  243. utils.test({
  244. onLoaded: (form: HTMLFormElement) => {
  245. setFormValues(form, {
  246. hello: ['2006-06-06', '2005-05-05'],
  247. })
  248. },
  249. actionBeforeSubmit: (cy: any) => cy.get('[type="submit"]'),
  250. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  251. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  252. .toString();
  253. const after = utils.makeSearchParams(search)
  254. .toString();
  255. expect(before)
  256. .toEqual(after);
  257. },
  258. expectedStaticValue: {
  259. hello: ['2006-06-06', '2005-05-05'],
  260. },
  261. });
  262. });
  263. });
  264. })