Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Added Test and example for deleteMessage()
Browse files Browse the repository at this point in the history
  • Loading branch information
synox committed Mar 28, 2019
1 parent 155d9bb commit f42de65
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Expand Up @@ -264,6 +264,29 @@ imaps.connect(config).then(function (connection) {
});
```


### delete messages by uid

```js
imaps.connect(config).then(connection => {

return connection.openBox('INBOX')
.then(() => connection.search(['ALL'], {bodies: ['HEADER']}))
.then( messages => {

// select messages from bob
const uidsToDelete = messages
.filter( message => {
return message.parts
.filter( part => part.which === 'HEADER')[0].body.to[0] === 'bob@example.com';
})
.map(message => message.attributes.uid);

return connection.deleteMessage(uidsToDelete);
});
});
```

## API

### Exported module
Expand Down
43 changes: 42 additions & 1 deletion test/index.spec.js
@@ -1,5 +1,5 @@
'use strict';
var { startTestServer,appendMessage } = require('./imapTestServer');
var {startTestServer, appendMessage} = require('./imapTestServer');
var expect = require('chai').expect;

var serverInstance = null;
Expand Down Expand Up @@ -66,5 +66,46 @@ describe('imap-simple', function () {
});

});

it('deletes messages', function () {

return imaps.connect(config).then(function (connection) {

return connection.openBox('INBOX')
.then(function () {return appendMessage(connection, 'jim@example.com', 'hello from jim');})
.then(function () {return appendMessage(connection, 'bob@example.com', 'hello from bob');})
.then(function () {return appendMessage(connection, 'bob@example.com', 'hello again from bob');})
.then(function () {return connection.search(['ALL'], {bodies: ['HEADER']});})
.then(function (messages) {

var uidsToDelete = messages
.filter(function (message) {
return message.parts.filter(function (part) {
return part.which === 'HEADER';
})[0].body.to[0] === 'bob@example.com';
})
.map(function (message) {
return message.attributes.uid;
});

return connection.deleteMessage(uidsToDelete);
})
.then(function () {
return connection.search(['ALL'], {bodies: ['HEADER']});
}).then(function (messages) {

var subjects = messages.map(function (res) {
return res.parts.filter(function (part) {
return part.which === 'HEADER';
})[0].body.subject[0];
});

expect(subjects).to.eql([
'hello from jim'
]);
console.log(subjects);
});
});
});
});

0 comments on commit f42de65

Please sign in to comment.