Skip to content

Commit

Permalink
add email integration tests (#2)
Browse files Browse the repository at this point in the history
* refactor to send welcome email to the user

* implement send email with mailgun

* implement send email with mailgun

* add huskey

* update ci

* update ci

* update ci
  • Loading branch information
DanielRamosAcosta authored Sep 24, 2023
1 parent c768d49 commit 2368534
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 53 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ jobs:
- run: npm run format:check
- run: npm run migrate
- run: npm run test:all
env:
MAILGUN_API_KEY: ${{ secrets.MAILGUN_API_KEY }}
TESTMAIL_API_KEY: ${{ secrets.TESTMAIL_API_KEY }}
3 changes: 3 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
npm run lint:check
npm run format:check
npm run test:unit -- --run
68 changes: 32 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
"devDependencies": {
"@vitest/coverage-v8": "^0.34.5",
"eslint": "^8.49.0",
"mongodb": "^6.1.0",
"husky": "^8.0.3",
"prettier": "^3.0.3",
"vitest": "^0.34.5"
},
"scripts": {
"test": "vitest",
"test:integration": "vitest --config vite.config.integration.js",
"test:all": "vitest --config vite.config.all.js",
"test": "npm run test:unit",
"test:all": "vitest",
"test:integration": "vitest --run --config vite.config.integration.js",
"test:unit": "vitest --run --config vite.config.unit.js",
"test:coverage": "vitest --coverage",
"lint:fix": "eslint . --fix",
"lint:check": "eslint .",
"format:fix": "prettier --write .",
"format:check": "prettier --check .",
"migrate": "node migrations/migrate-up.js"
"migrate": "node migrations/migrate-up.js",
"prepare": "husky install"
},
"repository": {
"type": "git",
Expand All @@ -33,6 +35,7 @@
},
"homepage": "https://github.com/AgileCraftsmanshipCanarias/kata-setup-javascript#readme",
"dependencies": {
"mongodb": "^6.1.0",
"pg": "^8.11.3"
}
}
2 changes: 1 addition & 1 deletion src/application/RegisterUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class RegisterUser {

const user = User.create(this.idGenerator.generate(), name, email, password, age)

await this.emailSender.send(email, "Welcome to our platform!")
await this.emailSender.sendWelcomeEmail(user)

await this.userRepository.save(user)
}
Expand Down
8 changes: 6 additions & 2 deletions src/application/RegisterUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("RegisterUser", () => {
vi.spyOn(userRepository, "save")
idGenerator = new IdGeneratorMock()
emailSender = new EmailSenderMock()
vi.spyOn(emailSender, "send")
vi.spyOn(emailSender, "sendWelcomeEmail")
registerUser = new RegisterUser(userRepository, idGenerator, emailSender)
})

Expand Down Expand Up @@ -51,10 +51,14 @@ describe("RegisterUser", () => {
})

it("sends a welcome email to the user", async () => {
const name = "John Doe"
const email = "[email protected]"
const age = 18
const password = "password"

await registerUser.execute(notImportantName, email, notImportantPassword, notImportantAge)

expect(emailSender.send).toHaveBeenCalledWith(email, "Welcome to our platform!")
const user = User.create(IdGeneratorMock.MOCK_ID, name, email, password, age)
expect(emailSender.sendWelcomeEmail).toHaveBeenCalledWith(user)
})
})
2 changes: 1 addition & 1 deletion src/domain/services/EmailSender.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class EmailSender {
// eslint-disable-next-line no-unused-vars
send(email, message) {
sendWelcomeEmail(user) {
throw new Error("This is an abstract class. You should implement the send method")
}
}
1 change: 1 addition & 0 deletions src/domain/utils/sleep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
12 changes: 12 additions & 0 deletions src/infrastructure/Config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const config = {
mailgun: {
apiKey: process.env.MAILGUN_API_KEY ?? "",
domain: process.env.MAILGUN_DOMAIN ?? "sandbox261f754ab73b43388177e85a621a13fb.mailgun.org",
fromAddressStart: "mailgun", // this means mailgun@domain.com
fromAddressName: "Dan",
},
testmail: {
apiKey: process.env.TESTMAIL_API_KEY ?? "",
namespace: process.env.TESTMAIL_NAMESPACE ?? "9eqfr",
},
}
Loading

0 comments on commit 2368534

Please sign in to comment.