Skip to content

Instantly share code, notes, and snippets.

@alanbsmith
Created January 15, 2018 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanbsmith/2bd52b24b7a4d3d531d1830d15badfec to your computer and use it in GitHub Desktop.
Save alanbsmith/2bd52b24b7a4d3d531d1830d15badfec to your computer and use it in GitHub Desktop.
// __tests__/validatePhone.js
import validatePhone from "../lib/validatePhone";
describe("validatePhone", () => {
it("should return true for a valid phone number", () => {
const validator = validatePhone("2022243121");
expect(validator).toBe(true);
});
it("should ignore spaces between numbers", () => {
const validator = validatePhone("202 224 3121");
expect(validator).toBe(true);
});
it("should ignore parentheses between numbers", () => {
const validator = validatePhone("(202) 224 3121");
expect(validator).toBe(true);
});
it("should ignore hyphens between numbers", () => {
const validator = validatePhone("(202) 224-3121");
const validator2 = validatePhone("202-224-3121");
expect(validator).toBe(true);
expect(validator2).toBe(true);
});
it("should ignore periods between numbers", () => {
const validator = validatePhone("202.224.3121");
const validator2 = validatePhone("(202) 224.3121");
expect(validator).toBe(true);
expect(validator2).toBe(true);
});
it("should return false if letters are present", () => {
const validator = validatePhone("(202) 224 ABCD");
const validator2 = validatePhone("(202) 224 3121 WXYZ");
expect(validator).toBe(false);
expect(validator2).toBe(false);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment