Skip to content

Commit 70c7c12

Browse files
test: add cypress end-to-end test setup (#313)
Co-authored-by: Jacob M-G Evans <27247160+JacobMGEvans@users.noreply.github.com>
1 parent 6a8b5ca commit 70c7c12

13 files changed

+1172
-52
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
'plugin:prettier/recommended',
66
'plugin:react/recommended',
77
'plugin:react-hooks/recommended',
8+
'plugin:cypress/recommended',
89
],
910
parserOptions: {
1011
ecmaVersion: 2020,

cypress.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"baseUrl": "http://localhost:1234",
3+
"integrationFolder": "cypress/e2e"
4+
}

cypress/e2e/initApp.spec.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
it('renders Testing Playground with an example', () => {
2+
cy.visit('/');
3+
4+
cy.findByRole('heading', {
5+
level: 1,
6+
name: 'Testing Playground mascot Froggy ️ Testing Playground',
7+
}).should('exist');
8+
9+
cy.findByRole('link', {
10+
name: 'Testing Playground mascot Froggy ️ Testing Playground',
11+
})
12+
.should('exist')
13+
.and('have.attr', 'href', '/');
14+
15+
cy.findByRole('button', { name: 'playground' }).should('exist');
16+
cy.findByRole('button', { name: 'run' }).should('exist');
17+
cy.findByRole('button', { name: 'settings' }).should('exist');
18+
cy.findByRole('button', { name: 'kebab menu' }).should('exist');
19+
20+
cy.getMarkupEditor().should('exist');
21+
22+
cy.getSandboxBody().within(() => {
23+
cy.findByLabelText('Email address')
24+
.should('exist')
25+
.and('have.attr', 'type', 'email')
26+
.and('have.attr', 'placeholder', 'Enter email');
27+
28+
cy.findByText("It's safe with us. We hate spam!").should('exist');
29+
30+
cy.findByLabelText('Password')
31+
.should('exist')
32+
.and('have.attr', 'type', 'password')
33+
.and('have.attr', 'placeholder', 'Password');
34+
35+
cy.findByLabelText('I accept the terms and conditions')
36+
.should('exist')
37+
.and('have.attr', 'type', 'checkbox')
38+
.and('not.be.checked');
39+
40+
cy.findByRole('link', { name: 'terms and conditions' })
41+
.should('exist')
42+
.and('have.attr', 'href', 'https://www.example.com');
43+
44+
cy.findByRole('button', { name: 'Submit' }).should('exist');
45+
});
46+
47+
cy.findByRole('region', { name: 'html preview' }).within(() => {
48+
cy.findByRole('button', { name: 'expand' }).should('exist');
49+
cy.findByText('accessible roles:').should('exist');
50+
cy.findByText('generic').should('exist');
51+
});
52+
53+
cy.getQueryEditor().should('exist');
54+
55+
cy.findByRole('region', { name: 'query suggestion' }).within(() => {
56+
cy.findByText('> <button type="submit" > Submit </button>').should('exist');
57+
cy.findByRole('button', { name: 'expand' }).should('exist');
58+
});
59+
60+
cy.getResult().within(() => {
61+
cy.findByText('suggested query').should('exist');
62+
63+
cy.findByText("> getByRole('button', { name: /submit/i })").should('exist');
64+
65+
cy.findByRole('button', { name: 'copy query' }).should('exist');
66+
67+
cy.findByText(
68+
'There is one thing though. You could make the query a bit more specific by adding the name option.',
69+
).should('exist');
70+
71+
cy.findByRole('heading', {
72+
level: 3,
73+
name: '1. Queries Accessible to Everyone',
74+
}).should('exist');
75+
76+
cy.findByRole('heading', {
77+
level: 3,
78+
name: '2. Semantic Queries',
79+
}).should('exist');
80+
81+
cy.findByRole('heading', {
82+
level: 3,
83+
name: '3. Test IDs',
84+
}).should('exist');
85+
});
86+
});

cypress/support/commands.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/cypress/add-commands';

cypress/support/index.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
// Add common command to get the body of the sandbox iframe
20+
// See: https://www.cypress.io/blog/2020/02/12/working-with-iframes-in-cypress/
21+
Cypress.Commands.add('getSandboxBody', () => {
22+
cy.log('getSandboxBody');
23+
24+
return cy
25+
.findByTitle('sandbox')
26+
.its('0.contentDocument.body', { log: false })
27+
.should('not.be.empty')
28+
.then((body) => cy.wrap(body, { log: false }));
29+
});
30+
31+
// Add commands to clear CodeMirror editors
32+
// See: https://stackoverflow.com/questions/62012319/how-can-i-clear-a-codemirror-editor-field-from-cypress
33+
34+
Cypress.Commands.add('clearMarkupEditor', () => {
35+
cy.log('clearMarkupEditor');
36+
37+
cy.get('.CodeMirror', { log: false })
38+
.first({ log: false })
39+
.its('0.CodeMirror', { log: false })
40+
.then((editor) => {
41+
editor.setValue('');
42+
});
43+
});
44+
45+
Cypress.Commands.add('clearQueryEditor', () => {
46+
cy.log('clearQueryEditor');
47+
48+
cy.get('.CodeMirror', { log: false })
49+
.last({ log: false })
50+
.its('0.CodeMirror', { log: false })
51+
.then((editor) => {
52+
editor.setValue('');
53+
});
54+
});
55+
56+
Cypress.Commands.add('getMarkupEditor', () => {
57+
cy.log('getMarkupEditor');
58+
59+
return cy.get('.CodeMirror textarea', { log: false }).first({ log: false });
60+
});
61+
62+
Cypress.Commands.add('getQueryEditor', () => {
63+
cy.log('getQueryEditor');
64+
65+
return cy.get('.CodeMirror textarea', { log: false }).last({ log: false });
66+
});
67+
68+
Cypress.Commands.add('getResult', () => {
69+
cy.log('getResult');
70+
71+
return cy.get('div[data-testid=result]', { log: false });
72+
});

0 commit comments

Comments
 (0)