Skip to content

Commit

Permalink
Merge branch 'release-3.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 3, 2017
2 parents 0567c61 + 7170ba4 commit a32876f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 56 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="3.0.3"></a>
## [3.0.3](https://github.com/adonisjs/adonis-mail/compare/v3.0.2...v3.0.3) (2017-10-03)


### Bug Fixes

* **mail:** use in-memory mail fake ([4665cbe](https://github.com/adonisjs/adonis-mail/commit/4665cbe))



<a name="3.0.2"></a>
## [3.0.2](https://github.com/adonisjs/adonis-mail/compare/v3.0.1...v3.0.2) (2017-09-06)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adonisjs/mail",
"version": "3.0.2",
"version": "3.0.3",
"description": "Mail provider for adonis framework and has support for all common mailing services to send emails",
"main": "index.js",
"directories": {
Expand Down
12 changes: 0 additions & 12 deletions src/Mail/Fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* file that was distributed with this source code.
*/

const { ioc } = require('@adonisjs/fold')
const MailManager = require('./Manager')
const clone = require('clone')
const proxyMethods = ['send', 'raw']
Expand Down Expand Up @@ -85,17 +84,6 @@ class FakeMail {
clear () {
this._mails = []
}

/**
* Restore the mail fake
*
* @method restore
*
* @return {void}
*/
restore () {
ioc.restore('Adonis/Addons/Mail')
}
}

proxyMethods.forEach((method) => {
Expand Down
43 changes: 38 additions & 5 deletions src/Mail/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,31 @@
* file that was distributed with this source code.
*/

const { ioc } = require('@adonisjs/fold')
const GE = require('@adonisjs/generic-exceptions')
const MailManager = require('./Manager')
const proxyMethods = ['send', 'raw']

const proxyHandler = {
get (target, name) {
/**
* if node is inspecting then stick to target properties
*/
if (typeof (name) === 'symbol' || name === 'inspect') {
return target[name]
}

/**
* If a faker object exists, give preference to it over
* the actual methods
*/
if (target._fake && target._fake[name] !== undefined) {
return typeof (target._fake[name]) === 'function' ? target._fake[name].bind(target._fake) : target._fake[name]
}

return target[name]
}
}

/**
* The mail class is used to grab an instance of
* sender for a given connection and driver.
Expand All @@ -29,6 +49,9 @@ class Mail {
this.Config = Config
this.View = View
this._sendersPool = {}
this._fake = null

return new Proxy(this, proxyHandler)
}

/**
Expand Down Expand Up @@ -83,16 +106,26 @@ class Mail {
}

/**
* Binding a fake to the Ioc container for the mail. It
* can be used to fake the emails and instead get
* them back as json objects.
* Setup a faker object, which will be used over
* using the actual emailer methods
*
* @method fake
*
* @return {void}
*/
fake () {
ioc.singletonFake('Adonis/Addons/Mail', () => new (require('./Fake'))(this.Config, this.View))
this._fake = new (require('./Fake'))(this.Config, this.View)
}

/**
* Restore faker object
*
* @method restore
*
* @return {void}
*/
restore () {
this._fake = null
}
}

Expand Down
80 changes: 42 additions & 38 deletions test/fake-mail.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,99 +9,106 @@
* file that was distributed with this source code.
*/

const { ioc } = require('@adonisjs/fold')
const test = require('japa')
const { Config } = require('@adonisjs/sink')
const FakeMail = require('../src/Mail/Fake')
const Mail = require('../src/Mail')

test.group('FakeMail', () => {
test('fake mail send all emails via memory driver', async (assert) => {
const fakeMail = new FakeMail(new Config())
const response = await fakeMail.raw('Hello everyone', (message) => {
const mail = new Mail(new Config())
mail.fake()

const response = await mail.raw('Hello everyone', (message) => {
message.from('foo@bar.com')
message.to('baz@bar.com')
})

assert.equal(response.message.from.address, 'foo@bar.com')
assert.equal(response.message.to[0].address, 'baz@bar.com')
assert.equal(response.message.text, 'Hello everyone')
})

test('store sent email in memory', async (assert) => {
const fakeMail = new FakeMail(new Config())
const response = await fakeMail.raw('Hello everyone', (message) => {
const mail = new Mail(new Config())
mail.fake()

const response = await mail.raw('Hello everyone', (message) => {
message.from('foo@bar.com')
message.to('baz@bar.com')
})
assert.deepEqual(response, fakeMail._mails[0])

assert.deepEqual(response, mail._mails[0])
})

test('give last email from the mails array', async (assert) => {
const fakeMail = new FakeMail(new Config())
const response = await fakeMail.raw('Hello everyone', (message) => {
const mail = new Mail(new Config())
mail.fake()

const response = await mail.raw('Hello everyone', (message) => {
message.from('foo@bar.com')
message.to('baz@bar.com')
})
assert.deepEqual(response, fakeMail.recent())

assert.deepEqual(response, mail.recent())
})

test('pull last email from array', async (assert) => {
const fakeMail = new FakeMail(new Config())
await fakeMail.raw('Hello everyone', (message) => {
const mail = new Mail(new Config())
mail.fake()

await mail.raw('Hello everyone', (message) => {
message.from('foo@bar.com')
message.to('baz@bar.com')
})

const response = await fakeMail.raw('Another one', (message) => {
const response = await mail.raw('Another one', (message) => {
message.from('foo@bar.com')
message.to('baz@bar.com')
})

assert.deepEqual(response, fakeMail.pullRecent())
assert.lengthOf(fakeMail._mails, 1)
assert.deepEqual(response, mail.pullRecent())
assert.lengthOf(mail._mails, 1)
})

test('return a copy of all emails', async (assert) => {
const fakeMail = new FakeMail(new Config())
await fakeMail.raw('Hello everyone', (message) => {
const mail = new Mail(new Config())
mail.fake()

await mail.raw('Hello everyone', (message) => {
message.from('foo@bar.com')
message.to('baz@bar.com')
message.subject('Hello everyone')
})

await fakeMail.raw('Another one', (message) => {
await mail.raw('Another one', (message) => {
message.from('foo@bar.com')
message.to('baz@bar.com')
})

const mails = fakeMail.all()
const mails = mail.all()
assert.lengthOf(mails, 2)
mails[0].message.subject = 'Foo'
assert.equal(fakeMail._mails[0].message.subject, 'Hello everyone')
assert.equal(mail._mails[0].message.subject, 'Hello everyone')
})

test('clear all emails', async (assert) => {
const fakeMail = new FakeMail(new Config())
await fakeMail.raw('Hello everyone', (message) => {
const mail = new Mail(new Config())
mail.fake()

await mail.raw('Hello everyone', (message) => {
message.from('foo@bar.com')
message.to('baz@bar.com')
message.subject('Hello everyone')
})

await fakeMail.raw('Another one', (message) => {
await mail.raw('Another one', (message) => {
message.from('foo@bar.com')
message.to('baz@bar.com')
})

fakeMail.clear()
assert.lengthOf(fakeMail.all(), 0)
})

test('bind fake mailer when fake method is called', async (assert) => {
const mail = new Mail(new Config())
mail.fake()
assert.instanceOf(ioc.use('Adonis/Addons/Mail'), FakeMail)
ioc.use('Adonis/Addons/Mail').restore()
mail.clear()
assert.lengthOf(mail.all(), 0)
})

test('restore fake mailer', async (assert) => {
Expand All @@ -110,13 +117,10 @@ test.group('FakeMail', () => {
const mail = new Mail(new Config())
mail.fake()

assert.instanceOf(ioc.use('Adonis/Addons/Mail'), FakeMail)
ioc.use('Adonis/Addons/Mail').restore()
assert.instanceOf(mail._fake, FakeMail)

mail.restore()

try {
ioc.use('Adonis/Addons/Mail')
} catch ({ message }) {
assert.equal(message, `Cannot find module 'Adonis/Addons/Mail'`)
}
assert.isNull(mail._fake)
})
})

0 comments on commit a32876f

Please sign in to comment.