From c63e2dabb6928fd59b5df45f3f1399a0c4aa7a8e Mon Sep 17 00:00:00 2001 From: Sander Hautvast Date: Tue, 2 Mar 2021 18:04:49 +0100 Subject: [PATCH] added tests --- cypress/fixtures/example.json | 5 +++++ cypress/integration/console.spec.js | 29 +++++++++++++++++++++++++++++ cypress/support/index.js | 1 + package-lock.json | 6 ++++++ package.json | 1 + src/js/index.js | 13 +++++++------ 6 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 cypress/fixtures/example.json diff --git a/cypress/fixtures/example.json b/cypress/fixtures/example.json new file mode 100644 index 0000000..da18d93 --- /dev/null +++ b/cypress/fixtures/example.json @@ -0,0 +1,5 @@ +{ + "name": "Using fixtures to represent data", + "email": "hello@cypress.io", + "body": "Fixtures are a great way to mock data for responses to routes" +} \ No newline at end of file diff --git a/cypress/integration/console.spec.js b/cypress/integration/console.spec.js index 3fa6b3a..c74298f 100644 --- a/cypress/integration/console.spec.js +++ b/cypress/integration/console.spec.js @@ -14,3 +14,32 @@ describe('draw a vector', () => { cy.get('#2').invoke('attr','d').should('eq','M550 350 L500 200'); }) }) + +describe('draw a lazy vector', () => { + it('adds the svg vector', () => { + cy.visit('http://localhost:8080'); + + cy.get('#command_input').type("a = vector(0,0,0.5,0.5){enter}"); + cy.get('#0').invoke('attr','d').should('eq','M550 350 L600 300'); + cy.get('#0').invoke('attr','class').should('eq','vector'); + cy.get('#0').invoke('attr','marker-end').should('eq','url(#arrow)'); + + cy.get('#command_input').type("b = vector(0,0,-1,1){enter}"); + cy.get('#1').invoke('attr','d').should('eq','M550 350 L450 250'); + + cy.get('#command_input').type("c = \"a + b\"{enter}"); // same as above, but lazy + cy.get('#3').invoke('attr','d').should('eq','M550 350 L500 200'); + + cy.get('#command_input').type("a = a*2{enter}"); // so what happens to lazy, when it's constituents are altered? + // a is now double in size, so c must also changed + // c now refers to @4 + cy.get('#4').invoke('attr','d').should('eq','M550 350 L650 250'); + + cy.get('#command_input').type("a = 1{enter}"); // so what happens to lazy, when it's constituents are altered? + + cy.get("#l4").then(text => { + expect(text.text()).to.equal('@4'); // binding has been changed (to 1, ie not a vector), + // so this is now a 'free' vector with a reference label + }); + }) +}) \ No newline at end of file diff --git a/cypress/support/index.js b/cypress/support/index.js index eab9d66..891b0c4 100644 --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -1,2 +1,3 @@ // import './commands' +require('cypress-xpath'); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 2a86706..5b20c77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1643,6 +1643,12 @@ } } }, + "cypress-xpath": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/cypress-xpath/-/cypress-xpath-1.6.2.tgz", + "integrity": "sha512-mtwJPl840GQPGtb480fKR5vDIcijBHhAVwby5/AIPIT/UVT7UJhM2L42/R+venR7N01I0PoOJErb6UiMbCyUxg==", + "dev": true + }, "dashdash": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", diff --git a/package.json b/package.json index a72e8b3..943b8f1 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "devDependencies": { "css-loader": "^5.0.2", "cypress": "^6.5.0", + "cypress-xpath": "^1.6.2", "eslint": "^7.20.0", "eslint-loader": "^4.0.2", "eslint-plugin-cypress": "^2.11.2", diff --git a/src/js/index.js b/src/js/index.js index 0abb54f..72a3cc4 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -37,17 +37,18 @@ const show = function (vector) { } export const update_lazy_objects = function () { - let b = Object.values(bindings); - for (let i = 0; i < b.length; i++) { + let b = Object.values(bindings); // a lazy expression must be bound + for (let i = 0; i < b.length; i++) { // unbound ones can exist, but are meaningless, because you cannot refer to them let binding = b[i]; - if (state[binding.name].lazy_expression) { - let value = visit(state[binding.name].lazy_expression); - let existing_value = bindings[binding.name].evaluated; + if (state[binding.name].lazy_expression) { // if lazy, + let value = visit(state[binding.name].lazy_expression); // reevaluate, + let existing_value = bindings[binding.name].evaluated; // update view if (existing_value.id) { + // update view after reevaluation of lazy vectors update_vector_arrow(existing_value.id, value); bindings[binding.name].evaluated = value; } else if (value.is_new && value.is_vector){ - // hidden lazy object reappearing + // hidden lazy vector reappears value.label_text=binding.name; add_vector_arrow_to_svg(value); }