forms.cy.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const apiUrl = Cypress.env('apiUrl');
  2. describe('Forms', () => {
  3. it('Opens forms page', () => {
  4. cy.resetDB();
  5. cy.loginAndVisit('/lists/forms');
  6. });
  7. it('Checks form URL', () => {
  8. cy.get('a[data-cy=url]').contains('http://localhost:9000');
  9. });
  10. it('Checks public lists', () => {
  11. cy.get('ul[data-cy=lists] li')
  12. .should('contain', 'Opt-in list')
  13. .its('length')
  14. .should('eq', 1);
  15. cy.get('[data-cy=form] pre').should('not.exist');
  16. });
  17. it('Selects public list', () => {
  18. // Click the list checkbox.
  19. cy.get('ul[data-cy=lists] .checkbox').click();
  20. // Make sure the <pre> form HTML has appeared.
  21. cy.get('[data-cy=form] pre').then(($pre) => {
  22. // Check that the ID of the list in the checkbox appears in the HTML.
  23. cy.get('ul[data-cy=lists] input').then(($inp) => {
  24. cy.wrap($pre).contains($inp.val());
  25. });
  26. });
  27. // Click the list checkbox.
  28. cy.get('ul[data-cy=lists] .checkbox').click();
  29. cy.get('[data-cy=form] pre').should('not.exist');
  30. });
  31. it('Subscribes from public form page', () => {
  32. // Create a public test list.
  33. cy.request('POST', `${apiUrl}/api/lists`, { name: 'test-list', type: 'public', optin: 'single' });
  34. // Open the public page and subscribe to alternating lists multiple times.
  35. // There should be no errors and two new subscribers should be subscribed to two lists.
  36. for (let i = 0; i < 2; i++) {
  37. for (let j = 0; j < 2; j++) {
  38. cy.loginAndVisit(`${apiUrl}/subscription/form`);
  39. cy.get('input[name=email]').clear().type(`test${i}@test.com`);
  40. cy.get('input[name=name]').clear().type(`test${i}`);
  41. cy.get('input[type=checkbox]').eq(j).click();
  42. cy.get('button').click();
  43. cy.wait(250);
  44. cy.get('.wrap').contains(/has been sent|successfully/);
  45. }
  46. }
  47. // Verify form subscriptions.
  48. cy.request(`${apiUrl}/api/subscribers`).should((response) => {
  49. const { data } = response.body;
  50. // Two new + two dummy subscribers that are there by default.
  51. expect(data.total).to.equal(4);
  52. // The two new subscribers should each have two list subscriptions.
  53. for (let i = 0; i < 2; i++) {
  54. expect(data.results.find((s) => s.email === `test${i}@test.com`).lists.length).to.equal(2);
  55. }
  56. });
  57. });
  58. it('Unsubscribes', () => {
  59. // Add all lists to the dummy campaign.
  60. cy.request('PUT', `${apiUrl}/api/campaigns/1`, { 'lists': [2] });
  61. cy.request('GET', `${apiUrl}/api/subscribers`).then((response) => {
  62. let subUUID = response.body.data.results[0].uuid;
  63. cy.request('GET', `${apiUrl}/api/campaigns`).then((response) => {
  64. let campUUID = response.body.data.results[0].uuid;
  65. cy.loginAndVisit(`${apiUrl}/subscription/${campUUID}/${subUUID}`);
  66. });
  67. });
  68. cy.wait(500);
  69. // Unsubscribe from one list.
  70. cy.get('button').click();
  71. cy.request('GET', `${apiUrl}/api/subscribers`).then((response) => {
  72. const { data } = response.body;
  73. expect(data.results[0].lists.find((s) => s.id === 2).subscription_status).to.equal('unsubscribed');
  74. expect(data.results[0].lists.find((s) => s.id === 3).subscription_status).to.equal('unconfirmed');
  75. });
  76. // Go back.
  77. cy.url().then((u) => {
  78. cy.loginAndVisit(u);
  79. });
  80. // Unsubscribe from all.
  81. cy.get('#privacy-blocklist').click();
  82. cy.get('button').click();
  83. cy.request('GET', `${apiUrl}/api/subscribers`).then((response) => {
  84. const { data } = response.body;
  85. expect(data.results[0].status).to.equal('blocklisted');
  86. expect(data.results[0].lists.find((s) => s.id === 2).subscription_status).to.equal('unsubscribed');
  87. expect(data.results[0].lists.find((s) => s.id === 3).subscription_status).to.equal('unsubscribed');
  88. });
  89. });
  90. it('Manages subscription preferences', () => {
  91. cy.request('GET', `${apiUrl}/api/subscribers`).then((response) => {
  92. let subUUID = response.body.data.results[1].uuid;
  93. cy.request('GET', `${apiUrl}/api/campaigns`).then((response) => {
  94. let campUUID = response.body.data.results[0].uuid;
  95. cy.loginAndVisit(`${apiUrl}/subscription/${campUUID}/${subUUID}?manage=1`);
  96. });
  97. });
  98. // Change name and unsubscribe from one list.
  99. cy.get('input[name=name]').clear().type('new-name');
  100. cy.get('ul.lists input:first').click();
  101. cy.get('button:first').click();
  102. cy.request('GET', `${apiUrl}/api/subscribers`).then((response) => {
  103. const { data } = response.body;
  104. expect(data.results[1].name).to.equal('new-name');
  105. expect(data.results[1].lists.find((s) => s.id === 2).subscription_status).to.equal('unsubscribed');
  106. expect(data.results[1].lists.find((s) => s.id === 3).subscription_status).to.equal('unconfirmed');
  107. });
  108. });
  109. });