@tryghost/email-mock-receiver 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013-2023 Ghost Foundation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Email Mock Receiver
2
+
3
+ Mocks email sending method and manages snapshots for outgoing email information such as to addresses, html content of emails, etc.
4
+
5
+ ## Install
6
+
7
+ `npm install @tryghost/email-mock-receiver --save`
8
+
9
+ or
10
+
11
+ `yarn add @tryghost/email-mock-receiver`
12
+
13
+ ## Usage
14
+
15
+
16
+ ## Develop
17
+
18
+ This is a monorepo package.
19
+
20
+ Follow the instructions for the top-level repo.
21
+ 1. `git clone` this repo & `cd` into it as usual
22
+ 2. Run `yarn` to install top-level dependencies.
23
+
24
+
25
+
26
+ ## Test
27
+
28
+ - `yarn lint` run just eslint
29
+ - `yarn test` run lint and tests
30
+
package/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./lib/email-mock-receiver');
@@ -0,0 +1,77 @@
1
+ const assert = require('assert');
2
+ const {AssertionError} = require('assert');
3
+
4
+ class EmailMockReceiver {
5
+ #sendResponse;
6
+ #snapshotManager;
7
+ #snapshots = [];
8
+
9
+ constructor({snapshotManager, sendResponse = 'Mail is disabled'}) {
10
+ this.#snapshotManager = snapshotManager;
11
+ this.#sendResponse = sendResponse;
12
+ }
13
+
14
+ /**
15
+ * Method mocking email sending logic
16
+ *
17
+ * @param {Object} message
18
+ * @param {string} message.subject - email subject
19
+ * @param {string} message.html - email content
20
+ * @param {string} message.to - email recipient address
21
+ * @param {string} [message.replyTo]
22
+ * @param {string} [message.from] - sender email address
23
+ * @param {string} [message.text] - text version of this message
24
+ */
25
+ async send(message) {
26
+ // store snapshot
27
+ this.#snapshots.push(message);
28
+
29
+ return this.#sendResponse;
30
+ }
31
+
32
+ sentEmailCount(count) {
33
+ assert.equal(this.#snapshots.length, count, 'Email count does not match');
34
+ }
35
+
36
+ matchHTMLSnapshot(snapshotIndex = 0) {
37
+ const error = new AssertionError({});
38
+
39
+ let assertion = {
40
+ properties: null,
41
+ field: 'html',
42
+ hint: `[html ${snapshotIndex + 1}]`,
43
+ error
44
+ };
45
+
46
+ this.#snapshotManager.assertSnapshot({
47
+ html: this.#snapshots[snapshotIndex].html
48
+ }, assertion);
49
+
50
+ return this;
51
+ }
52
+
53
+ matchMetadataSnapshot(snapshotIndex = 0, properties = {}) {
54
+ const error = new AssertionError({});
55
+ let assertion = {
56
+ properties: properties,
57
+ field: 'metadata',
58
+ hint: `[metadata ${snapshotIndex + 1}]`,
59
+ error
60
+ };
61
+
62
+ const metadata = Object.assign({}, this.#snapshots[snapshotIndex]);
63
+ delete metadata.html;
64
+
65
+ this.#snapshotManager.assertSnapshot({
66
+ metadata
67
+ }, assertion);
68
+
69
+ return this;
70
+ }
71
+
72
+ reset() {
73
+ this.#snapshots = [];
74
+ }
75
+ }
76
+
77
+ module.exports = EmailMockReceiver;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@tryghost/email-mock-receiver",
3
+ "version": "0.1.0",
4
+ "repository": "https://github.com/TryGhost/framework/tree/main/packages/email-mock-receiver",
5
+ "author": "Ghost Foundation",
6
+ "license": "MIT",
7
+ "main": "index.js",
8
+ "scripts": {
9
+ "dev": "echo \"Implement me!\"",
10
+ "test:unit": "NODE_ENV=testing c8 --all --check-coverage --100 --reporter text --reporter cobertura mocha './test/**/*.test.js'",
11
+ "test": "yarn test:unit",
12
+ "lint:code": "eslint *.js lib/ --ext .js --cache",
13
+ "lint": "yarn lint:code && yarn lint:test",
14
+ "lint:test": "eslint -c test/.eslintrc.js test/ --ext .js --cache"
15
+ },
16
+ "files": [
17
+ "index.js",
18
+ "lib"
19
+ ],
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "devDependencies": {
24
+ "c8": "7.12.0",
25
+ "mocha": "10.2.0",
26
+ "sinon": "15.0.1"
27
+ },
28
+ "gitHead": "7d645deb73825642289309a43333ee64b9a3a5d1"
29
+ }