-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from esteticalVE/email-validation
Added email validation, unit-tests
- Loading branch information
Showing
15 changed files
with
1,645 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.cache | ||
package.json | ||
package-lock.json | ||
yarn-lock.json | ||
public |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "test-file-stub" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const React = require("react") | ||
const gatsby = jest.requireActual("gatsby") | ||
|
||
module.exports = { | ||
...gatsby, | ||
graphql: jest.fn(), | ||
Link: jest | ||
.fn() | ||
.mockImplementation( | ||
({ | ||
activeClassName, | ||
activeStyle, | ||
getProps, | ||
innerRef, | ||
partiallyActive, | ||
ref, | ||
replace, | ||
to, | ||
...rest | ||
}) => | ||
React.createElement("a", { | ||
...rest, | ||
href: to, | ||
}) | ||
), | ||
StaticQuery: jest.fn(), | ||
useStaticQuery: jest.fn(), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const babelOptions = { | ||
presets: ["babel-preset-gatsby"], | ||
} | ||
|
||
module.exports = require("babel-jest").createTransformer(babelOptions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = { | ||
transform: { | ||
"^.+\\.jsx?$": "<rootDir>/jest-preprocess.js", | ||
}, | ||
moduleNameMapper: { | ||
".+\\.(css|styl|less|sass|scss)$": "identity-obj-proxy", | ||
".+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": | ||
"<rootDir>/__mocks__/file-mock.js", | ||
}, | ||
testPathIgnorePatterns: ["node_modules", ".cache", "public"], | ||
transformIgnorePatterns: ["node_modules/(?!(gatsby)/)"], | ||
globals: { | ||
__PATH_PREFIX__: "", | ||
}, | ||
testURL: "http://localhost", | ||
setupFiles: ["<rootDir>/loadershim.js"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
global.___loader = { | ||
enqueue: jest.fn(), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ValidateError } from "./validate-error" | ||
// eslint-disable-next-line | ||
const validEmailReg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ | ||
|
||
export const validateEmail = value => { | ||
if (!value && value.trim() === "") { | ||
throw new ValidateError("Email is empty") | ||
} | ||
if (!validEmailReg.test(value)) { | ||
throw new ValidateError("Email is not valid, please try again") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { validateEmail } from "./validate-email" | ||
import { ValidateError } from "./validate-error" | ||
|
||
describe("utils/validate-email", () => { | ||
const errorTexts = { | ||
empty: "Email is empty", | ||
invalid: "Email is not valid, please try again", | ||
} | ||
it("Should throw error", () => { | ||
expect(() => validateEmail("")).toThrowError(ValidateError) | ||
expect(() => validateEmail("test")).toThrowError(ValidateError) | ||
expect(() => validateEmail("test@test")).toThrowError(ValidateError) | ||
expect(() => validateEmail("test.test.com")).toThrowError(ValidateError) | ||
expect(() => validateEmail(`test"test"test@test.test`)).toThrowError( | ||
ValidateError | ||
) | ||
expect(() => validateEmail("test@test@test@test.test")).toThrowError( | ||
ValidateError | ||
) | ||
expect(() => | ||
validateEmail(`a\"b(c)d,e:f;g<h>i[j\\k]l@example.com`) | ||
).toThrowError(ValidateError) | ||
expect(() => | ||
validateEmail(`test\\ test\\"test\\\\test@test.test`) | ||
).toThrowError(ValidateError) | ||
expect(() => validateEmail("test_test@test_test.test.com")).toThrowError( | ||
ValidateError | ||
) | ||
}) | ||
it("Should throw error with correct error text", () => { | ||
expect(() => validateEmail("")).toThrowError(errorTexts.empty) | ||
expect(() => validateEmail("test@test")).toThrowError(errorTexts.invalid) | ||
expect(() => validateEmail("test_test@test_test.test.com")).toThrowError( | ||
errorTexts.invalid | ||
) | ||
}) | ||
it("Should not throw error with correct email", () => { | ||
expect(() => validateEmail("sbrichards@gmail.com")).not.toThrowError() | ||
expect(() => validateEmail("ve.gr94@gmail.com")).not.toThrowError() | ||
expect(() => validateEmail("test_test@ya.ru")).not.toThrowError() | ||
expect(() => validateEmail("test+test@test.com")).not.toThrowError() | ||
expect(() => validateEmail("a@test.com")).not.toThrowError() | ||
expect(() => validateEmail("test-test@test-test.com")).not.toThrowError() | ||
expect(() => validateEmail("test!test@test.test")).not.toThrowError() | ||
expect(() => validateEmail("test%test.test@test.test")).not.toThrowError() | ||
expect(() => | ||
validateEmail("test.test-test-test@test.test") | ||
).not.toThrowError() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class ValidateError extends Error { | ||
constructor(message) { | ||
super(message) | ||
this.name = this.constructor.name | ||
Object.setPrototypeOf(this, ValidateError.prototype) | ||
} | ||
} |
Oops, something went wrong.