Skip to content

Commit

Permalink
test(e2e): add Cypress (#187)
Browse files Browse the repository at this point in the history
* test(e2e): add Cypress

This includes three very simple tests (click the canvas, place a node,
place two nodes and connect them with an edge). These should ensure that
the library is not completely broken though they test only a small part
of the library.

* ci: run e2e tests in CI
  • Loading branch information
Thomaash authored Oct 29, 2019
1 parent a97082e commit 0c9246e
Show file tree
Hide file tree
Showing 17 changed files with 2,033 additions and 110 deletions.
66 changes: 46 additions & 20 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,36 @@ jobs:
key: v1-dependencies-{{ checksum "package.json" }}

- persist_to_workspace:
root: .
root: ..
paths:
- "*"
- "repo"
- ".cache/Cypress"

build:
executor: node

steps:
- attach_workspace:
at: .
at: ..

- run: npm run build

- persist_to_workspace:
root: .
root: ..
paths:
- "dist"
- "repo/dist"

examples:
executor: node

steps:
- attach_workspace:
at: .
at: ..

- run:
name: Install dependencies for headless Chromium
command: |
sudo apt-get update
sudo apt-get install -yq \
ca-certificates fonts-liberation gconf-service libappindicator1 \
libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 \
Expand All @@ -77,16 +79,16 @@ jobs:
- run: npm run generate-examples-index

- persist_to_workspace:
root: .
root: ..
paths:
- "examples"
- "repo/examples"

lint_js:
executor: node

steps:
- attach_workspace:
at: .
at: ..

- run: npm run lint:js

Expand All @@ -95,25 +97,41 @@ jobs:

steps:
- attach_workspace:
at: .
at: ..

- run: npm run lint:ts

test:
test_unit:
executor: node

steps:
- attach_workspace:
at: .
at: ..

- run: npm run test-cov
- run: npm run test:unit

test_e2e:
executor: node

steps:
- attach_workspace:
at: ..

- run:
name: Install dependencies for Cypress
command: |
sudo apt-get update
sudo apt-get install -yq \
xvfb libgtk-3-dev libnotify-dev libgconf-2-4 libnss3 libxss1 \
libasound2
- run: npm run test:e2e

gh_pages:
executor: node

steps:
- attach_workspace:
at: .
at: ..

- run:
name: Set Git credentials
Expand All @@ -131,7 +149,7 @@ jobs:

steps:
- attach_workspace:
at: .
at: ..

- run:
name: Prepare NPM
Expand Down Expand Up @@ -167,30 +185,38 @@ workflows:
requires:
- prepare

- test:
- test_unit:
requires:
- prepare

- gh_pages:
- test_e2e:
requires:
- prepare
- build

- gh_pages:
requires:
- build
- examples
# - lint_js
- lint_ts
- test
- prepare
- test_e2e
- test_unit
filters:
branches:
only:
- master

- release:
requires:
- prepare
- build
- gh_pages
# - lint_js
- lint_ts
- test
- prepare
- test_e2e
- test_unit
filters:
branches:
only:
Expand Down
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
env: {
"cypress/globals": true,
browser: true,
es6: true,
node: true,
Expand All @@ -13,7 +14,7 @@ module.exports = {
project: 'tsconfig.json',
},

plugins: ['prettier', '@typescript-eslint'],
plugins: ["prettier", "@typescript-eslint", "cypress"],

extends: ['eslint:recommended', 'prettier'],

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ coverage/
.rpt2_cache

# built files
/cypress/screenshots/
/cypress/support/**/*.js.map
/cypress/videos/
/declarations/
/dist/
/examples/index.html
Expand Down
1 change: 1 addition & 0 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
18 changes: 18 additions & 0 deletions cypress/integration/manipulation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
context("Manipulation GUI", () => {
beforeEach(() => {
cy.visit("http://localhost:58253/cypress/pages/simple.html");
});

it("Add a node", () => {
cy.visPlaceNode(101, 102);
});

it("Add 2 nodes and 1 edge", () => {
const a = { x: 101, y: 102 };
const b = { x: 201, y: 202 };

cy.visPlaceNode(a.x, a.y);
cy.visPlaceNode(b.x, b.y);
cy.visConnectNodes(a, b);
});
});
15 changes: 15 additions & 0 deletions cypress/integration/simple.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
context("Simple", () => {
beforeEach(() => {
cy.visit("http://localhost:58253/cypress/pages/simple.html");
});

it("Click the canvas", () => {
const x = 101;
const y = 102;

cy.get("#mynetwork").click(x, y);

cy.get("#events .click .pointer.DOM.x").should("have.text", "" + x);
cy.get("#events .click .pointer.DOM.y").should("have.text", "" + y);
});
});
119 changes: 119 additions & 0 deletions cypress/pages/simple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<!DOCTYPE html>
<html>
<head>
<title>Vis Network | Cypress | Simple</title>

<script
type="text/javascript"
src="../../standalone/umd/vis-network.min.js"
></script>

<style type="text/css">
html,
body,
#mynetwork {
margin: 0px;
padding: 0px;
}

#mynetwork {
position: fixed;
left: 0px;
top: 0px;
bottom: 0px;
right: 50%;
min-height: 100vh;
border-right: 1px solid lightgray;
background: white;
}

#text {
position: absolute;
left: 50%;
padding: 1em;
}

#title {
margin-bottom: 5em;
}
</style>

<script
type="text/javascript"
src="../../../../dist/vis-network.min.js"
></script>
<link
href="../../../../dist/vis-network.min.css"
rel="stylesheet"
type="text/css"
/>
</head>

<body>
<div id="text">
<div id="title">
<h1>Vis Network</h1>
<h2>Cypress</h2>
<h3>Simple</h3>
</div>

<div id="events"></div>
</div>

<div id="mynetwork"></div>
<script type="text/javascript">
/*
* create a network
*/
const container = document.getElementById("mynetwork");
const data = { nodes: [], edges: [] };
const options = {
manipulation: true,
physics: false
};
const network = new vis.Network(container, data, options);

/*
* Events
*/
function add(root, names, content) {
names = Array.isArray(names) ? names : [names];

const elem = document.createElement("pre");
elem.classList.add(...names);
elem.innerText = content;

root.appendChild(elem);
}

const root = document.getElementById("events");
["click"].forEach(eventName => {
network.on(eventName, function(params) {
console.log({ eventName, params });

const oldElem = root.querySelector(`#events > .${eventName}`);
if (oldElem) {
oldElem.parentNode.removeChild(oldElem);
}

const el = document.createElement("div");
el.classList.add(eventName);

add(el, ["pointer", "DOM", "x"], params.pointer.DOM.x);
add(el, ["pointer", "DOM", "y"], params.pointer.DOM.y);
add(el, ["pointer", "canvas", "x"], params.pointer.canvas.x);
add(el, ["pointer", "canvas", "y"], params.pointer.canvas.y);

params.edges.forEach(edgeId => {
add(el, ["edge", edgeId], edgeId);
});
params.nodes.forEach(nodeId => {
add(el, ["node", nodeId], nodeId);
});

root.appendChild(el);
});
});
</script>
</body>
</html>
44 changes: 44 additions & 0 deletions cypress/support/commands.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0c9246e

Please sign in to comment.