Use forms with or without client-side JavaScript--no code duplication required!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

form.cy.ts 3.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. describe('form', () => {
  2. beforeEach(() => {
  3. cy.task('resetDb');
  4. });
  5. describe('script', () => {
  6. it('submits form with complete data', () => {
  7. cy.visit('http://localhost:3000/notes');
  8. cy.intercept('POST', '/api/notes').as('action');
  9. cy.get('form').should('exist').within(() => {
  10. cy.get('input[name="title"]').type('test title');
  11. cy.get('input[name="image"]').selectFile('cypress/fixtures/file.jpg');
  12. cy.get('textarea[name="content"]').type('test content');
  13. cy.get('button[type="submit"]').click();
  14. cy.wait('@action').its('response.statusCode').should('eq', 201);
  15. });
  16. cy.url().should('match', /\/notes\/[a-z0-9-]+$/);
  17. });
  18. it('submits form with incomplete data', () => {
  19. cy.visit('http://localhost:3000/notes');
  20. cy.intercept('POST', '/api/notes').as('action');
  21. cy.get('form').should('exist').within(() => {
  22. cy.get('input[name="title"]').type('test title');
  23. cy.get('textarea[name="content"]').type('test content');
  24. cy.get('button[type="submit"]').click();
  25. cy.wait('@action').its('response.statusCode').should('eq', 201);
  26. });
  27. cy.url().should('match', /\/notes\/[a-z0-9-]+$/);
  28. });
  29. it('handles redirects', () => {
  30. cy.visit('http://localhost:3000/notes');
  31. cy.intercept('DELETE', '/api/notes/*').as('action');
  32. cy.get('form').should('exist').within(() => {
  33. cy.get('input[name="title"]').type('test title');
  34. cy.get('textarea[name="content"]').type('test content');
  35. cy.get('button[type="submit"]').click();
  36. });
  37. cy.get('button').contains('Delete').click();
  38. cy.wait('@action').its('response.statusCode').should('eq', 204);
  39. cy.url().should('match', /\/notes$/);
  40. });
  41. });
  42. describe('noscript', () => {
  43. it('submits form with complete data', () => {
  44. cy.visit('http://localhost:3000/notes?noscript=true');
  45. cy.intercept('POST', '/a/notes?noscript=true').as('action');
  46. cy.intercept('POST', '/notes/*').as('destination');
  47. cy.get('form').should('exist').within(() => {
  48. cy.get('input[name="title"]').type('test title');
  49. cy.get('input[name="image"]').selectFile('cypress/fixtures/file.jpg');
  50. cy.get('textarea[name="content"]').type('test content');
  51. cy.get('button[type="submit"]').click();
  52. });
  53. cy.wait('@action').its('response.statusCode').should('eq', 307);
  54. cy.wait('@destination').its('response.statusCode').should('eq', 201);
  55. cy.url().should('match', /\/notes\/[a-z0-9-]+\?/);
  56. });
  57. it('submits form with incomplete data', () => {
  58. cy.visit('http://localhost:3000/notes?noscript=true');
  59. cy.intercept('POST', '/a/notes?noscript=true').as('action');
  60. cy.intercept('POST', '/notes/*').as('destination');
  61. cy.get('form').should('exist').within(() => {
  62. cy.get('input[name="title"]').type('test title');
  63. cy.get('textarea[name="content"]').type('test content');
  64. cy.get('button[type="submit"]').click();
  65. });
  66. cy.wait('@action').its('response.statusCode').should('eq', 307);
  67. cy.wait('@destination').its('response.statusCode').should('eq', 201);
  68. cy.url().should('match', /\/notes\/[a-z0-9-]+\?/);
  69. });
  70. it('handles redirects', () => {
  71. cy.visit('http://localhost:3000/notes?noscript=true');
  72. cy.get('form').should('exist').within(() => {
  73. cy.get('input[name="title"]').type('test title');
  74. cy.get('textarea[name="content"]').type('test content');
  75. cy.get('button[type="submit"]').click();
  76. });
  77. cy.intercept('POST', '/a/notes/*').as('action');
  78. cy.intercept('POST', '/notes').as('destination');
  79. cy.get('button').contains('Delete').click();
  80. cy.wait('@action').its('response.statusCode').should('eq', 307);
  81. cy.wait('@destination').its('response.statusCode').should('eq', 200);
  82. cy.url().should('match', /\/notes$/);
  83. });
  84. });
  85. });