Skip to content

Commit

Permalink
Add cypress integration (#38)
Browse files Browse the repository at this point in the history
* Add hasChanged and hasBlurred props to useField

* Add cypress testing

* Add circleci cache for docs

* Add node cache alias (test)

* Update aliases

* move things around

* bug-reproduction

* Add tests for examples

* Add dependency to deploy ci step

* Fix prettier anomoly

* Remove vars
  • Loading branch information
andyrichardson committed Feb 2, 2020
1 parent 9524312 commit 6c12d5b
Show file tree
Hide file tree
Showing 13 changed files with 410 additions and 55 deletions.
133 changes: 106 additions & 27 deletions .circleci/config.yml
@@ -1,43 +1,42 @@
variables:
- &module-cache node-cache-{{ checksum "package-lock.json" }}
- &build-cache build-cache-{{ .Revision }}
- &package-cache package-cache-{{ .Revision }}
- &docs-module-cache docs-module-cache-{{ checksum "docs/yarn.lock" }}

jobs:
install:
docker:
- image: node:13
steps:
- checkout
- restore_cache:
keys:
- *module-cache
- run: npm ci
- save_cache:
key: node-cache-{{ .Revision }}
key: *module-cache
paths:
- 'node_modules'

build:
prettier:
docker:
- image: node:13
steps:
- checkout
- restore_cache:
keys:
- node-cache-{{ .Revision }}
- run: npm run build
- save_cache:
key: build-cache-{{ .Revision }}
paths:
- 'dist'
- *module-cache
- run: npm run check-formatting

build docs:
lint:
docker:
- image: node:13
steps:
- checkout
- run:
command: yarn
working_directory: docs
- run:
command: yarn run build
working_directory: docs
- store_artifacts:
path: docs/.docz/dist
destination: static-docs
- restore_cache:
keys:
- *module-cache
- run: npm run lint

test:
docker:
Expand All @@ -46,29 +45,95 @@ jobs:
- checkout
- restore_cache:
keys:
- node-cache-{{ .Revision }}
- *module-cache
- run: npm run test -- --coverage
- run: npx codecov

prettier:
build:
docker:
- image: node:13
steps:
- checkout
- restore_cache:
keys:
- node-cache-{{ .Revision }}
- run: npm run check-formatting
- *module-cache
- run: npm run build
- save_cache:
key: *build-cache
paths:
- 'dist'
- run: npm pack
- run: mv fielder*.tgz fielder.tgz
- save_cache:
key: *package-cache
paths:
- 'fielder.tgz'
- store_artifacts:
path: fielder.tgz

lint:
e2e (example 1):
docker:
- image: cypress/browsers:chrome67
steps:
- checkout
- restore_cache:
keys:
- *package-cache
- run:
command: npm remove fielder
working_directory: examples/1-basics
- run:
command: npm install
working_directory: examples/1-basics
- run:
command: npm install ../../fielder.tgz
working_directory: examples/1-basics
- run:
command: npm run serve & sleep 10 && npm run test
working_directory: examples/1-basics

e2e (example 2):
docker:
- image: cypress/browsers:chrome67
steps:
- checkout
- restore_cache:
keys:
- *package-cache
- run:
command: npm remove fielder
working_directory: examples/2-multi-step
- run:
command: npm install
working_directory: examples/2-multi-step
- run:
command: npm install ../../fielder.tgz
working_directory: examples/2-multi-step
- run:
command: npm run serve & sleep 10 && npm run test
working_directory: examples/2-multi-step

build docs:
docker:
- image: node:13
steps:
- checkout
- restore_cache:
keys:
- node-cache-{{ .Revision }}
- run: npm run lint
- *docs-module-cache
- run:
command: yarn
working_directory: docs
- save_cache:
key: *docs-module-cache
paths:
- 'docs/node_modules'
- run:
command: yarn run build
working_directory: docs
- store_artifacts:
path: docs/.docz/dist
destination: static-docs

deploy:
docker:
Expand All @@ -77,7 +142,7 @@ jobs:
- checkout
- restore_cache:
keys:
- build-cache-{{ .Revision }}
- *build-cache
- run:
command: |
node -e "process.env.CIRCLE_TAG === \`v\${require('./package.json').version}\` ? process.exit(0) : (console.error('Tag version does not line up with version in package.json') || process.exit(1))"
Expand All @@ -103,6 +168,18 @@ workflows:
filters:
tags:
only: /.*/
- e2e (example 1):
requires:
- build
filters:
tags:
only: /.*/
- e2e (example 2):
requires:
- build
filters:
tags:
only: /.*/
- prettier:
requires:
- install
Expand All @@ -129,6 +206,8 @@ workflows:
- build
- prettier
- lint
- e2e (example 1)
- e2e (example 2)
filters:
tags:
only: /.*/
Expand Down
6 changes: 6 additions & 0 deletions examples/1-basics/cypress.json
@@ -0,0 +1,6 @@
{
"baseUrl": "http://localhost:1234",
"fixturesFolder": false,
"pluginsFile": false,
"supportFile": false
}
77 changes: 77 additions & 0 deletions examples/1-basics/cypress/integration/index.ts
@@ -0,0 +1,77 @@
beforeEach(() => {
cy.visit('/');
});

describe('username', () => {
it('changes value', () => {
const value = 'hi';
cy.get('input[name="username"]')
.type(value)
.should('have.value', value);
});

it('validates on blur', () => {
cy.get('input[name="username"]')
.type('hi')
.blur()
.next()
.should('contain.text', 'Username must be at least 4 characters.');
});

it('updates validation on change', () => {
cy.get('input[name="username"]')
.type('hi')
.blur()
.type('hello there')
.next()
.should('not.contain.text', 'Username must be at least 4 characters.');
});
});

describe('password', () => {
it('changes value', () => {
const value = 'hi';

cy.get('input[name="password"]')
.type(value)
.should('have.value', value);
});

it('validates on blur', () => {
cy.get('input[name="password"]')
.type('hi')
.blur()
.next()
.should('contain.text', 'Password must be at least 4 characters.');
});

it('updates validation on change', () => {
cy.get('input[name="password"]')
.type('hi')
.blur()
.type('hello there')
.next()
.should('not.contain.text', 'Password must be at least 4 characters.');
});
});

describe('next button', () => {
it('is disabled on mount', () => {
cy.get('button').should('have.attr', 'disabled');
});

it('is enabled when fields are valid', () => {
cy.get('input[name="username"]').type('hello there');
cy.get('input[name="password"]').type('hello there');
cy.get('button').should('not.have.attr', 'disabled');
});

it('is disabled when fields are invalid', () => {
cy.get('input[name="username"]').type('hello there');
cy.get('input[name="password"]').type('hello there');
cy.get('input[name="password"]')
.clear()
.type('hi');
cy.get('button').should('have.attr', 'disabled');
});
});
10 changes: 10 additions & 0 deletions examples/1-basics/cypress/tsconfig.json
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"strict": true,
"baseUrl": "../node_modules",
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress"]
},
"include": ["**/*.ts"]
}
5 changes: 4 additions & 1 deletion examples/1-basics/package.json
Expand Up @@ -5,7 +5,9 @@
"main": "index.html",
"scripts": {
"start": "parcel index.html --open",
"dev": "sed -i -e 's|\"react\".*|\"react\": \"file:../../node_modules/react\",|; s|\"react-dom\".*|\"react-dom\": \"file:../../node_modules/react-dom\",|; s|\"fielder\".*|\"fielder\": \"file:../../\",|' package.json && npm run start",
"serve": "parcel serve index.html",
"setup-dev": "sed -i -e 's|\"react\".*|\"react\": \"file:../../node_modules/react\",|; s|\"react-dom\".*|\"react-dom\": \"file:../../node_modules/react-dom\",|; s|\"fielder\".*|\"fielder\": \"file:../../\",|' package.json",
"test": "cypress run",
"build": "parcel build index.html"
},
"dependencies": {
Expand All @@ -16,6 +18,7 @@
"semantic-ui-react": "0.88.2"
},
"devDependencies": {
"cypress": "^3.8.3",
"parcel-bundler": "^1.6.1",
"typescript": "^3.7.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/1-basics/src/form/FormContent.tsx
Expand Up @@ -18,7 +18,7 @@ export const FormContent: FC = () => {
}, []);

return (
<form autocomplete="off">
<form autoComplete="off">
<div className="field">
<label>Username</label>
<input type="text" placeholder="Username" {...usernameProps} />
Expand Down
6 changes: 6 additions & 0 deletions examples/2-multi-step/cypress.json
@@ -0,0 +1,6 @@
{
"baseUrl": "http://localhost:1234",
"fixturesFolder": false,
"pluginsFile": false,
"supportFile": false
}

0 comments on commit 6c12d5b

Please sign in to comment.