From 4ed3911d454e7dc8ceb3344b157fdaf42691f885 Mon Sep 17 00:00:00 2001 From: Colin DAMON Date: Mon, 27 Jun 2022 09:50:58 +0200 Subject: [PATCH] Add cypress tests to modules page --- src/test/javascript/cypress/cypress-config.ts | 3 +- .../javascript/cypress/fixtures/modules.json | 38 +++++++++ .../cypress/integration/Modules.spec.ts | 81 ++++++++++++++++++- .../javascript/cypress/support/Interceptor.ts | 21 +++++ 4 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 src/test/javascript/cypress/fixtures/modules.json create mode 100644 src/test/javascript/cypress/support/Interceptor.ts diff --git a/src/test/javascript/cypress/cypress-config.ts b/src/test/javascript/cypress/cypress-config.ts index 91a29b677b5..52b6246fee6 100644 --- a/src/test/javascript/cypress/cypress-config.ts +++ b/src/test/javascript/cypress/cypress-config.ts @@ -4,7 +4,8 @@ export default defineConfig({ e2e: { baseUrl: 'http://localhost:7471', specPattern: 'src/test/javascript/cypress/integration/**/*.spec.ts', - fixturesFolder: false, + fixturesFolder: 'src/test/javascript/cypress/fixtures', + supportFolder: 'src/test/javascript/cypress/support', supportFile: false, video: false, }, diff --git a/src/test/javascript/cypress/fixtures/modules.json b/src/test/javascript/cypress/fixtures/modules.json new file mode 100644 index 00000000000..05a41bb88ed --- /dev/null +++ b/src/test/javascript/cypress/fixtures/modules.json @@ -0,0 +1,38 @@ +{ + "categories": [ + { + "name": "Spring", + "modules": [ + { + "slug": "spring-cucumber", + "description": "Add cucumber to the application", + "properties": { + "definitions": [ + { + "type": "STRING", + "mandatory": true, + "key": "baseName", + "description": "Application base name", + "example": "jhipster" + }, + { + "type": "BOOLEAN", + "mandatory": false, + "key": "optionalBoolean" + }, + { + "type": "INTEGER", + "mandatory": false, + "key": "optionalInteger" + } + ] + } + }, + { + "slug": "spring-test", + "description": "Add spring test to the application" + } + ] + } + ] +} diff --git a/src/test/javascript/cypress/integration/Modules.spec.ts b/src/test/javascript/cypress/integration/Modules.spec.ts index 6a7076e5c8c..7d0e74879a9 100644 --- a/src/test/javascript/cypress/integration/Modules.spec.ts +++ b/src/test/javascript/cypress/integration/Modules.spec.ts @@ -1,9 +1,84 @@ import { dataSelector } from '../support/selector'; +import { interceptForever } from '../support/Interceptor'; describe('Modules', () => { - it('Should display modules', () => { - cy.visit('/modules'); + describe('E2E', () => { + it('Should display modules', () => { + cy.visit('/modules'); - cy.get(dataSelector('modules-list')).should('be.visible'); + cy.get(dataSelector('modules-list')).should('be.visible'); + }); + }); + + describe('Component', () => { + it('Should display loader while loading modules', () => { + const result = interceptForever({ path: '/api/modules' }, { fixture: 'modules.json' }); + + cy.visit('/modules'); + + cy.get(dataSelector('modules-loader')) + .should('be.visible') + .then(() => { + result.send(); + + cy.get(dataSelector('modules-loader')).should('not.exist'); + cy.get(dataSelector('modules-list')).should('be.visible'); + cy.get(dataSelector('module-spring-test-application-button')).should('be.disabled'); + cy.get(dataSelector('module-spring-cucumber-application-button')).should('be.disabled'); + }); + }); + + it('Should apply module without properties', () => { + cy.intercept({ path: '/api/modules' }, { fixture: 'modules.json' }); + + cy.intercept({ + path: '/api/modules/spring-test/apply', + method: 'POST', + }).as('spring-test-creation'); + + cy.visit('/modules'); + cy.get(dataSelector('folder-path-field')).type('test'); + cy.get(dataSelector('module-spring-test-application-button')).click(); + + cy.wait('@spring-test-creation').should(xhr => { + const body = xhr.request.body; + + expect(body).to.deep.equal({ + projectFolder: 'test', + properties: {}, + }); + }); + }); + + it('Should apply module with properties', () => { + cy.intercept({ path: '/api/modules' }, { fixture: 'modules.json' }); + + cy.intercept({ + path: '/api/modules/spring-cucumber/apply', + method: 'POST', + }).as('spring-cucumber-creation'); + + cy.visit('/modules'); + + cy.get(dataSelector('spring-cucumber-module-content')).click(); + cy.get(dataSelector('folder-path-field')).type('test'); + cy.get(dataSelector('property-baseName-field')).type('jhipster'); + cy.get(dataSelector('property-optionalBoolean-field')).select('true'); + cy.get(dataSelector('property-optionalInteger-field')).type('42'); + cy.get(dataSelector('module-spring-cucumber-application-button')).click(); + + cy.wait('@spring-cucumber-creation').should(xhr => { + const body = xhr.request.body; + + expect(body).to.deep.equal({ + projectFolder: 'test', + properties: { + baseName: 'jhipster', + optionalBoolean: true, + optionalInteger: 42, + }, + }); + }); + }); }); }); diff --git a/src/test/javascript/cypress/support/Interceptor.ts b/src/test/javascript/cypress/support/Interceptor.ts new file mode 100644 index 00000000000..e91adfa04f4 --- /dev/null +++ b/src/test/javascript/cypress/support/Interceptor.ts @@ -0,0 +1,21 @@ +import { HttpResponseInterceptor, RouteMatcher, StaticResponse } from '../../../../../node_modules/cypress/types/net-stubbing'; + +type ResponseSender = { + send: () => void; +}; + +export const interceptForever = (requestMatcher: RouteMatcher, response?: StaticResponse | HttpResponseInterceptor): ResponseSender => { + let send; + + const trigger = new Promise(resolve => { + send = resolve; + }); + + cy.intercept(requestMatcher, request => { + return trigger.then(() => { + request.reply(response); + }); + }); + + return { send }; +};