Skip to content

Commit f194c39

Browse files
committed
feat: add cypress and e2e tests
1 parent 592e934 commit f194c39

7 files changed

Lines changed: 822 additions & 29 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ npm-debug.log
1414
build
1515
dist
1616
bundle
17+
18+
# Cypress
19+
cypress/screenshots/
20+
cypress/videos/
21+
cypress/downloads/

admin/src/index.cy.jsx

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/// <reference types="cypress" />
2+
//
3+
4+
describe('Config Sync', () => {
5+
beforeEach(() => {
6+
cy.task('deleteFolder', 'playground/config/sync');
7+
});
8+
9+
it('Check the config diff', () => {
10+
cy.login();
11+
cy.navigateToInterface();
12+
cy.initialExport();
13+
14+
cy.makeConfigChanges();
15+
16+
cy.navigateToInterface();
17+
18+
cy.get('tbody tr').contains('plugin_users-permissions_advanced').click();
19+
20+
cy.contains('"unique_email": true,');
21+
cy.contains('"unique_email": false,');
22+
});
23+
24+
it('Download the config as zip', () => {
25+
cy.login();
26+
cy.navigateToInterface();
27+
cy.initialExport();
28+
29+
cy.intercept({
30+
method: 'GET',
31+
url: '/config-sync/zip',
32+
}).as('getConfigZip');
33+
34+
cy.get('button').contains('Download Config').click();
35+
36+
cy.wait('@getConfigZip').then((interception) => {
37+
const configZipResponse = interception.response.body;
38+
const downloadsFolder = Cypress.config('downloadsFolder');
39+
cy.readFile(`${downloadsFolder}/${configZipResponse.name.replaceAll(':', '_')}`).should('exist');
40+
});
41+
});
42+
43+
it('Partial import & export', () => {
44+
cy.login();
45+
cy.navigateToInterface();
46+
cy.initialExport();
47+
48+
cy.makeConfigChanges();
49+
50+
cy.navigateToInterface();
51+
52+
cy.get('button[aria-label="Select all entries"]').click();
53+
54+
cy.intercept({
55+
method: 'POST',
56+
url: '/config-sync/import',
57+
}).as('importConfig');
58+
cy.get('button[aria-label="Select plugin_upload_settings"]').click();
59+
cy.get('button').contains('Import').click();
60+
cy.get('button').contains('Yes, import').click();
61+
cy.wait('@importConfig').its('response.statusCode').should('equal', 200);
62+
cy.contains('plugin_users-permissions_advanced');
63+
cy.contains('plugin_users-permissions_email');
64+
65+
cy.intercept({
66+
method: 'POST',
67+
url: '/config-sync/export',
68+
}).as('exportConfig');
69+
cy.get('button[aria-label="Select plugin_users-permissions_advanced"]').click();
70+
cy.get('button').contains('Export').click();
71+
cy.get('button').contains('Yes, export').click();
72+
cy.wait('@exportConfig').its('response.statusCode').should('equal', 200);
73+
cy.contains('plugin_users-permissions_email');
74+
});
75+
});

cypress.config.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { defineConfig } = require('cypress');
2+
const fs = require('fs-extra');
3+
4+
module.exports = defineConfig({
5+
e2e: {
6+
baseUrl: 'http://localhost:1337',
7+
specPattern: '**/*.cy.{js,ts,jsx,tsx}',
8+
video: false,
9+
defaultCommandTimeout: 10000,
10+
requestTimeout: 10000,
11+
setupNodeEvents(on, config) {
12+
// implement node event listeners here.
13+
require('cypress-terminal-report/src/installLogsPrinter')(on);
14+
15+
on('task', {
16+
deleteFolder(folderName) {
17+
console.log(`deleting folder ${folderName}`);
18+
19+
return fs.remove(folderName)
20+
.then(() => {
21+
console.log(`folder ${folderName} deleted`);
22+
return null;
23+
})
24+
.catch(err => {
25+
console.error(`error deleting folder ${folderName}`, err);
26+
throw err;
27+
});
28+
},
29+
});
30+
},
31+
},
32+
});

