Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

[#361] [CLEANUP] Dé-duplication de la validation d'e-mail. #361

Merged
merged 1 commit into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions live/app/components/follower-form.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ember from 'ember';
import ENV from 'pix-live/config/environment';
import isEmailValid from 'pix-live/utils/email-validator';

const messageDisplayDuration = 1500;

Expand All @@ -21,7 +22,6 @@ export default Ember.Component.extend({

classNames: ['follower-form'],

emailValidator: Ember.inject.service('email-validator'),
store: Ember.inject.service(),
errorType:'invalid' ,
status: 'empty', // empty | pending | success | error
Expand Down Expand Up @@ -63,18 +63,11 @@ export default Ember.Component.extend({
return (this.get('status') === 'pending') ? 'envoi en cours' : 's\'inscrire';
}),

_checkEmail(email){
if (!this.get('emailValidator').emailIsValid(email)) {
return false;
}
return true;
},

actions: {
submit(){
this.set('status', 'pending');
const email = (this.get('followerEmail'))? this.get('followerEmail').trim() : '';
if (!this._checkEmail(email) || email.length < 1) {
if (!isEmailValid(email)) {
this.set('status', 'error');
hideMessageDiv(this);
return;
Expand Down
15 changes: 0 additions & 15 deletions live/app/services/email-validator.js

This file was deleted.

7 changes: 5 additions & 2 deletions live/app/utils/email-validator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export default function isValidate(email) {
//XXX: Cf - http://stackoverflow.com/a/46181/5430854
export default function isEmailValid(email) {
if (!email) {
return false;
}
// From http://stackoverflow.com/a/46181/5430854
const pattern = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
return pattern.test(email.trim());
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import {expect} from 'chai';
import {describe, it} from 'mocha';
import {setupTest} from 'ember-mocha';
import isEmailValid from 'pix-live/utils/email-validator';

describe('Unit | Service | EmailValidatorService', function () {

setupTest('service:email-validator', {});
let validator;
beforeEach(function () {
validator = this.subject();
});

it('exists', function () {
expect(validator).to.be.ok;
});

describe('Test all case Invalid and then valid email', function () {
describe('Unit | Utility | email validator', function () {
describe('Invalid emails', function () {
[
'',
' ',
Expand All @@ -27,10 +16,12 @@ describe('Unit | Service | EmailValidatorService', function () {
'@pix'
].forEach(function (badEmail) {
it(`should return false when email is invalid: ${badEmail}`, function () {
expect(validator.emailIsValid(badEmail)).to.be.false;
expect(isEmailValid(badEmail)).to.be.false;
});
});
});

describe('Valid emails', function () {
[
'follower@pix.fr',
'follower@pix.fr ',
Expand All @@ -43,10 +34,8 @@ describe('Unit | Service | EmailValidatorService', function () {
'follower+beta@pix.beta.gouv.fr'
].forEach(function (validEmail) {
it(`should return true if provided email is valid: ${validEmail}`, function () {
expect(validator.emailIsValid(validEmail)).to.be.true;
expect(isEmailValid(validEmail)).to.be.true;
});
});
});


});