Skip to content

Instantly share code, notes, and snippets.

@hansott
Created December 12, 2021 16:05
Show Gist options
  • Save hansott/0e25fcb1e737e3964398160d3aab00a3 to your computer and use it in GitHub Desktop.
Save hansott/0e25fcb1e737e3964398160d3aab00a3 to your computer and use it in GitHub Desktop.
describe("ForgotPassword", () => {
test("it sends email when reset is requested", async () => {
const users = new UsersInMemory();
const forgotPassword = new ForgotPassword(users);
// ...
});
});
export class ForgotPassword {
constructor(private readonly users: Users) {}
async requestReset(email: Email) {
const user = await this.users.getByEmail(email);
// ...
}
}
function generateTests(instance: () => Users) {
test("it returns user by email", async () => {
const users = instance();
const john = new User(/* ... */);
await users.persist(john);
expect(await users.getByEmail(john.getEmail())).toEqual(john);
});
}
describe("UsersInMemory", () => {
generateTests(() => new UsersInMemory());
});
describe("UsersMySQL", () => {
beforeEach(async () => {
// ...
await mysql.query("TRUNCATE users");
});
generateTests(() => new UsersMySQL(mysql));
afterEach(() => {
mysql.close();
});
});
export interface Users {
getByEmail(email: Email): Promise<User | undefined>;
persist(user: User): Promise<void>;
}
export class UsersInMySQL implements Users {
constructor(private readonly mysql: MySql) {}
async getByEmail(email: Email) {
const rows = await this.mysql.select(
"SELECT id, email, ... FROM users WHERE email = LOWERCASE(:email)",
{ email: email.toString() }
);
// ...
}
async persist(user: User) {
await this.mysql.insert("INSERT INTO users (...) VALUES (...) ON DUPLICATE KEY UPDATE ...");
}
// ...
}
export class UsersInMemory implements Users {
private users: User[] = [];
async getByEmail(email: Email) {
return this.users.find(/* ... */);
}
async persist(user: User) {
// Simplified
this.users.push(user);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment