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.
 
 
 

98 lines
3.7 KiB

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