From c5edf6d9616dc4fc1a0ceeb8860382d7fcdbf3f8 Mon Sep 17 00:00:00 2001
From: rhenck <richard.henck@iqb.hu-berlin.de>
Date: Mon, 26 Feb 2024 18:19:07 +0100
Subject: [PATCH] [e2e] Improve buttons tests

---
 e2e/tests/e2e/button.spec.cy.ts | 20 ++++++--------------
 e2e/tests/helper-functions.ts   | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/e2e/tests/e2e/button.spec.cy.ts b/e2e/tests/e2e/button.spec.cy.ts
index 8d1858356..4d1d2cc6e 100644
--- a/e2e/tests/e2e/button.spec.cy.ts
+++ b/e2e/tests/e2e/button.spec.cy.ts
@@ -1,8 +1,8 @@
 import {
-  addPage, navigateToPage, selectFromDropdown
+  addPage, addPostMessageStub, navigateToPage, selectFromDropdown
 } from '../helper-functions';
 
-describe('Basic Unit', { testIsolation: false }, () => {
+describe('Button element', { testIsolation: false }, () => {
   it('creates basic buttons in editor', () => {
     cy.openEditor();
     cy.contains('Sonstige').click();
@@ -64,12 +64,13 @@ describe('Basic Unit', { testIsolation: false }, () => {
     cy.contains('button', 'Knopf-next-unit').click();
     cy.get('@postMessage')
       .should('be.calledWithMatch',
-        Cypress.sinon.match(message => message.type === 'vopUnitNavigationRequestedNotification' &&
-          message.target === 'next'));
+        Cypress.sinon.match({
+          type: 'vopUnitNavigationRequestedNotification',
+          target: 'next'
+        }));
   });
 
   it('finds and uses a button with page nav', () => {
-    addPostMessageStub();
     cy.contains('button', 'Knopf-page-2').click();
     cy.contains('Knopf-seite-2');
   });
@@ -78,13 +79,4 @@ describe('Basic Unit', { testIsolation: false }, () => {
     cy.get('input[type="image"').should('have.attr', 'alt', 'Bild nicht gefunden');
     cy.get('[src^="data:image"]');
   });
-
-  function addPostMessageStub() {
-    const postMessageStub = cy.stub().as('postMessage');
-    cy.window().then(window => {
-      window.parent.addEventListener('message', e => {
-        postMessageStub(e.data);
-      });
-    });
-  }
 });
diff --git a/e2e/tests/helper-functions.ts b/e2e/tests/helper-functions.ts
index 6e706ae4c..09c40bace 100644
--- a/e2e/tests/helper-functions.ts
+++ b/e2e/tests/helper-functions.ts
@@ -20,3 +20,36 @@ export function addButton() {
   });
   cy.contains('Knopf').click();
 }
+
+export function addPostMessageStub() {
+  const postMessageStub = cy.stub().as('postMessage');
+  cy.window().then(window => {
+    window.parent.addEventListener('message', e => {
+      postMessageStub(e.data);
+    });
+  });
+}
+
+export function assertValueChanged(id: string, value: any): void {
+  cy.get('@postMessage')
+    .should('be.calledWithMatch',
+      Cypress.sinon.match.has('unitState',
+        Cypress.sinon.match.has(
+          'dataParts', Cypress.sinon.match.has('elementCodes',
+            Cypress.sinon.match(
+              `{"id":"${id}","value":${value},"status":"VALUE_CHANGED"}`))
+        )));
+
+  // alternative without 'has'
+  // -------------------------
+  // cy.get('@postMessage')
+  //   .should('be.calledWithMatch',
+  //     Cypress.sinon.match({
+  //       type: 'vopStateChangedNotification',
+  //       unitState: Cypress.sinon.match({
+  //         dataParts: Cypress.sinon.match({
+  //           elementCodes: Cypress.sinon.match('{"id":"dropdown_2","value":2,"status":"VALUE_CHANGED"}')
+  //         })
+  //       })
+  //     }));
+}
-- 
GitLab