cypress/support/commands.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/// <reference types="cypress" />
2+
// ***********************************************
3+
// This example commands.ts shows you how to
4+
// create various custom commands and overwrite
5+
// existing commands.
6+
//
7+
// For more comprehensive examples of custom
8+
// commands please read more here:
9+
// https://on.cypress.io/custom-commands
10+
// ***********************************************
11+
//
12+
//
13+
// -- This is a parent command --
14+
// Cypress.Commands.add('login', (email, password) => { ... })
15+
//
16+
//
17+
// -- This is a child command --
18+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
19+
//
20+
//
21+
// -- This is a dual command --
22+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
23+
//
24+
//
25+
// -- This will overwrite an existing command --
26+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
27+
//
28+
29+
Cypress.Commands.add('login', (path) => {
30+
cy.visit('/');
31+
32+
cy.intercept({
33+
method: 'GET',
34+
url: '/admin/users/me',
35+
}).as('sessionCheck');
36+
37+
cy.intercept({
38+
method: 'GET',
39+
url: '/admin/init',
40+
}).as('adminInit');
41+
42+
// Wait for the initial request to complete.
43+
cy.wait('@adminInit').its('response.statusCode').should('equal', 200);
44+
45+
// Wait for the form to render.
46+
cy.wait(1000);
47+
48+
cy.get('body').then(($body) => {
49+
// Login
50+
if ($body.text().includes('Log in to your Strapi account')) {
51+
cy.get('input[name="email"]').type('johndoe@example.com');
52+
cy.get('input[name="password"]').type('Abc12345678');
53+
cy.get('button[type="submit"]').click();
54+
cy.wait('@sessionCheck').its('response.statusCode').should('equal', 200);
55+
}
56+
// Register
57+
if ($body.text().includes('Credentials are only used to authenticate in Strapi')) {
58+
cy.get('input[name="firstname"]').type('John');
59+
cy.get('input[name="email"]').type('johndoe@example.com');
60+
cy.get('input[name="password"]').type('Abc12345678');
61+
cy.get('input[name="confirmPassword"]').type('Abc12345678');
62+
cy.get('button[type="submit"]').click();
63+
cy.wait('@sessionCheck').its('response.statusCode').should('equal', 200);
64+
}
65+
});
66+
});
67+
68+
Cypress.Commands.add('navigateToInterface', (path) => {
69+
cy.intercept({
70+
method: 'GET',
71+
url: '/config-sync/diff',
72+
}).as('getConfigDiff');
73+
74+
cy.get('a[href="/admin/settings"]').click();
75+
cy.get('a[href="/admin/settings/config-sync"]').click();
76+
77+
cy.wait('@getConfigDiff').its('response.statusCode').should('equal', 200);
78+
});
79+
80+
81+
Cypress.Commands.add('initialExport', (path) => {
82+
cy.intercept({
83+
method: 'POST',
84+
url: '/config-sync/export',
85+
}).as('exportConfig');
86+
87+
cy.get('button').contains('Make the initial export').click();
88+
cy.get('button').contains('Yes, export').click();
89+
90+
cy.wait('@exportConfig').its('response.statusCode').should('equal', 200);
91+
92+
cy.contains('Config was successfully exported to config/sync/.');
93+
});
94+
95+
Cypress.Commands.add('makeConfigChanges', (path) => {
96+
// Change a setting in the UP advanced settings
97+
cy.intercept({
98+
method: 'PUT',
99+
url: '/users-permissions/advanced',
100+
}).as('saveUpAdvanced');
101+
cy.get('a[href="/admin/settings/users-permissions/advanced-settings"]').click();
102+
cy.get('input[name="unique_email"').click();
103+
cy.get('button[type="submit"]').click();
104+
cy.wait('@saveUpAdvanced').its('response.statusCode').should('equal', 200);
105+
106+
// Change a setting in the media library settings
107+
cy.intercept({
108+
method: 'PUT',
109+
url: '/upload/settings',
110+
}).as('saveMediaLibrarySettings');
111+
cy.get('a[href="/admin/settings/media-library"]').click();
112+
cy.get('input[name="responsiveDimensions"').click();
113+
cy.get('button[type="submit"]').click();
114+
cy.wait('@saveMediaLibrarySettings').its('response.statusCode').should('equal', 200);
115+
116+
// Change a setting in the email templates
117+
cy.intercept({
118+
method: 'PUT',
119+
url: '/users-permissions/email-templates',
120+
}).as('saveUpEmailTemplates');
121+
cy.get('a[href="/admin/settings/users-permissions/email-templates"]').click();
122+
cy.get('tbody tr').contains('Reset password').click();
123+
cy.get('input[name="options.response_email"]').clear().type(`${Math.random().toString(36).substring(2, 15)}@example.com`);
124+
cy.get('button[type="submit"]').click();
125+
cy.wait('@saveUpEmailTemplates').its('response.statusCode').should('equal', 200);
126+
});

cypress/support/e2e.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ***********************************************************
2+
// This example support/e2e.ts 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+
require('cypress-terminal-report/src/installLogsCollector')();
20+
21+
// Alternatively you can use CommonJS syntax:
22+
// require('./commands')

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"eslint:fix": "eslint --fix './**/*.{js,jsx}'",
3737
"test:unit": "jest --verbose",
3838
"test:integration": "cd playground && node_modules/.bin/jest --verbose --forceExit --detectOpenHandles",
39+
"test:e2e": "cypress open",
3940
"playground:install": "yarn playground:yalc-add-link && cd playground && yarn install",
4041
"playground:yalc-add": "cd playground && yalc add strapi-plugin-config-sync",
4142
"playground:yalc-add-link": "cd playground && yalc add --link strapi-plugin-config-sync",
@@ -95,12 +96,15 @@
9596
"@strapi/strapi": "^5.0.0",
9697
"@strapi/utils": "^5.0.0",
9798
"babel-eslint": "9.0.0",
99+
"cypress": "^13.9.0",
100+
"cypress-terminal-report": "^6.0.2",
98101
"eslint": "^7.32.0",
99102
"eslint-config-airbnb": "^18.2.1",
100103
"eslint-config-react-app": "^3.0.7",
101104
"eslint-import-resolver-webpack": "^0.11.0",
102105
"eslint-loader": "^4.0.2",
103106
"eslint-plugin-babel": "^5.3.0",
107+
"eslint-plugin-cypress": "^3.2.0",
104108
"eslint-plugin-flowtype": "2.50.1",
105109
"eslint-plugin-import": "^2.22.1",
106110
"eslint-plugin-jsx-a11y": "^6.4.1",

0 commit comments

Comments
 (0)