Created
December 12, 2021 16:05
-
-
Save hansott/0e25fcb1e737e3964398160d3aab00a3 to your computer and use it in GitHub Desktop.
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
describe("ForgotPassword", () => { | |
test("it sends email when reset is requested", async () => { | |
const users = new UsersInMemory(); | |
const forgotPassword = new ForgotPassword(users); | |
// ... | |
}); | |
}); |
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
export class ForgotPassword { | |
constructor(private readonly users: Users) {} | |
async requestReset(email: Email) { | |
const user = await this.users.getByEmail(email); | |
// ... | |
} | |
} |
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
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(); | |
}); | |
}); |
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
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