Skip to content

Commit e7e1e8b

Browse files
committed
Setup E2E tests on Cypress
1 parent 1311b26 commit e7e1e8b

File tree

8 files changed

+161
-1
lines changed

8 files changed

+161
-1
lines changed

cypress.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"baseUrl": "http://localhost:5000"
3+
}

cypress/fixtures/example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "[email protected]",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}

cypress/integration/test.spec.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* eslint-env mocha */
2+
/* global cy, expect */
3+
4+
describe('Integration Tests', () => {
5+
before(() => cy.visit('/'))
6+
7+
afterEach(() => cy.wait(500))
8+
9+
it('Displays a toast', () => {
10+
cy.get('[data-btn=default]')
11+
.click()
12+
.get('._toastBtn')
13+
.click()
14+
})
15+
16+
it('Displays colored toast', () => {
17+
cy.get('[data-btn=green]')
18+
.click()
19+
.get('._toastItem')
20+
.should('have.css', 'background-color', 'rgb(72, 187, 120)')
21+
.get('._toastBtn')
22+
.click()
23+
})
24+
25+
it('Displays rich HTML', () => {
26+
cy.get('[data-btn=richHtml]')
27+
.click()
28+
.get('._toastItem')
29+
.find('a')
30+
.get('._toastBtn')
31+
.click()
32+
})
33+
34+
it('Can change duration', () => {
35+
cy.window()
36+
.invoke('toast.push', 'Test', { duration: 1000 })
37+
.get('._toastItem')
38+
.wait(1500)
39+
.should('not.exist')
40+
})
41+
42+
it('Can be non-dismissable then popped', () => {
43+
cy.get('[data-btn=nonDismissable]')
44+
.click()
45+
.get('._toastBtn')
46+
.should('not.exist')
47+
.get('[data-btn=removeLastToast]')
48+
.click()
49+
.wait(500)
50+
.get('._toastItem')
51+
.should('not.exist')
52+
})
53+
54+
it('Flips progress bar', () => {
55+
cy.get('[data-btn=flipProgressBar]')
56+
.click()
57+
.get('._toastBar')
58+
.then($bar => {
59+
const old = parseFloat($bar.val())
60+
cy.wait(500).then(() => {
61+
expect(parseFloat($bar.val())).to.be.above(old)
62+
cy.get('._toastBtn').click()
63+
})
64+
})
65+
})
66+
67+
it('Dynamically update progress bar', () => {
68+
cy.window()
69+
.invoke('toast.push', 'Test', { duration: 1, initial: 0, progress: 0 })
70+
.then(id => {
71+
cy.get('._toastBar').then($bar => {
72+
expect($bar.val()).to.equal(0)
73+
cy.window().invoke('toast.set', id, { progress: 0.2 }).wait(50).then(() => {
74+
expect(parseFloat($bar.val())).to.equal(0.2)
75+
cy.get(',_toastBtn').click()
76+
})
77+
})
78+
})
79+
})
80+
})

cypress/plugins/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference types="cypress" />
2+
// ***********************************************************
3+
// This example plugins/index.js can be used to load plugins
4+
//
5+
// You can change the location of this file or turn off loading
6+
// the plugins file with the 'pluginsFile' configuration option.
7+
//
8+
// You can read more here:
9+
// https://on.cypress.io/plugins-guide
10+
// ***********************************************************
11+
12+
// This function is called when a project is opened or re-opened (e.g. due to
13+
// the project's config changing)
14+
15+
/**
16+
* @type {Cypress.PluginConfig}
17+
*/
18+
// eslint-disable-next-line no-unused-vars
19+
module.exports = (on, config) => {
20+
// `on` is used to hook into various events Cypress emits
21+
// `config` is the resolved Cypress config
22+
}

cypress/support/commands.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ***********************************************
2+
// This example commands.js shows you how to
3+
// create various custom commands and overwrite
4+
// existing commands.
5+
//
6+
// For more comprehensive examples of custom
7+
// commands please read more here:
8+
// https://on.cypress.io/custom-commands
9+
// ***********************************************
10+
//
11+
//
12+
// -- This is a parent command --
13+
// Cypress.Commands.add('login', (email, password) => { ... })
14+
//
15+
//
16+
// -- This is a child command --
17+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
18+
//
19+
//
20+
// -- This is a dual command --
21+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
22+
//
23+
//
24+
// -- This will overwrite an existing command --
25+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })

cypress/support/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ***********************************************************
2+
// This example support/index.js is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands'
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

docs/App.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { tick } from 'svelte'
33
import { SvelteToast, toast } from '../src'
44
import Prism from './Prism.svelte'
5+
import camelCase from 'camelcase'
56
67
// Hoist to `window` for debug
78
window.toast = toast
@@ -216,6 +217,7 @@ toast.set(id, { progress: 1 })`,
216217
<button
217218
class:selected={selected === btn.name}
218219
on:click={() => { handleClick(btn) }}
220+
data-btn={camelCase(btn.name)}
219221
>{btn.name}</button>
220222
{/each}
221223

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
"build:dist": "rollup -c rollup.dist.config.js",
2525
"build:tailwind": "NODE_ENV=production tailwindcss -i docs/tailwind.css -c docs/tailwind.config.js -o docs/build/global.css -m",
2626
"build": "run-s lint build:*",
27+
"test": "cypress run",
2728
"prepublishOnly": "npm run build"
2829
},
2930
"devDependencies": {
3031
"@rollup/plugin-commonjs": "^19.0.0",
3132
"@rollup/plugin-node-resolve": "^13.0.0",
32-
"@typescript-eslint/eslint-plugin": "^4.28.1",
33+
"@typescript-eslint/eslint-plugin": "^4.28.2",
3334
"autoprefixer": "^10.2.6",
35+
"camelcase": "^6.2.0",
36+
"cypress": "^7.6.0",
3437
"eslint": "^7.30.0",
3538
"eslint-config-standard-with-typescript": "^20.0.0",
3639
"eslint-plugin-html": "^6.1.2",

0 commit comments

Comments
 (0)