Skip to content

Commit

Permalink
fix(tests): Test avatar upload (#362)
Browse files Browse the repository at this point in the history
Follow up of PR #358. Contributes to #199.

Also:
- removes all Webdriver leftovers;
- converts all Cypress support files to TypeScript;
- stops running Cypress tests from the "Docker" CI workflow. (too complicated for too little added value)
  • Loading branch information
adrienjoly committed Sep 5, 2020
1 parent 8119724 commit 04c4f91
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 77 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,4 @@ jobs:
run: |
docker-compose exec -T web npm run test-api
- name: Run Cypress E2E tests
run: |
npx cypress@3.8.3 run --config pluginsFile=false # because plugins/index.js requires typescript, which is not installed in this production image
# To publish the Docker image from here, see https://github.com/openwhyd/openwhyd/pull/308/commits/1eacaa98885789642ba0073c9bb4d822021f0d95#diff-12a86cef0c4707531fdbabac3e38cb2aR36
1 change: 1 addition & 0 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"screenshotOnRunFailure": false,
"video": false,
"chromeWebSecurity": false,
"baseUrl": "http://localhost:8080"
}
85 changes: 19 additions & 66 deletions cypress/integration/upload.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
context('upload', () => {
const SAMPLE_IMG_PATH = 'upload-resources/sample-avatar.jpg';
let userId;

beforeEach('login', () => {
Expand All @@ -14,78 +15,30 @@ context('upload', () => {
cy.request(`/images/blank_user.gif`).then((response) => {
defaultImageBody = response.body;
});
cy.request(`/img/u/${userId}?_t=${new Date().getTime()}`).should(
(response) => {
expect(response.body).to.equal(defaultImageBody);
}
cy.request(
`/img/u/${userId}?_t=${new Date().getTime()}`
).should((response) =>
expect(response.body.length).to.equal(defaultImageBody.length)
);

// upload a new profile image
// open the "edit profile" dialog
cy.visit(`/u/${userId}`); // user's profile page
cy.get('body').contains('Edit profile').click();
cy.get('body').contains('Edit Profile Info').click();
cy.get('body').contains('Drop your image file here'); // to wait for upload scripts to load and init propertly on the page

// TODO: make the following tests (from webdriver) work:
/*
it(`upload sample avatar`, function() {
browser.pause(200);
var path = __dirname + 'test/specs/upload-resources/sample-avatar.jpg';
// 0. display form and file input (for browser security reasons)
browser.execute(function() {
$('#avatarForm').show();
});
browser.pause(200);
// 1. add file input
$('input[type="file"]').addValue(path);
browser.pause(200);
//browser.keys(path);
$('input[type="file"]').keys(path);
$('#avatarForm').submitForm();
browser.pause(200);
// 2. simulate drag and drop
/ *
browser.execute(function(path) {
var files = [ path ];
var eve = document.createEvent("HTMLEvents");
eve.initEvent("drop", true, true);
eve.type = "drop";
eve.dataTransfer = {
preventDefault: function() {},
files: files,
item: function(i) { return files[i]; },
};
document.getElementById("avatarDrop").dispatchEvent(eve);
$('#avatarDrop')[0].ondrop({ preventDefault: function(){}, dataTransfer: { files: files } });
}, path);
* /
// test in browser:
// var path = '/Users/adrienjoly/dev/openwhyd/openwhyd/test/specs/upload-resources/sample-avatar.jpg';
// $('input[type="file"]').val(path); // does not work, for security reasons
// $('#avatarDrop')[0].ondrop({ preventDefault: function(){}, dataTransfer: { files: [ path ] } });
});
it(`save profile changes`, function() {
browser.pause(5000)
browser.clickOnContent('Save');
});
it(`has new avatar"`, function async() {
return new Promise(function (resolve, reject) {
browser.pause(1000).then(function() {
//assert.ok(!/blank_user.gif/.test($('.avatar-box img').getAttribute('src')));
request(`${URL_PREFIX}/img/u/${ADMIN_USER.id}?_t=${new Date().getTime()}`, function (error, response, body) {
console.log('defaultAvatarLen', defaultAvatarLen);
console.log('current avatar length', body.length);
assert.notEqual(defaultAvatarLen, body.length);
resolve();
});
});
});
});
// upload a new profile image
cy.get('input[type="file"]').attachFile(SAMPLE_IMG_PATH); // to upload the file
cy.get('body').contains('Drop your image file here'); // to wait for the progress bar to disappear
cy.wait(1000); // to wait for the form to be ready to save
cy.get('body').contains('Save').scrollIntoView().click({ force: true });
cy.wait(1000); // to wait for the dialog to close and page to refresh

it(`should output browser log`, function () {
console.log('browser log:', browser.log('browser').value.slice(-10));
});
*/
// check that the user's profile image was updated
cy.request(
`/img/u/${userId}?_t=${new Date().getTime()}`
).should((response) =>
expect(response.body.length).not.to.equal(defaultImageBody.length)
);
});
});
3 changes: 0 additions & 3 deletions cypress/plugins/index.js

This file was deleted.

3 changes: 3 additions & 0 deletions cypress/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = (on) => {
on; // e.g. on('file:preprocessor', cypressTypeScriptPreprocessor);
};
2 changes: 2 additions & 0 deletions cypress/support/commands.js → cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* global Cypress, cy */

import 'cypress-file-upload';

// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
Expand Down
2 changes: 0 additions & 2 deletions cypress/support/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="cypress" />

// To let TypeScript compiler know that we have added a custom command and have IntelliSense working
// (see https://github.com/cypress-io/cypress-example-todomvc#cypress-intellisense)

Expand Down
4 changes: 3 additions & 1 deletion cypress/support/index.js → cypress/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
// https://on.cypress.io/configuration
// ***********************************************************

require('./commands');
/// <reference types="./" />

import './commands';

beforeEach(function () {
// reset the db before each it() test, across all files no matter what,
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@semantic-release/git": "^9.0.0",
"all-contributors-cli": "6.17.2",
"cypress": "^5.1.0",
"cypress-file-upload": "^4.1.1",
"eslint": "^7.8.1",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"include": ["node_modules/cypress", "**/*.js", "cypress/*/*.ts"]
"include": ["node_modules/cypress", "cypress/*/*.ts", "cypress/*/*.d.ts"]
}

0 comments on commit 04c4f91

Please sign in to comment.