Added toHaveBeenCalled tests
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
/* @flow */
|
||||
"use strict";
|
||||
|
||||
/*
|
||||
In real life, validateUser could check a database,
|
||||
look into an Active Directory, call another service,
|
||||
etc. -- but for this demo, let's keep it quite
|
||||
simple and only accept a single hardcoded user.
|
||||
*/
|
||||
|
||||
const validateUser = (
|
||||
userName: string,
|
||||
password: string,
|
||||
callback: (?string, ?string) => void
|
||||
) => {
|
||||
if (!userName || !password) {
|
||||
callback("Missing user/password", null);
|
||||
} else if (userName === "fkereki" && password === "modernjsbook") {
|
||||
callback(null, "fkereki"); // OK, send userName back
|
||||
} else {
|
||||
callback("Not valid user", null);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = validateUser;
|
||||
@@ -0,0 +1,25 @@
|
||||
/* @flow */
|
||||
"use strict";
|
||||
|
||||
const validateUser = require("./validate_user");
|
||||
|
||||
describe("validateUser", () => {
|
||||
it("should reject a call with empty user", () => {
|
||||
const cb = jest.fn();
|
||||
validateUser("", "somepass", cb);
|
||||
expect(cb).toHaveBeenCalled();
|
||||
expect(cb).toHaveBeenCalledWith("Missing user/password", null);
|
||||
});
|
||||
|
||||
it("should reject a wrong password", () => {
|
||||
const cb = jest.fn();
|
||||
validateUser("fkereki", "wrongpassword", cb);
|
||||
expect(cb).toHaveBeenCalledWith("Not valid user", null);
|
||||
});
|
||||
|
||||
it("should accept a correct password", () => {
|
||||
const cb = jest.fn();
|
||||
validateUser("fkereki", "modernjsbook", cb);
|
||||
expect(cb).toHaveBeenCalledWith(null, "fkereki");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user