Skip to content

Commit

Permalink
test(domain): test the newsletter service
Browse files Browse the repository at this point in the history
  • Loading branch information
tericcabrel committed Nov 1, 2023
1 parent a49bee6 commit af61462
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import SharinganError from '@sharingan/utils';
import nock from 'nock';

import NewsletterService from '../../../src/newsletters/newsletter.service';

const newsletterService = new NewsletterService({
apiKey: 'apiKey',
formId: 'formId',
});

const baseURL = 'https://api.convertkit.com/v3';

describe('Test the newsletter service', () => {
test('Add the email address to the newsletter subscribers', async () => {
// GIVEN
const emailToSubscribe = '[email protected]';
const tags = ['sharingan'];
const formId = 'formId';

const scope = nock(baseURL)
.post(`/forms/${formId}/subscribe`, {
api_key: 'apiKey',
email: emailToSubscribe,
tags,
})
.reply(200, {
subscription: {
id: '123ABC',
},
});

// WHEN
await newsletterService.subscribe(emailToSubscribe, tags);

// THEN
expect(scope.isDone()).toBe(true);

nock.cleanAll();
});

test('Handle HTTP error when the request to add the email address to the newsletter subscribers fails', async () => {
// GIVEN
const emailToSubscribe = '[email protected]';
const tags = ['sharingan'];
const formId = 'formId';

nock(baseURL)
.post(`/forms/${formId}/subscribe`, {
api_key: 'apiKey',
email: emailToSubscribe,
tags,
})
.reply(400, {
message: 'Wrong api key provided!',
});

// WHEN
// THEN
const catchErrorsFormatted = {
data: {
message: 'Wrong api key provided!',
},
message: 'Request failed with status code 400',
status: 400,
};

await expect(async () => {
await newsletterService.subscribe(emailToSubscribe, tags);
}).rejects.toThrow(
new SharinganError(JSON.stringify(catchErrorsFormatted, null, 2), 'NEWSLETTER_SUBSCRIBE_FAILED'),
);

nock.cleanAll();
});
});
1 change: 1 addition & 0 deletions packages/domain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@types/bcryptjs": "^2.4.2",
"@types/jest": "^28.1.4",
"jest": "^28.1.2",
"nock": "^13.2.9",
"ts-jest": "^28.0.5",
"typescript": "4.8.4"
}
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9356,6 +9356,11 @@ json-stable-stringify@^1.0.1:
dependencies:
jsonify "~0.0.0"

json-stringify-safe@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==

json-to-pretty-yaml@^1.2.2:
version "1.2.2"
resolved "https://registry.npmjs.org/json-to-pretty-yaml/-/json-to-pretty-yaml-1.2.2.tgz"
Expand Down Expand Up @@ -10322,6 +10327,16 @@ no-case@^3.0.4:
lower-case "^2.0.2"
tslib "^2.0.3"

nock@^13.2.9:
version "13.2.9"
resolved "https://registry.yarnpkg.com/nock/-/nock-13.2.9.tgz#4faf6c28175d36044da4cfa68e33e5a15086ad4c"
integrity sha512-1+XfJNYF1cjGB+TKMWi29eZ0b82QOvQs2YoLNzbpWGqFMtRQHTa57osqdGj4FrFPgkO4D4AZinzUJR9VvW3QUA==
dependencies:
debug "^4.1.0"
json-stringify-safe "^5.0.1"
lodash "^4.17.21"
propagate "^2.0.0"

node-dir@^0.1.17:
version "0.1.17"
resolved "https://registry.npmjs.org/node-dir/-/node-dir-0.1.17.tgz"
Expand Down Expand Up @@ -11193,6 +11208,11 @@ prop-types@^15.7.2, prop-types@^15.8.1:
object-assign "^4.1.1"
react-is "^16.13.1"

propagate@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45"
integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==

property-expr@^2.0.4:
version "2.0.5"
resolved "https://registry.npmjs.org/property-expr/-/property-expr-2.0.5.tgz"
Expand Down

0 comments on commit af61462

Please sign in to comment.