Skip to content

Commit

Permalink
Add cypress in monorepo and split by package
Browse files Browse the repository at this point in the history
  • Loading branch information
soupette committed Nov 22, 2018
1 parent efd1ed4 commit d98db64
Show file tree
Hide file tree
Showing 20 changed files with 1,468 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ package-lock.json

testApp
coverage
cypress/screenshots
cypress/videos


############################
Expand Down
11 changes: 11 additions & 0 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"chromeWebSecurity": false,
"backendUrl": "http://localhost:1337",
"baseUrl": "http://localhost:1337",
"frontLoadingDelay": 3000,
"animDelay": 1000,
"serverRestartDelay": 11000,
"viewportHeight": 900,
"viewportWidth": 1440,
"integrationFolder": "./packages"
}
23 changes: 23 additions & 0 deletions cypress/fixtures/api/category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "category",
"description": "",
"connection": "default",
"attributes": [
{
"name": "name",
"params": {
"required": true,
"unique": true,
"type": "string"
}
},
{
"name": "products",
"params": {
"key": "category",
"nature": "oneToMany",
"target": "product"
}
}
]
}
59 changes: 59 additions & 0 deletions cypress/fixtures/api/product.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "product",
"description": "",
"connection": "default",
"attributes": [
{
"name": "name",
"params": {
"required": true,
"type": "string"
}
},
{
"name": "description",
"params": {
"type": "text",
"appearance": {
"WYSIWYG": false
}
}
},
{
"name": "price",
"params": {
"type": "integer",
"default": 0
}
},
{
"name": "bool",
"params": {
"type": "boolean",
"default": false
}
},
{
"name": "bool1",
"params": {
"type": "boolean",
"default": true
}
},
{
"name": "email",
"params": {
"type": "email"
}
},
{
"name": "tags",
"params": {
"dominant": true,
"key": "products",
"nature": "manyToMany",
"target": "tag"
}
}
]
}
14 changes: 14 additions & 0 deletions cypress/fixtures/api/tag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "tag",
"description": "",
"connection": "default",
"attributes": [
{
"name": "name",
"params": {
"required": true,
"type": "string"
}
}
]
}
17 changes: 17 additions & 0 deletions cypress/fixtures/seeds/category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"name": "cat1"
},
{
"name": "cat2"
},
{
"name": "cat3"
},
{
"name": "drinks"
},
{
"name": "french food"
}
]
18 changes: 18 additions & 0 deletions cypress/fixtures/seeds/product.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"name": "name",
"description": "This is a super description",
"price": 1337,
"bool": true,
"bool1": false,
"email": "[email protected]"
},
{
"name": "name1",
"description": "This description is not cool",
"price": 4000,
"bool": false,
"bool1": true,
"email": "[email protected]"
}
]
14 changes: 14 additions & 0 deletions cypress/fixtures/seeds/tag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"name": "tag1"
},
{
"name": "tag2"
},
{
"name": "tag3"
},
{
"name": "special tag"
}
]
17 changes: 17 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
184 changes: 184 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
const stringify = JSON.stringify;
const backendUrl = Cypress.config('backendUrl');
const serverRestartDelay = Cypress.config('serverRestartDelay');

Cypress.Commands.add('createUser', () => {
const user = {
username: 'soup',
email: '[email protected]',
password: 'coucou123',
};

return cy.request({ url: `${backendUrl}/users-permissions/init`, method: 'GET' })
.then(response => {
const { body: { hasAdmin } } = response;

if (!hasAdmin) {
// Create one
cy.request({ url: `${backendUrl}/auth/local/register`, method: 'POST', body: user });
}
});
});

Cypress.Commands.add('checkModalOpening', () => {
return cy.get('.modal').invoke('show');
})

Cypress.Commands.add('deleteUser', (id, jwt) => {
cy.request({
url: `${backendUrl}/users/${id}`,
method: 'DELETE',
headers: {
Authorization: `Bearer ${jwt}`
}
});
});

Cypress.Commands.add('createProductAndTagApis', (jwt = null) => {
return cy
.fixture('api/tag.json')
.then(body => {
return cy.request({
url: `${backendUrl}/content-type-builder/models`,
method: 'POST',
headers: {
Authorization: `Bearer ${jwt}`
},
body,
})
.wait(serverRestartDelay)
.fixture('api/product.json')
.then(body => {
return cy.request({
url: `${backendUrl}/content-type-builder/models`,
method: 'POST',
headers: {
Authorization: `Bearer ${jwt}`
},
body,
})
.wait(serverRestartDelay);
});
});
});

Cypress.Commands.add('createCTMApis', (jwt = null) => {
return cy
.createProductAndTagApis(jwt)
.wait(serverRestartDelay)
.fixture('api/category.json')
.then(body => {
return cy
.request({
url: `${backendUrl}/content-type-builder/models`,
method: 'POST',
headers: {
Authorization: `Bearer ${jwt}`,
},
body,
})
.wait(serverRestartDelay);
});
});

Cypress.Commands.add('deleteAllModelData', (model, jwt, source = null) => {
// GET all data;
cy.request({
url: `${backendUrl}/content-manager/explorer/${model}`,
method: 'GET',
headers: {
Authorization: `Bearer ${jwt}`,
},
})
.then(data => {
const entriesToDelete = data.body.reduce((acc, curr) => {
return acc.concat(curr.id);
}, []);

const qs = Object.assign(entriesToDelete, source ? { source } : {});

return cy.request({
url: `${backendUrl}/content-manager/explorer/deleteAll/${model}`,
method: 'DELETE',
headers: {
Authorization: `Bearer ${jwt}`,
},
qs,
});
});
});

Cypress.Commands.add('deleteApi', (model, jwt) => {
return cy.request({
url: `${backendUrl}/content-type-builder/models/${model}`,
method: 'DELETE',
headers: {
Authorization: `Bearer ${jwt}`
},
})
.wait(serverRestartDelay);
});

Cypress.Commands.add('login', () => {
cy.createUser()
return cy.request({
url: `${backendUrl}/auth/local`,
method: 'POST',
body: {
identifier: 'soup',
password: 'coucou123',
},
})
.then(response => {
window.localStorage.setItem('jwtToken', stringify(response.body.jwt));
window.localStorage.setItem('userInfo', stringify(response.body.user));

return response.body;
})
});

Cypress.Commands.add('seedData', (model, jwt, source = null) => {
return cy
.fixture(`seeds/${model}.json`)
.then(seed => {
seed.forEach(body => {
cy.request({
method: 'POST',
url: `${backendUrl}/content-manager/explorer/${model}?source='content-manager`,
headers: {
Authorization: `Bearer ${jwt}`,
},
body,
});
});
});
});

Cypress.Commands.add('submitForm', () => {
return cy.get('form').submit();
});
Loading

0 comments on commit d98db64

Please sign in to comment.