Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

279 lines
6.7 KiB

  1. import { getFormValues, setFormValues } from '../../src';
  2. import * as utils from '../utils'
  3. describe('month', () => {
  4. describe('basic', () => {
  5. beforeEach(utils.setup(`
  6. <!DOCTYPE html>
  7. <html lang="en-PH">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>Month/Basic</title>
  11. </head>
  12. <body>
  13. <form>
  14. <label>
  15. <span>Hello</span>
  16. <input type="month" name="hello" value="2003-05" />
  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. action: (cy: any) => cy.get('[type="submit"]'),
  26. test: (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-05',
  36. },
  37. });
  38. });
  39. it('should enable Date representation', () => {
  40. utils.test({
  41. action: (cy: any) => cy.get('[type="submit"]'),
  42. test: (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>Month/Disabled</title>
  59. </head>
  60. <body>
  61. <form>
  62. <label>
  63. <span>Hello</span>
  64. <input
  65. type="month" name="hello" value="2003-05"
  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. action: (cy: any) => cy.get('[type="submit"]'),
  77. test: (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>Month/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="month" name="hello" value="2003-05" form="form" />
  104. </label>
  105. </body>
  106. </html>
  107. `))
  108. it('should have single form value', () => {
  109. utils.test({
  110. action: (cy: any) => cy.get('[type="submit"]'),
  111. test: (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-05',
  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>Text/Readonly</title>
  132. </head>
  133. <body>
  134. <form>
  135. <label>
  136. <span>Hello</span>
  137. <input
  138. type="text" name="hello" value="2003-05"
  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. action: (cy: any) => cy.get('[type="submit"]'),
  150. test: (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-05',
  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>Month/Programmatic Value Setting</title>
  171. </head>
  172. <body>
  173. <form>
  174. <label>
  175. <span>Hello</span>
  176. <input type="month" 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. action: (cy: any) => cy.get('[type="submit"]'),
  186. preAction: (form: HTMLFormElement) => {
  187. setFormValues(form, { hello: '2003-05', })
  188. },
  189. test: (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: '2003-05',
  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>Month/Duplicate</title>
  210. </head>
  211. <body>
  212. <form>
  213. <label>
  214. <span>Hello 1</span>
  215. <input id="hello1" type="month" name="hello" value="2003-05"/>
  216. </label>
  217. <label>
  218. <span>Hello 2</span>
  219. <input id="hello2" type="month" name="hello" value="2003-06"/>
  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. action: (cy: any) => cy.get('[type="submit"]'),
  229. test: (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: ['2003-05', '2003-06'],
  239. },
  240. });
  241. });
  242. it('should set both values', () => {
  243. utils.test({
  244. preAction: (form: HTMLFormElement) => {
  245. setFormValues(form, {
  246. hello: ['2003-04', '2003-02'],
  247. })
  248. },
  249. action: (cy: any) => cy.get('[type="submit"]'),
  250. test: (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: ['2003-04', '2003-02'],
  260. },
  261. });
  262. });
  263. });
  264. })